1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiapi\commands\Search\Filter; |
4
|
|
|
|
5
|
|
|
use hiapi\commands\Search\Filter\Type\ArrayOfFilter; |
6
|
|
|
use hiapi\commands\Search\Filter\Type\FilterInterface; |
7
|
|
|
use hiapi\commands\Search\Filter\Type\GenericFilter; |
8
|
|
|
use hiapi\commands\Search\Filter\Type\IntegerFilter; |
9
|
|
|
|
10
|
|
|
class SubfiltersProducer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @psalm-var array<class-string<FilterInterface>, array<string, class-string<FilterInterface>|list<class-string<FilterInterface>>>> |
14
|
|
|
*/ |
15
|
|
|
private $map = [ |
16
|
|
|
// Either this way |
17
|
|
|
IntegerFilter::class => [ |
18
|
|
|
'_ne' => IntegerFilter::class, |
19
|
|
|
'_gt' => IntegerFilter::class, |
20
|
|
|
'_lt' => IntegerFilter::class, |
21
|
|
|
'_in' => [ArrayOfFilter::class, IntegerFilter::class], |
22
|
|
|
'_ni' => [ArrayOfFilter::class, IntegerFilter::class], |
23
|
|
|
] |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @psalm-var array< |
28
|
|
|
string, |
29
|
|
|
array<string, string|array{class-string<FilterInterface>, string}> |
30
|
|
|
> |
31
|
|
|
*/ |
32
|
|
|
private $constantsMap = [ |
33
|
|
|
// OR this way. |
34
|
|
|
// Cons: |
35
|
|
|
// - no real namespaces for constant values |
36
|
|
|
// - filter name should be passed nearby as a separate property |
37
|
|
|
GenericFilter::INTEGER => [ |
38
|
|
|
'_ne' => GenericFilter::INTEGER, |
39
|
|
|
'_gt' => GenericFilter::INTEGER, |
40
|
|
|
'_lt' => GenericFilter::INTEGER, |
41
|
|
|
'_in' => [ArrayOfFilter::class, GenericFilter::INTEGER], |
42
|
|
|
'_ni' => [ArrayOfFilter::class, GenericFilter::INTEGER], |
43
|
|
|
] |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param FilterInterface $filter |
48
|
|
|
* @return FilterInterface[] |
49
|
|
|
* @psalm-return list<FilterInterface> |
50
|
|
|
*/ |
51
|
|
|
public function produce(FilterInterface $filter): array |
52
|
|
|
{ |
53
|
|
|
return $filter instanceof GenericFilter |
54
|
|
|
? $this->produceAsGeneric($filter) |
55
|
|
|
: $this->produceAsSeparateClass($filter); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
private function produceAsGeneric(FilterInterface $filter): array |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$type = $filter->type(); |
|
|
|
|
61
|
|
|
if (!isset($this->constantsMap[$type])) { |
62
|
|
|
return []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$result = []; |
66
|
|
|
foreach ($this->constantsMap[$type] as $suffix => $newType) { |
67
|
|
|
$attributeName = $filter->name() . $suffix; |
68
|
|
|
|
69
|
|
|
if (is_array($newType)) { |
70
|
|
|
$className = array_shift($newType); |
71
|
|
|
$result[] = new $className($attributeName, $newType[0]); |
72
|
|
|
} else { |
73
|
|
|
$result[] = GenericFilter::__callStatic($newType, [$attributeName]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $result; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
private function produceAsSeparateClass(FilterInterface $filter): array |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$type = get_class($filter); |
83
|
|
|
if (!isset($this->map[$type])) { |
84
|
|
|
return []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$result = []; |
88
|
|
|
foreach ($this->map[$type] as $suffix => $newType) { |
89
|
|
|
$attributeName = $filter->name() . $suffix; |
90
|
|
|
|
91
|
|
|
if (is_array($newType)) { |
92
|
|
|
$className = array_shift($newType); |
93
|
|
|
$result[] = new $className($attributeName, $newType[0]); |
94
|
|
|
} else { |
95
|
|
|
$result[] = new $newType($attributeName); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $result; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.