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 = self::DATA_SET_RECORD_LIMIT, $offset = 0) |
|
173 | |||
174 | /** |
||
175 | * Checks the data (row) array against the expressions. |
||
176 | * |
||
177 | * @param array $expression |
||
178 | * @param array $data |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | 2 | private function isExpressionMatch(array $expression, array $data) |
|
196 | |||
197 | /** |
||
198 | * Matches data against the subject according to the expression type. |
||
199 | * |
||
200 | * @param string $expressionType |
||
201 | * @param mixed $data |
||
202 | * @param mixed $subject |
||
203 | * |
||
204 | * @todo handle 'NOT IN' and 'NOT LIKE' expressions too. |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | 1 | private function match($expressionType, $data, $subject) |
|
220 | |||
221 | /** |
||
222 | * @param mixed $data |
||
223 | * @param mixed $subject |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 1 | private function checkWildcardMatch($data, $subject) |
|
232 | |||
233 | /** |
||
234 | * @param mixed $data |
||
235 | * @param mixed $subject |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | 1 | private function checkInArrayMatch($data, $subject) |
|
243 | |||
244 | /** |
||
245 | * @param string $relation |
||
246 | * @param mixed $data |
||
247 | * @param mixed $subject |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | 1 | private function checkRelation($relation, $data, $subject) |
|
264 | |||
265 | /** |
||
266 | * Gets expression type and also sets the expression subject. |
||
267 | * |
||
268 | * @param string $pattern |
||
269 | * @param string $subject |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | 1 | private function getExpressionType($pattern, &$subject) |
|
296 | |||
297 | |||
298 | |||
299 | /** |
||
300 | * Get the number of matched data in the set according to the expression. |
||
301 | * |
||
302 | * @param array $expression |
||
303 | * |
||
304 | * @return int |
||
305 | */ |
||
306 | 1 | public function getDataCardinality(array $expression) |
|
312 | |||
313 | /** |
||
314 | * Insert or update entity in the storage. |
||
315 | * |
||
316 | * @param mixed $identifier |
||
317 | * @param array $data |
||
318 | * |
||
319 | * @return mixed The ID of the saved entity in the storage |
||
320 | */ |
||
321 | 5 | public function saveData($identifier, array $data) |
|
345 | |||
346 | /** |
||
347 | * Removes an entity from the storage. |
||
348 | * |
||
349 | * @param mixed $identifier |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | 1 | public function deleteData($identifier) |
|
364 | } |
||
365 |