1 | <?php |
||
19 | class Generic |
||
20 | { |
||
21 | /** @var array Collection of all supported entity fields */ |
||
22 | protected static $parentFields = array( |
||
23 | Material::F_PRIMARY=> Material::F_PRIMARY, |
||
24 | Material::F_PRIORITY => Material::F_PRIORITY, |
||
25 | Material::F_IDENTIFIER => Material::F_IDENTIFIER, |
||
26 | Material::F_DELETION => Material::F_DELETION, |
||
27 | Material::F_PUBLISHED => Material::F_PUBLISHED, |
||
28 | Material::F_PARENT => Material::F_PARENT, |
||
29 | Material::F_CREATED => Material::F_CREATED, |
||
30 | ); |
||
31 | |||
32 | /** @var string Entity identifier */ |
||
33 | protected static $identifier; |
||
34 | |||
35 | /** @var string Entity navigation identifiers */ |
||
36 | protected static $navigationIDs = array(); |
||
37 | |||
38 | /** |
||
39 | * @var string Collection of entity field names |
||
40 | * @deprecated Created for old application who need real additional field names |
||
41 | */ |
||
42 | public static $fieldRealNames = array(); |
||
43 | |||
44 | /** @var string Collection of entity field names */ |
||
45 | public static $fieldNames = array(); |
||
46 | |||
47 | |||
48 | /** @var QueryInterface Database query instance */ |
||
49 | protected $query; |
||
50 | |||
51 | /** @var array Collection of entity fields to retrieved from database */ |
||
52 | protected $selectedFields; |
||
53 | |||
54 | /** @var Condition Query conditions */ |
||
55 | protected $conditions; |
||
56 | |||
57 | /** @var array Collection of ordering parameters */ |
||
58 | protected $orderBy = array(); |
||
59 | |||
60 | /** @var array Collection of limit parameters */ |
||
61 | protected $limit = array(); |
||
62 | |||
63 | /** @var array Collection of entity identifiers */ |
||
64 | protected $entityIDs = array(); |
||
65 | |||
66 | /** |
||
67 | * Convert date value to database format. |
||
68 | * TODO: Must implement at database layer |
||
69 | * |
||
70 | * @param string $date Date value for conversion |
||
71 | * @return string Converted date to correct format |
||
72 | */ |
||
73 | protected function convertToDateTime($date) |
||
77 | |||
78 | /** |
||
79 | * Add sorting to entity identifiers. |
||
80 | * |
||
81 | * @param array $entityIDs |
||
82 | * @param string $fieldName Additional field name for sorting |
||
83 | * @param string $order Sorting order(ASC|DESC) |
||
84 | * @return array Collection of entity identifiers ordered by additional field value |
||
85 | */ |
||
86 | protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') |
||
99 | |||
100 | /** |
||
101 | * Add condition to current query. |
||
102 | * |
||
103 | * @param string $fieldName Entity field name |
||
104 | * @param string $fieldValue Value |
||
105 | * @return $this Chaining |
||
106 | */ |
||
107 | public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) |
||
108 | { |
||
109 | $this->conditions->add($fieldName, $fieldValue, $fieldRelation); |
||
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Set field for sorting. |
||
116 | * |
||
117 | * @param string $fieldName Additional field name |
||
118 | * @param string $order Sorting order |
||
119 | * @return $this Chaining |
||
120 | */ |
||
121 | public function orderBy($fieldName, $order = 'ASC') |
||
129 | |||
130 | /** |
||
131 | * Add primary field query condition. |
||
132 | * |
||
133 | * @param string $value Field value |
||
134 | * @return $this Chaining |
||
135 | * @see Material::where() |
||
136 | */ |
||
137 | public function primary($value) |
||
141 | |||
142 | /** |
||
143 | * Add identifier field query condition. |
||
144 | * |
||
145 | * @param string $value Field value |
||
146 | * @return $this Chaining |
||
147 | * @see Material::where() |
||
148 | */ |
||
149 | public function identifier($value) |
||
153 | |||
154 | /** |
||
155 | * Add active flag condition. |
||
156 | * |
||
157 | * @param bool $value Field value |
||
158 | * @return $this Chaining |
||
159 | * @see Material::where() |
||
160 | */ |
||
161 | public function active($value) |
||
165 | |||
166 | /** |
||
167 | * Add entity published field query condition. |
||
168 | * |
||
169 | * @param string $value Field value |
||
170 | * @return $this Chaining |
||
171 | * @see Material::where() |
||
172 | */ |
||
173 | public function published($value) |
||
177 | |||
178 | /** |
||
179 | * Add entity creation field query condition. |
||
180 | * |
||
181 | * @param string $value Field value |
||
182 | * @param string $relation @see ArgumentInterface types |
||
183 | * @return $this Chaining |
||
184 | * @see Material::where() |
||
185 | */ |
||
186 | public function created($value, $relation = ArgumentInterface::EQUAL) |
||
190 | |||
191 | /** |
||
192 | * Add entity modification field query condition. |
||
193 | * |
||
194 | * @param string $value Field value |
||
195 | * @param string $relation @see ArgumentInterface types |
||
196 | * @return $this Chaining |
||
197 | * @see Material::where() |
||
198 | */ |
||
199 | public function modified($value, $relation = ArgumentInterface::EQUAL) |
||
203 | |||
204 | /** |
||
205 | * Perform SamsonCMS query and get entities collection. |
||
206 | * |
||
207 | * @return \samsoncms\api\Entity[] Collection of found entities |
||
208 | */ |
||
209 | public function find() |
||
210 | { |
||
211 | $this->query->entity(static::$identifier); |
||
212 | |||
213 | // Set entity primary keys if predefined |
||
214 | if (count($this->entityIDs)) { |
||
215 | $this->primary($this->entityIDs); |
||
216 | } |
||
217 | |||
218 | // Add query sorter for showed page |
||
219 | if (count($this->orderBy) === 2) { |
||
220 | $this->query->orderBy($this->orderBy[0], $this->orderBy[1]); |
||
221 | } |
||
222 | |||
223 | // Proxy to regular database query |
||
224 | $return = $this->query |
||
225 | ->whereCondition($this->conditions) |
||
226 | ->exec(); |
||
227 | |||
228 | // Reorder if entity identifiers collection was defined |
||
229 | return $this->sortArrayByArray($return, $this->entityIDs); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Reorder elements in one array according to keys of another. |
||
234 | * |
||
235 | * @param array $array Source array |
||
236 | * @param array $orderArray Ideal array |
||
237 | * @return array Ordered array |
||
238 | */ |
||
239 | protected function sortArrayByArray(array $array, array $orderArray) { |
||
249 | |||
250 | /** |
||
251 | * Perform SamsonCMS query and get collection of entities fields. |
||
252 | * |
||
253 | * @param string $fieldName Entity field name |
||
254 | * @return array Collection of entity fields |
||
255 | */ |
||
256 | public function fields($fieldName) |
||
264 | |||
265 | /** |
||
266 | * Perform SamsonCMS query and get first matching entity. |
||
267 | * |
||
268 | * @return \samsoncms\api\Entity First matching entity |
||
269 | */ |
||
270 | public function first() |
||
281 | |||
282 | /** |
||
283 | * Perform SamsonCMS query and get amount resulting entities. |
||
284 | * |
||
285 | * @return int Amount of resulting entities |
||
286 | */ |
||
287 | public function count() |
||
295 | |||
296 | /** |
||
297 | * Generic constructor. |
||
298 | * |
||
299 | * @param QueryInterface $query Database query instance |
||
300 | */ |
||
301 | public function __construct(QueryInterface $query) |
||
306 | } |
||
307 |