Blame view

app/Classes/SortData.php 1.49 KB
28e548150   Андрей Ларионов   Рефакторинг кода ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  <?php
  
  
  namespace App\Classes;
  
  
  class SortData
  {
      protected $builder;
      protected $request;
  
      public function __construct($builder, $request) {
          $this->builder = $builder;
          $this->request = $request;
      }
  
      public function apply() {
          foreach ($this->sorter() as $filter => $value) {
              if (method_exists($this, $filter)) {
                  $this->filter($value);
              }
          }
          return $this->builder;
      }
  
      public function sort_price($value) {
          switch ($value) {
              case 1: $this->builder = $this->builder->orderBy('price');break;
              case 2: $this->builder = $this->builder->orderByDesc('price');break;
              default: $this->builder = $this->builder->orderBy('price');break;
          }
      }
  
      public function sort_new($value) {
          switch ($value) {
              case 1: $this->builder = $this->builder->orderByDesc('created_at');break;
              case 2: $this->builder = $this->builder->orderBy('created_at');break;
              default: $this->builder = $this->builder->orderByDesc('created_at');break;
          }
      }
  
      public function sort_area($value) {
          switch ($value) {
              case 1: $this->builder = $this->builder->orderByDesc('area');break;
              case 2: $this->builder = $this->builder->orderBy('area');break;
              default: $this->builder = $this->builder->orderByDesc('area');break;
          }
      }
  
      public function sorter() {
          return $this->request->all();
      }
  
  }