These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Metaways Infosystems GmbH, 2011 |
||
6 | * @copyright Aimeos (aimeos.org), 2015-2017 |
||
7 | * @package MShop |
||
8 | * @subpackage Common |
||
9 | */ |
||
10 | |||
11 | |||
12 | namespace Aimeos\MShop\Common\Item\Lists; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Default implementation of the list item. |
||
17 | * |
||
18 | * @package MShop |
||
19 | * @subpackage Common |
||
20 | */ |
||
21 | class Standard |
||
22 | extends \Aimeos\MShop\Common\Item\Base |
||
23 | implements \Aimeos\MShop\Common\Item\Lists\Iface |
||
24 | { |
||
25 | private $prefix; |
||
26 | private $values; |
||
27 | private $refItem; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Initializes the list item object. |
||
32 | * |
||
33 | * @param string $prefix Property prefix when converting to array |
||
34 | * @param array $values Associative list of key/value pairs of the list item |
||
35 | */ |
||
36 | public function __construct( $prefix, array $values = [] ) |
||
37 | { |
||
38 | parent::__construct( $prefix, $values ); |
||
39 | |||
40 | $this->prefix = (string) $prefix; |
||
41 | $this->values = $values; |
||
42 | } |
||
43 | |||
44 | |||
45 | |||
46 | |||
47 | /** |
||
48 | * Returns the parent ID of the common list item, |
||
49 | * like the unique ID of a product or a tree node. |
||
50 | * |
||
51 | * @return integer|null Parent ID of the common list item |
||
52 | */ |
||
53 | public function getParentId() |
||
54 | { |
||
55 | if( isset( $this->values[$this->prefix . 'parentid'] ) ) { |
||
56 | return (int) $this->values[$this->prefix . 'parentid']; |
||
57 | } |
||
58 | |||
59 | return null; |
||
60 | } |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Sets the parent ID of the common list item, |
||
65 | * like the unique ID of a product or a tree node. |
||
66 | * |
||
67 | * @param string $parentid New parent ID of the common list item |
||
68 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
69 | */ |
||
70 | public function setParentId( $parentid ) |
||
71 | { |
||
72 | if( (string) $parentid !== $this->getParentId() ) |
||
73 | { |
||
74 | $this->values[$this->prefix . 'parentid'] = (int) $parentid; |
||
75 | $this->setModified(); |
||
76 | } |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Returns the domain of the common list item, e.g. text or media. |
||
84 | * |
||
85 | * @return string Domain of the common list item |
||
86 | */ |
||
87 | public function getDomain() |
||
88 | { |
||
89 | if( isset( $this->values[$this->prefix . 'domain'] ) ) { |
||
90 | return (string) $this->values[$this->prefix . 'domain']; |
||
91 | } |
||
92 | |||
93 | return ''; |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Sets the new domain of the common list item, e.g. text od media. |
||
99 | * |
||
100 | * @param string $domain New domain of the common list item |
||
101 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
102 | */ |
||
103 | public function setDomain( $domain ) |
||
104 | { |
||
105 | if( (string) $domain !== $this->getDomain() ) |
||
106 | { |
||
107 | $this->values[$this->prefix . 'domain'] = (string) $domain; |
||
108 | $this->setModified(); |
||
109 | } |
||
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Returns the reference id of the common list item, like the unique id |
||
117 | * of a text item or a media item. |
||
118 | * |
||
119 | * @return string Reference id of the common list item |
||
120 | */ |
||
121 | public function getRefId() |
||
122 | { |
||
123 | if( isset( $this->values[$this->prefix . 'refid'] ) ) { |
||
124 | return (string) $this->values[$this->prefix . 'refid']; |
||
125 | } |
||
126 | |||
127 | return ''; |
||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Sets the new reference id of the common list item, like the unique id |
||
133 | * of a text item or a media item. |
||
134 | * |
||
135 | * @param string $refid New reference id of the common list item |
||
136 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
137 | */ |
||
138 | public function setRefId( $refid ) |
||
139 | { |
||
140 | if( (string) $refid !== $this->getRefId() ) |
||
141 | { |
||
142 | $this->values[$this->prefix . 'refid'] = (string) $refid; |
||
143 | $this->setModified(); |
||
144 | } |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Returns the start date of the common list item (YYYY-MM-DD hh:mm:ss). |
||
152 | * |
||
153 | * @return string|null Start date of the common list item (YYYY-MM-DD hh:mm:ss) |
||
154 | */ |
||
155 | public function getDateStart() |
||
156 | { |
||
157 | if( isset( $this->values[$this->prefix . 'datestart'] ) ) { |
||
158 | return (string) $this->values[$this->prefix . 'datestart']; |
||
159 | } |
||
160 | |||
161 | return null; |
||
162 | } |
||
163 | |||
164 | |||
165 | /** |
||
166 | * Sets the new start date of the common list item (YYYY-MM-DD hh:mm:ss). |
||
167 | * |
||
168 | * @param string $date New start date of the common list item (YYYY-MM-DD hh:mm:ss) |
||
169 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
170 | */ |
||
171 | public function setDateStart( $date ) |
||
172 | { |
||
173 | if( (string) $date !== $this->getDateStart() ) |
||
174 | { |
||
175 | $this->values[$this->prefix . 'datestart'] = $this->checkDateFormat( $date ); |
||
176 | $this->setModified(); |
||
177 | } |
||
178 | |||
179 | return $this; |
||
180 | } |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Returns the end date of the common list item (YYYY-MM-DD hh:mm:ss). |
||
185 | * |
||
186 | * @return string|null End date of the common list item (YYYY-MM-DD hh:mm:ss) |
||
187 | */ |
||
188 | public function getDateEnd() |
||
189 | { |
||
190 | if( isset( $this->values[$this->prefix . 'dateend'] ) ) { |
||
191 | return (string) $this->values[$this->prefix . 'dateend']; |
||
192 | } |
||
193 | |||
194 | return null; |
||
195 | } |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Sets the new end date of the common list item (YYYY-MM-DD hh:mm:ss). |
||
200 | * |
||
201 | * @param string $date New end date of the common list item (YYYY-MM-DD hh:mm:ss) |
||
202 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
203 | */ |
||
204 | public function setDateEnd( $date ) |
||
205 | { |
||
206 | if( (string) $date !== $this->getDateEnd() ) |
||
207 | { |
||
208 | $this->values[$this->prefix . 'dateend'] = $this->checkDateFormat( $date ); |
||
209 | $this->setModified(); |
||
210 | } |
||
211 | |||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | |||
216 | /** |
||
217 | * Returns the type of the list item. |
||
218 | * |
||
219 | * @return string|null Type of the list item |
||
220 | */ |
||
221 | public function getType() |
||
222 | { |
||
223 | if( isset( $this->values[$this->prefix . 'type'] ) ) { |
||
224 | return (string) $this->values[$this->prefix . 'type']; |
||
225 | } |
||
226 | |||
227 | return null; |
||
228 | } |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Returns the localized name of the type |
||
233 | * |
||
234 | * @return string|null Localized name of the type |
||
235 | */ |
||
236 | public function getTypeName() |
||
237 | { |
||
238 | if( isset( $this->values[$this->prefix . 'typename'] ) ) { |
||
239 | return (string) $this->values[$this->prefix . 'typename']; |
||
240 | } |
||
241 | |||
242 | return null; |
||
243 | } |
||
244 | |||
245 | |||
246 | /** |
||
247 | * Returns the type id of the list item. |
||
248 | * |
||
249 | * @return integer|null Type id of the list item |
||
250 | */ |
||
251 | public function getTypeId() |
||
252 | { |
||
253 | if( isset( $this->values[$this->prefix . 'typeid'] ) ) { |
||
254 | return (int) $this->values[$this->prefix . 'typeid']; |
||
255 | } |
||
256 | |||
257 | return null; |
||
258 | } |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Sets the new type id of the list item. |
||
263 | * |
||
264 | * @param string $typeid type id of the list item |
||
265 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
266 | */ |
||
267 | public function setTypeId( $typeid ) |
||
268 | { |
||
269 | if( (string) $typeid != $this->getTypeId() ) |
||
270 | { |
||
271 | $this->values[$this->prefix . 'typeid'] = (string) $typeid; |
||
272 | $this->setModified(); |
||
273 | } |
||
274 | |||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Returns the position of the list item. |
||
281 | * |
||
282 | * @return integer Position of the list item |
||
283 | */ |
||
284 | public function getPosition() |
||
285 | { |
||
286 | if( isset( $this->values[$this->prefix . 'position'] ) ) { |
||
287 | return (int) $this->values[$this->prefix . 'position']; |
||
288 | } |
||
289 | |||
290 | return 0; |
||
291 | } |
||
292 | |||
293 | |||
294 | /** |
||
295 | * Sets the new position of the list item. |
||
296 | * |
||
297 | * @param integer $pos position of the list item |
||
298 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
299 | */ |
||
300 | public function setPosition( $pos ) |
||
301 | { |
||
302 | if( (int) $pos !== $this->getPosition() ) |
||
303 | { |
||
304 | $this->values[$this->prefix . 'position'] = (int) $pos; |
||
305 | $this->setModified(); |
||
306 | } |
||
307 | |||
308 | return $this; |
||
309 | } |
||
310 | |||
311 | |||
312 | /** |
||
313 | * Returns the status of the list item. |
||
314 | * |
||
315 | * @return integer Status of the item |
||
316 | */ |
||
317 | public function getStatus() |
||
318 | { |
||
319 | if( isset( $this->values[$this->prefix . 'status'] ) ) { |
||
320 | return (int) $this->values[$this->prefix . 'status']; |
||
321 | } |
||
322 | |||
323 | return 1; |
||
324 | } |
||
325 | |||
326 | |||
327 | /** |
||
328 | * Sets the new status of the list item. |
||
329 | * |
||
330 | * @param integer $status Status of the item |
||
331 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
332 | */ |
||
333 | public function setStatus( $status ) |
||
334 | { |
||
335 | if( (int) $status !== $this->getStatus() ) |
||
336 | { |
||
337 | $this->values[$this->prefix . 'status'] = (int) $status; |
||
338 | $this->setModified(); |
||
339 | } |
||
340 | |||
341 | return $this; |
||
342 | } |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Returns the configuration of the list item. |
||
347 | * |
||
348 | * @return array Custom configuration values |
||
349 | */ |
||
350 | public function getConfig() |
||
351 | { |
||
352 | if( isset( $this->values[$this->prefix . 'config'] ) ) { |
||
353 | return (array) $this->values[$this->prefix . 'config']; |
||
354 | } |
||
355 | |||
356 | return []; |
||
357 | } |
||
358 | |||
359 | |||
360 | /** |
||
361 | * Sets the new configuration for the list item. |
||
362 | * |
||
363 | * @param array $config Custom configuration values |
||
364 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
365 | */ |
||
366 | public function setConfig( array $config ) |
||
367 | { |
||
368 | $this->values[$this->prefix . 'config'] = $config; |
||
369 | $this->setModified(); |
||
370 | |||
371 | return $this; |
||
372 | } |
||
373 | |||
374 | |||
375 | /** |
||
376 | * Returns the referenced item if it's available. |
||
377 | * |
||
378 | * @return \Aimeos\MShop\Common\Item\Iface Referenced list item |
||
379 | */ |
||
380 | public function getRefItem() |
||
381 | { |
||
382 | return $this->refItem; |
||
383 | } |
||
384 | |||
385 | |||
386 | /** |
||
387 | * Stores the item referenced by the list item. |
||
388 | * |
||
389 | * @param \Aimeos\MShop\Common\Item\Iface $refItem Item referenced by the list item |
||
390 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
391 | */ |
||
392 | public function setRefItem( \Aimeos\MShop\Common\Item\Iface $refItem ) |
||
393 | { |
||
394 | $this->refItem = $refItem; |
||
395 | |||
396 | return $this; |
||
397 | } |
||
398 | |||
399 | |||
400 | /** |
||
401 | * Returns the item type |
||
402 | * |
||
403 | * @return string Item type, subtypes are separated by slashes |
||
404 | */ |
||
405 | public function getResourceType() |
||
406 | { |
||
407 | return str_replace( '.', '/', rtrim( $this->prefix, '.' ) ); |
||
408 | } |
||
409 | |||
410 | |||
411 | /** |
||
412 | * Tests if the item is available based on status, time, language and currency |
||
413 | * |
||
414 | * @return boolean True if available, false if not |
||
415 | */ |
||
416 | public function isAvailable() |
||
417 | { |
||
418 | return (bool) $this->getStatus() |
||
419 | && ( $this->getDateStart() === null || $this->getDateStart() < $this->values['date'] ) |
||
420 | && ( $this->getDateEnd() === null || $this->getDateEnd() > $this->values['date'] ); |
||
421 | } |
||
422 | |||
423 | |||
424 | /** |
||
425 | * Sets the item values from the given array. |
||
426 | * |
||
427 | * @param array $list Associative list of item keys and their values |
||
428 | * @return array Associative list of keys and their values that are unknown |
||
429 | */ |
||
430 | public function fromArray( array $list ) |
||
431 | { |
||
432 | $unknown = []; |
||
433 | $list = parent::fromArray( $list ); |
||
434 | |||
435 | foreach( $list as $key => $value ) |
||
436 | { |
||
437 | switch( $key ) |
||
438 | { |
||
439 | case $this->prefix . 'parentid': $this->setParentId( $value ); break; |
||
440 | case $this->prefix . 'typeid': $this->setTypeId( $value ); break; |
||
441 | case $this->prefix . 'domain': $this->setDomain( $value ); break; |
||
442 | case $this->prefix . 'refid': $this->setRefId( $value ); break; |
||
443 | case $this->prefix . 'datestart': $this->setDateStart( $value ); break; |
||
444 | case $this->prefix . 'dateend': $this->setDateEnd( $value ); break; |
||
445 | case $this->prefix . 'config': $this->setConfig( $value ); break; |
||
446 | case $this->prefix . 'position': $this->setPosition( $value ); break; |
||
447 | case $this->prefix . 'status': $this->setStatus( $value ); break; |
||
448 | default: $unknown[$key] = $value; |
||
0 ignored issues
–
show
|
|||
449 | } |
||
450 | } |
||
451 | |||
452 | return $unknown; |
||
453 | } |
||
454 | |||
455 | |||
456 | /** |
||
457 | * Returns the item values as array. |
||
458 | * |
||
459 | * @param boolean True to return private properties, false for public only |
||
460 | * @return array Associative list of item properties and their values |
||
461 | */ |
||
462 | public function toArray( $private = false ) |
||
463 | { |
||
464 | $list = parent::toArray( $private ); |
||
465 | |||
466 | $list[$this->prefix . 'domain'] = $this->getDomain(); |
||
467 | $list[$this->prefix . 'refid'] = $this->getRefId(); |
||
468 | $list[$this->prefix . 'datestart'] = $this->getDateStart(); |
||
469 | $list[$this->prefix . 'dateend'] = $this->getDateEnd(); |
||
470 | $list[$this->prefix . 'config'] = $this->getConfig(); |
||
471 | $list[$this->prefix . 'position'] = $this->getPosition(); |
||
472 | $list[$this->prefix . 'status'] = $this->getStatus(); |
||
473 | $list[$this->prefix . 'typename'] = $this->getTypeName(); |
||
474 | $list[$this->prefix . 'type'] = $this->getType(); |
||
475 | |||
476 | if( $private === true ) |
||
477 | { |
||
478 | $list[$this->prefix . 'parentid'] = $this->getParentId(); |
||
479 | $list[$this->prefix . 'typeid'] = $this->getTypeId(); |
||
480 | } |
||
481 | |||
482 | return $list; |
||
483 | } |
||
484 | |||
485 | } |
||
486 |
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.