1 | <?php declare(strict_types=1); |
||
7 | class Linq |
||
8 | { |
||
9 | private $collection; |
||
10 | |||
11 | private const CONDITIONS_TYPES = [ |
||
12 | '=', |
||
13 | '!=', |
||
14 | '>', |
||
15 | '<', |
||
16 | '<=', |
||
17 | '>=', |
||
18 | ]; |
||
19 | |||
20 | 4 | public function __construct(Collection $collection) |
|
24 | |||
25 | 2 | public function where(string $query): self |
|
26 | { |
||
27 | 2 | $pattern = '#(?P<field>.*)?(?P<condition>' . implode('|', self::CONDITIONS_TYPES) . ')(?P<value>.*)#is'; |
|
28 | |||
29 | 2 | preg_match($pattern, $query, $matches); |
|
30 | |||
31 | 2 | $count = count($matches); |
|
32 | |||
33 | 2 | if (!$count || $count < 4) { |
|
34 | 2 | throw new InvalidArgumentException('Not matches ' . '<pre>' . print_r($matches, true) . '</pre>'); |
|
35 | } |
||
36 | 2 | $args = array_map('trim', $matches); |
|
37 | |||
38 | $array = [ |
||
39 | 2 | '=' => '===', |
|
40 | '!=' => '!==', |
||
41 | ]; |
||
42 | |||
43 | 2 | return $this->prepare($args['field'], |
|
44 | 2 | (in_array($args['condition'], $array, true) ? $array[$args['condition']] : $args['condition']), |
|
45 | 2 | $args['value']); |
|
46 | } |
||
47 | |||
48 | 2 | private function condition($valFirst, string $condition, $valSecond): bool |
|
67 | |||
68 | 2 | private function prepare(string $field, string $condition, $value): self |
|
80 | |||
81 | |||
82 | 3 | public function get(): array |
|
86 | } |
||
87 |