Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
20 / 20 |
Filters | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
20 / 20 |
__construct | |
100.00% |
1 / 1 |
4 | |
100.00% |
9 / 9 |
|||
__get | |
100.00% |
1 / 1 |
3 | |
100.00% |
3 / 3 |
|||
getErrors | |
100.00% |
1 / 1 |
3 | |
100.00% |
8 / 8 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\ApiFilterRequest; |
4 | |
5 | use Illuminate\Support\Str; |
6 | use Qmp\Laravel\ApiFilterRequest\Filters\Fields; |
7 | use Qmp\Laravel\ApiFilterRequest\Filters\OrderBy; |
8 | use Qmp\Laravel\ApiFilterRequest\Filters\Conditions; |
9 | use Qmp\Laravel\ApiFilterRequest\Filters\Aggregate; |
10 | use Qmp\Laravel\ApiFilterRequest\Filters\Limit; |
11 | |
12 | class Filters |
13 | { |
14 | protected $variables = ['fields', 'orderBy', 'conditions', 'aggregate', 'limit']; |
15 | |
16 | /** |
17 | * @var Fields |
18 | */ |
19 | private $fields; |
20 | |
21 | /** |
22 | * @var OrderBy |
23 | */ |
24 | private $orderBy; |
25 | |
26 | /** |
27 | * @var Conditions |
28 | */ |
29 | private $conditions; |
30 | |
31 | /** |
32 | * @var Aggregate |
33 | */ |
34 | private $aggregate; |
35 | |
36 | /** |
37 | * @var Limit |
38 | */ |
39 | private $limit; |
40 | |
41 | /** |
42 | * Filters constructor. |
43 | * @param array $parameters |
44 | */ |
45 | public function __construct(array $parameters) |
46 | { |
47 | $variables = $this->variables; |
48 | foreach ($variables as $key) { |
49 | $variable = Str::snake($key); |
50 | $classname = '\\Qmp\\Laravel\\ApiFilterRequest\\Filters\\' . Str::studly($key); |
51 | $this->$key = new $classname(); |
52 | if (isset($parameters[$variable])) { |
53 | |
54 | if ($this->$key->check($parameters[$variable])) { |
55 | $this->$key->set($parameters[$variable]); |
56 | } |
57 | } |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * Return object's propterty |
63 | * |
64 | * @param $variable |
65 | * @return null |
66 | */ |
67 | public function __get($variable) |
68 | { |
69 | if (property_exists($this, $variable) && $this->$variable !== null) { |
70 | return $this->$variable; |
71 | } |
72 | |
73 | return null; |
74 | } |
75 | |
76 | /** |
77 | * @return array |
78 | */ |
79 | public function getErrors() |
80 | { |
81 | $errors = []; |
82 | $variables = $this->variables; |
83 | foreach ($variables as $key) { |
84 | $error = $this->$key->getErrors(); |
85 | if (!empty($error)) { |
86 | $variable = Str::snake($key); |
87 | $errors[$variable] = $error; |
88 | } |
89 | } |
90 | |
91 | return $errors; |
92 | } |
93 | } |