1 | <?php |
||
21 | class InMemoryAdapter implements DataAdapterInterface |
||
22 | { |
||
23 | const EXPRESSION_IN_ARRAY = 'IN'; |
||
24 | const EXPRESSION_WILDCARD = 'LIKE'; |
||
25 | |||
26 | /** @var array */ |
||
27 | private $dataStorage; |
||
28 | /** @var string */ |
||
29 | private $dataGroup = 'default'; |
||
30 | /** @var string */ |
||
31 | private $idKey = 'id'; |
||
32 | |||
33 | /** |
||
34 | * PDOAdapter constructor. |
||
35 | * |
||
36 | * @param mixed $dataStorage |
||
37 | * |
||
38 | * @throws InvalidArgumentException |
||
39 | */ |
||
40 | 12 | public function __construct($dataStorage = null) |
|
41 | { |
||
42 | 12 | if (empty($dataStorage)) { |
|
43 | 8 | $dataStorage = []; |
|
44 | 8 | } |
|
45 | |||
46 | 12 | if (!is_array($dataStorage)) { |
|
47 | 1 | throw new InvalidArgumentException('The constructor parameter must be empty or an array.'); |
|
48 | } |
||
49 | |||
50 | 11 | foreach ($dataStorage as $rowData) { |
|
51 | 3 | if (!is_array($rowData)) { |
|
52 | 1 | throw new InvalidArgumentException('The constructor parameter if present must be an array of arrays.'); |
|
53 | } |
||
54 | 10 | } |
|
55 | |||
56 | 10 | $this->dataStorage[$this->dataGroup] = $dataStorage; |
|
57 | 10 | } |
|
58 | |||
59 | /** |
||
60 | * Returns the Data Storage instance. |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | 9 | public function getDataStorage() |
|
68 | |||
69 | /** |
||
70 | * Set adapter data group. |
||
71 | * |
||
72 | * @param string $dataGroup |
||
73 | * |
||
74 | * @throws InitException |
||
75 | * |
||
76 | * @return InMemoryAdapter |
||
77 | */ |
||
78 | 6 | public function setDataGroup($dataGroup) |
|
93 | |||
94 | /** |
||
95 | * Set adapter ID key. For Databases this can be the Primary key. Only simple key is allowed. |
||
96 | * |
||
97 | * @param string $idKey |
||
98 | * |
||
99 | * @throws InitException |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | 6 | public function setIdKey($idKey) |
|
114 | |||
115 | /** |
||
116 | * Get exactly one "row" of data according to the expression. |
||
117 | * |
||
118 | * @param mixed $identifier |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | 2 | public function getData($identifier) |
|
134 | |||
135 | /** |
||
136 | * Get a set of data according to the expression and the chunk. |
||
137 | * |
||
138 | * @param array $expression |
||
139 | * @param int $limit |
||
140 | * @param int $offset |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 2 | public function getDataSet(array $expression, $limit = null, $offset = null) |
|
169 | |||
170 | /** |
||
171 | * Checks the data (row) array against the expressions. |
||
172 | * |
||
173 | * @param array $expression |
||
174 | * @param array $data |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | 2 | private function isExpressionMatch(array $expression, array $data) |
|
202 | |||
203 | /** |
||
204 | * @param mixed $data |
||
205 | * @param mixed $subject |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | 1 | private function checkWildcardMatch($data, $subject) |
|
214 | |||
215 | /** |
||
216 | * @param mixed $data |
||
217 | * @param mixed $subject |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 1 | private function checkInArrayMatch($data, $subject) |
|
225 | |||
226 | /** |
||
227 | * @param string $relation |
||
228 | * @param mixed $data |
||
229 | * @param mixed $subject |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 1 | private function checkRelation($relation, $data, $subject) |
|
246 | |||
247 | /** |
||
248 | * Gets expression type and also sets the expression subject. |
||
249 | * |
||
250 | * @param string $pattern |
||
251 | * @param string $subject |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 1 | private function getExpressionType($pattern, &$subject) |
|
278 | |||
279 | |||
280 | |||
281 | /** |
||
282 | * Get the number of matched data in the set according to the expression. |
||
283 | * |
||
284 | * @param array $expression |
||
285 | * |
||
286 | * @return int |
||
287 | */ |
||
288 | 1 | public function getDataCardinality(array $expression) |
|
294 | |||
295 | /** |
||
296 | * Insert or update entity in the storage. |
||
297 | * |
||
298 | * @param mixed $identifier |
||
299 | * @param array $data |
||
300 | * |
||
301 | * @return mixed The ID of the saved entity in the storage |
||
302 | */ |
||
303 | 5 | public function saveData($identifier, array $data) |
|
327 | |||
328 | /** |
||
329 | * Removes an entity from the storage. |
||
330 | * |
||
331 | * @param mixed $identifier |
||
332 | * |
||
333 | * @return bool |
||
334 | */ |
||
335 | 1 | public function deleteData($identifier) |
|
346 | } |
||
347 |