@@ -19,7 +19,8 @@ discard block |
||
19 | 19 | |
20 | 20 | public function __construct(SpecificationInterface $expression) |
21 | 21 | { |
22 | - if (!$expression instanceof AbstractSorter) { |
|
22 | + if (!$expression instanceof AbstractSorter) |
|
23 | + { |
|
23 | 24 | throw new \LogicException('Only sorters allowed'); |
24 | 25 | } |
25 | 26 | |
@@ -33,13 +34,15 @@ discard block |
||
33 | 34 | { |
34 | 35 | $injector = static::INJECTION; |
35 | 36 | |
36 | - if (!class_exists($injector)) { |
|
37 | + if (!class_exists($injector)) |
|
38 | + { |
|
37 | 39 | throw new \LogicException( |
38 | 40 | sprintf('Class "%s" does not exist', $injector) |
39 | 41 | ); |
40 | 42 | } |
41 | 43 | |
42 | - if (!is_subclass_of($injector, FragmentInterface::class)) { |
|
44 | + if (!is_subclass_of($injector, FragmentInterface::class)) |
|
45 | + { |
|
43 | 46 | throw new \LogicException( |
44 | 47 | 'INJECTION class does not implement FragmentInterface' |
45 | 48 | ); |
@@ -53,23 +53,28 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function write($source, SpecificationInterface $specification, Compiler $compiler) |
55 | 55 | { |
56 | - if (!$this->targetAcceptable($source)) { |
|
56 | + if (!$this->targetAcceptable($source)) |
|
57 | + { |
|
57 | 58 | return null; |
58 | 59 | } |
59 | 60 | |
60 | - if ($specification instanceof Specification\FilterInterface) { |
|
61 | + if ($specification instanceof Specification\FilterInterface) |
|
62 | + { |
|
61 | 63 | return $this->writeFilter($source, $specification, $compiler); |
62 | 64 | } |
63 | 65 | |
64 | - if ($specification instanceof Specification\SorterInterface) { |
|
66 | + if ($specification instanceof Specification\SorterInterface) |
|
67 | + { |
|
65 | 68 | return $this->writeSorter($source, $specification, $compiler); |
66 | 69 | } |
67 | 70 | |
68 | - if ($specification instanceof Specification\Pagination\Limit) { |
|
71 | + if ($specification instanceof Specification\Pagination\Limit) |
|
72 | + { |
|
69 | 73 | return $source->limit($specification->getValue()); |
70 | 74 | } |
71 | 75 | |
72 | - if ($specification instanceof Specification\Pagination\Offset) { |
|
76 | + if ($specification instanceof Specification\Pagination\Offset) |
|
77 | + { |
|
73 | 78 | return $source->offset($specification->getValue()); |
74 | 79 | } |
75 | 80 | |
@@ -84,15 +89,18 @@ discard block |
||
84 | 89 | */ |
85 | 90 | protected function writeFilter($source, Specification\FilterInterface $filter, Compiler $compiler) |
86 | 91 | { |
87 | - if ($filter instanceof Specification\Filter\All || $filter instanceof Specification\Filter\Map) { |
|
92 | + if ($filter instanceof Specification\Filter\All || $filter instanceof Specification\Filter\Map) |
|
93 | + { |
|
88 | 94 | return $source->where(static function () use ($compiler, $filter, $source): void { |
89 | 95 | $compiler->compile($source, ...$filter->getFilters()); |
90 | 96 | }); |
91 | 97 | } |
92 | 98 | |
93 | - if ($filter instanceof Specification\Filter\Any) { |
|
99 | + if ($filter instanceof Specification\Filter\Any) |
|
100 | + { |
|
94 | 101 | return $source->where(static function () use ($compiler, $filter, $source): void { |
95 | - foreach ($filter->getFilters() as $subFilter) { |
|
102 | + foreach ($filter->getFilters() as $subFilter) |
|
103 | + { |
|
96 | 104 | $source->orWhere(static function () use ($compiler, $subFilter, $source): void { |
97 | 105 | $compiler->compile($source, $subFilter); |
98 | 106 | }); |
@@ -100,9 +108,11 @@ discard block |
||
100 | 108 | }); |
101 | 109 | } |
102 | 110 | |
103 | - if ($filter instanceof Specification\Filter\InjectionFilter) { |
|
111 | + if ($filter instanceof Specification\Filter\InjectionFilter) |
|
112 | + { |
|
104 | 113 | $expression = $filter->getFilter(); |
105 | - if ($expression instanceof Specification\Filter\Expression) { |
|
114 | + if ($expression instanceof Specification\Filter\Expression) |
|
115 | + { |
|
106 | 116 | return $source->where( |
107 | 117 | $filter->getInjection(), |
108 | 118 | $this->getExpressionOperator($expression), |
@@ -111,7 +121,8 @@ discard block |
||
111 | 121 | } |
112 | 122 | } |
113 | 123 | |
114 | - if ($filter instanceof Specification\Filter\Expression) { |
|
124 | + if ($filter instanceof Specification\Filter\Expression) |
|
125 | + { |
|
115 | 126 | return $source->where( |
116 | 127 | $filter->getExpression(), |
117 | 128 | $this->getExpressionOperator($filter), |
@@ -128,11 +139,13 @@ discard block |
||
128 | 139 | */ |
129 | 140 | protected function getExpressionOperator(Specification\Filter\Expression $filter): string |
130 | 141 | { |
131 | - if ($filter instanceof Specification\Filter\Like) { |
|
142 | + if ($filter instanceof Specification\Filter\Like) |
|
143 | + { |
|
132 | 144 | return 'LIKE'; |
133 | 145 | } |
134 | 146 | |
135 | - if ($filter instanceof Specification\Filter\InArray || $filter instanceof Specification\Filter\NotInArray) { |
|
147 | + if ($filter instanceof Specification\Filter\InArray || $filter instanceof Specification\Filter\NotInArray) |
|
148 | + { |
|
136 | 149 | return static::ARRAY_OPERATORS[get_class($filter)]; |
137 | 150 | } |
138 | 151 | |
@@ -145,11 +158,13 @@ discard block |
||
145 | 158 | */ |
146 | 159 | protected function getExpressionArgs(Specification\Filter\Expression $filter): array |
147 | 160 | { |
148 | - if ($filter instanceof Specification\Filter\Like) { |
|
161 | + if ($filter instanceof Specification\Filter\Like) |
|
162 | + { |
|
149 | 163 | return [sprintf($filter->getPattern(), $this->fetchValue($filter->getValue()))]; |
150 | 164 | } |
151 | 165 | |
152 | - if ($filter instanceof Specification\Filter\InArray || $filter instanceof Specification\Filter\NotInArray) { |
|
166 | + if ($filter instanceof Specification\Filter\InArray || $filter instanceof Specification\Filter\NotInArray) |
|
167 | + { |
|
153 | 168 | return [new Parameter($this->fetchValue($filter->getValue()))]; |
154 | 169 | } |
155 | 170 | |
@@ -164,8 +179,10 @@ discard block |
||
164 | 179 | */ |
165 | 180 | protected function writeSorter($source, Specification\SorterInterface $sorter, Compiler $compiler) |
166 | 181 | { |
167 | - if ($sorter instanceof Specification\Sorter\SorterSet) { |
|
168 | - foreach ($sorter->getSorters() as $subSorter) { |
|
182 | + if ($sorter instanceof Specification\Sorter\SorterSet) |
|
183 | + { |
|
184 | + foreach ($sorter->getSorters() as $subSorter) |
|
185 | + { |
|
169 | 186 | $source = $compiler->compile($source, $subSorter); |
170 | 187 | } |
171 | 188 | |
@@ -177,16 +194,19 @@ discard block |
||
177 | 194 | || $sorter instanceof Specification\Sorter\DescSorter |
178 | 195 | ) { |
179 | 196 | $direction = static::SORTER_DIRECTIONS[get_class($sorter)]; |
180 | - foreach ($sorter->getExpressions() as $expression) { |
|
197 | + foreach ($sorter->getExpressions() as $expression) |
|
198 | + { |
|
181 | 199 | $source = $source->orderBy($expression, $direction); |
182 | 200 | } |
183 | 201 | |
184 | 202 | return $source; |
185 | 203 | } |
186 | 204 | |
187 | - if ($sorter instanceof InjectionSorter) { |
|
205 | + if ($sorter instanceof InjectionSorter) |
|
206 | + { |
|
188 | 207 | $direction = static::SORTER_DIRECTIONS[get_class($sorter)] ?? 'ASC'; |
189 | - foreach ($sorter->getInjections() as $injection) { |
|
208 | + foreach ($sorter->getInjections() as $injection) |
|
209 | + { |
|
190 | 210 | $source = $source->orderBy($injection, $direction); |
191 | 211 | } |
192 | 212 | |
@@ -204,7 +224,8 @@ discard block |
||
204 | 224 | */ |
205 | 225 | protected function fetchValue($value) |
206 | 226 | { |
207 | - if ($value instanceof Specification\ValueInterface) { |
|
227 | + if ($value instanceof Specification\ValueInterface) |
|
228 | + { |
|
208 | 229 | throw new CompilerException('Value expects user input, none given'); |
209 | 230 | } |
210 | 231 | |
@@ -217,11 +238,13 @@ discard block |
||
217 | 238 | */ |
218 | 239 | protected function targetAcceptable($target): bool |
219 | 240 | { |
220 | - if (class_exists(SelectQuery::class) && $target instanceof SelectQuery) { |
|
241 | + if (class_exists(SelectQuery::class) && $target instanceof SelectQuery) |
|
242 | + { |
|
221 | 243 | return true; |
222 | 244 | } |
223 | 245 | |
224 | - if (class_exists(Select::class) && $target instanceof Select) { |
|
246 | + if (class_exists(Select::class) && $target instanceof Select) |
|
247 | + { |
|
225 | 248 | return true; |
226 | 249 | } |
227 | 250 |