1 | <?php |
||
30 | 1 | class ArraySource extends \Nette\Object implements IDataSource |
|
31 | { |
||
32 | /** @var array */ |
||
33 | protected $data; |
||
34 | |||
35 | /** |
||
36 | * @param array $data |
||
37 | */ |
||
38 | public function __construct(array $data) |
||
42 | |||
43 | /** |
||
44 | * This method needs tests! |
||
45 | * @param Condition $condition |
||
46 | * @param array $data |
||
47 | * @return array |
||
48 | */ |
||
49 | protected function makeWhere(Condition $condition, array $data = NULL) |
||
50 | { |
||
51 | $data = $data === NULL |
||
52 | 1 | ? $this->data |
|
53 | 1 | : $data; |
|
54 | |||
55 | 1 | return array_filter($data, function ($row) use ($condition) { |
|
56 | 1 | if ($condition->callback) { |
|
57 | return call_user_func_array($condition->callback, [$condition->value, $row]); |
||
58 | } |
||
59 | |||
60 | 1 | $i = 0; |
|
61 | 1 | $results = []; |
|
62 | 1 | foreach ($condition->column as $column) { |
|
63 | 1 | if (Condition::isOperator($column)) { |
|
64 | 1 | $results[] = " $column "; |
|
65 | |||
66 | 1 | } else { |
|
67 | 1 | $i = count($condition->condition) > 1 ? $i : 0; |
|
68 | 1 | $results[] = (int) $this->compare( |
|
69 | 1 | $row[$column], |
|
70 | 1 | $condition->condition[$i], |
|
71 | 1 | isset($condition->value[$i]) ? $condition->value[$i] : NULL |
|
72 | 1 | ); |
|
73 | |||
74 | 1 | $i++; |
|
75 | } |
||
76 | 1 | } |
|
77 | |||
78 | 1 | $result = implode('', $results); |
|
79 | 1 | return count($condition->column) === 1 |
|
80 | 1 | ? (bool) $result |
|
81 | 1 | : eval("return $result;"); // QUESTION: How to remove this eval? hmmm? |
|
82 | 1 | }); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param string $actual |
||
87 | * @param string $condition |
||
88 | * @param mixed $expected |
||
89 | * @throws Exception |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function compare($actual, $condition, $expected) |
||
133 | |||
134 | /*********************************** interface IDataSource ************************************/ |
||
135 | |||
136 | /** |
||
137 | * @return int |
||
138 | */ |
||
139 | public function getCount() |
||
143 | |||
144 | /** |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getData() |
||
151 | |||
152 | /** |
||
153 | * @param array $conditions |
||
154 | */ |
||
155 | public function filter(array $conditions) |
||
161 | |||
162 | /** |
||
163 | * @param int $offset |
||
164 | * @param int $limit |
||
165 | */ |
||
166 | public function limit($offset, $limit) |
||
170 | |||
171 | /** |
||
172 | * @param array $sorting |
||
173 | * @throws Exception |
||
174 | */ |
||
175 | public function sort(array $sorting) |
||
202 | |||
203 | /** |
||
204 | * @param mixed $column |
||
205 | * @param array $conditions |
||
206 | * @param int $limit |
||
207 | * @return array |
||
208 | * @throws Exception |
||
209 | */ |
||
210 | public function suggest($column, array $conditions, $limit) |
||
236 | } |
||
237 |