1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dc\Filter; |
4
|
|
|
|
5
|
|
|
use dc\Filter\Type; |
6
|
|
|
use dc\Iterator\RecursiveIterator; |
7
|
|
|
use dc\Exception\CollectionException; |
8
|
|
|
|
9
|
|
|
class Collection implements \IteratorAggregate |
10
|
|
|
{ |
11
|
|
|
const LOGICAL_AND = 'and'; |
12
|
|
|
const LOGICAL_OR = 'or'; |
13
|
|
|
const LOGICAL_XAND = 'xand'; |
14
|
|
|
const LOGICAL_XOR = 'xor'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $logical; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $data = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var null | Collection |
28
|
|
|
*/ |
29
|
|
|
private $parent; |
30
|
|
|
|
31
|
19 |
|
public function __construct(string $logical = self::LOGICAL_AND, Collection $parent = null) |
32
|
|
|
{ |
33
|
19 |
|
$this->logical = $logical; |
34
|
19 |
|
$this->parent = $parent; |
35
|
19 |
|
} |
36
|
|
|
|
37
|
3 |
|
public function getLogical(): string |
38
|
|
|
{ |
39
|
3 |
|
return $this->logical; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Collection |
44
|
|
|
* @throws CollectionException |
45
|
|
|
*/ |
46
|
3 |
|
public function parent(): Collection |
47
|
|
|
{ |
48
|
3 |
|
if ($this->parent === null) { |
49
|
1 |
|
throw CollectionException::noParentFound(); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
return $this->parent; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $index |
57
|
|
|
* @return Collection |
58
|
|
|
* @throws CollectionException |
59
|
|
|
*/ |
60
|
3 |
|
public function child(int $index): Collection |
61
|
|
|
{ |
62
|
3 |
|
$indexCount = 0; |
63
|
3 |
|
foreach ($this->data as $data) { |
64
|
2 |
|
if ($data instanceof Collection) { |
65
|
2 |
|
if ($indexCount === $index) { |
66
|
2 |
|
return $data; |
67
|
|
|
} |
68
|
1 |
|
$indexCount++; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
throw CollectionException::noChildFound($index); |
73
|
|
|
} |
74
|
|
|
|
75
|
15 |
|
private function add($data) |
76
|
|
|
{ |
77
|
15 |
|
$this->data[] = $data; |
78
|
15 |
|
} |
79
|
|
|
|
80
|
3 |
|
public function addCollection(string $logical = self::LOGICAL_AND) |
81
|
|
|
{ |
82
|
3 |
|
$collection = new self($logical, $this); |
83
|
3 |
|
$this->add($collection); |
84
|
|
|
|
85
|
3 |
|
return $collection; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function addCustomFilter(string $key, $value, string $comparison) |
89
|
|
|
{ |
90
|
1 |
|
$this->add(new Type\Custom($key, $value, $comparison)); |
91
|
|
|
|
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
public function addEqual(string $key, $value) |
96
|
|
|
{ |
97
|
2 |
|
$this->add(new Type\Equal($key, $value)); |
98
|
|
|
|
99
|
2 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function addExists(string $key, bool $value) |
103
|
|
|
{ |
104
|
1 |
|
$this->add(new Type\Exists($key, $value)); |
105
|
|
|
|
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
public function addGreaterThan(string $key, $value) |
110
|
|
|
{ |
111
|
1 |
|
$this->add(new Type\GreaterThan($key, $value)); |
112
|
|
|
|
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function addGreaterThanEqual(string $key, $value) |
117
|
|
|
{ |
118
|
1 |
|
$this->add(new Type\GreaterThanEqual($key, $value)); |
119
|
|
|
|
120
|
1 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function addIn(string $key, array $value) |
124
|
|
|
{ |
125
|
1 |
|
$this->add(new Type\In($key, $value)); |
126
|
|
|
|
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
public function addLowerThan(string $key, $value) |
131
|
|
|
{ |
132
|
1 |
|
$this->add(new Type\LowerThan($key, $value)); |
133
|
|
|
|
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
public function addLowerThanEqual(string $key, $value) |
138
|
|
|
{ |
139
|
1 |
|
$this->add(new Type\LowerThanEqual($key, $value)); |
140
|
|
|
|
141
|
1 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
public function addNotEqual(string $key, $value) |
145
|
|
|
{ |
146
|
1 |
|
$this->add(new Type\NotEqual($key, $value)); |
147
|
|
|
|
148
|
1 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
public function addNotIn(string $key, array $value) |
152
|
|
|
{ |
153
|
1 |
|
$this->add(new Type\NotIn($key, $value)); |
154
|
|
|
|
155
|
1 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
public function addQuery(string $key, string $value) |
159
|
|
|
{ |
160
|
1 |
|
$this->add(new Type\Query($key, $value)); |
161
|
|
|
|
162
|
1 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
public function addRegex(string $key, string $value) |
166
|
|
|
{ |
167
|
1 |
|
$this->add(new Type\Regex($key, $value)); |
168
|
|
|
|
169
|
1 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
public function getIterator() |
173
|
|
|
{ |
174
|
1 |
|
$recursiveIterator = new RecursiveIterator($this->getData()); |
175
|
1 |
|
return new \RecursiveIteratorIterator($recursiveIterator, \RecursiveIteratorIterator::SELF_FIRST); |
176
|
|
|
} |
177
|
|
|
|
178
|
14 |
|
public function getData(): array |
179
|
|
|
{ |
180
|
14 |
|
$this->sortCollectionToTheEndOfArray(); |
181
|
|
|
|
182
|
14 |
|
return $this->data; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
private function sortCollectionToTheEndOfArray() |
186
|
|
|
{ |
187
|
14 |
|
uasort($this->data, function($a) { |
188
|
1 |
|
if ($a instanceof Collection) { |
189
|
1 |
|
return 1; |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
return -1; |
193
|
14 |
|
}); |
194
|
14 |
|
} |
195
|
|
|
} |
196
|
|
|
|