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 Attribute |
||
9 | */ |
||
10 | |||
11 | |||
12 | namespace Aimeos\MShop\Attribute\Item; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Default attribute item implementation. |
||
17 | * |
||
18 | * @package MShop |
||
19 | * @subpackage Attribute |
||
20 | */ |
||
21 | class Standard |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
22 | extends \Aimeos\MShop\Common\Item\ListRef\Base |
||
23 | implements \Aimeos\MShop\Attribute\Item\Iface |
||
24 | { |
||
25 | private $values; |
||
26 | private $propItems; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * Initializes the attribute item. |
||
31 | * |
||
32 | * @param array $values Associative array with id, domain, code, and status to initialize the item properties; Optional |
||
33 | * @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items |
||
34 | * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items |
||
35 | * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items |
||
36 | */ |
||
37 | public function __construct( array $values = [], array $listItems = [], |
||
38 | array $refItems = [], array $propItems = [] ) |
||
39 | { |
||
40 | parent::__construct( 'attribute.', $values, $listItems, $refItems ); |
||
41 | |||
42 | $this->propItems = $propItems; |
||
43 | $this->values = $values; |
||
44 | } |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Returns the property items of the attribute |
||
49 | * |
||
50 | * @param string|null $type Name of the property item type or null for all |
||
51 | * @param boolean $active True to return only active items, false to return all |
||
52 | * @return \Aimeos\MShop\Product\Item\Property\Iface[] Associative list of property IDs as keys and property items as values |
||
53 | */ |
||
54 | public function getPropertyItems( $type = null, $active = true ) |
||
55 | { |
||
56 | $list = []; |
||
57 | |||
58 | foreach( $this->propItems as $propId => $propItem ) |
||
59 | { |
||
60 | if( ( $type === null || $propItem->getType() === $type ) |
||
61 | && ( $active === false || $propItem->isAvailable() ) |
||
62 | ) { |
||
63 | $list[$propId] = $propItem; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return $list; |
||
68 | } |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Returns the domain of the attribute item. |
||
73 | * |
||
74 | * @return string Returns the domain for this item e.g. text, media, price... |
||
75 | */ |
||
76 | public function getDomain() |
||
77 | { |
||
78 | if( isset( $this->values['attribute.domain'] ) ) { |
||
79 | return (string) $this->values['attribute.domain']; |
||
80 | } |
||
81 | |||
82 | return ''; |
||
83 | } |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Set the name of the domain for this attribute item. |
||
88 | * |
||
89 | * @param string $domain Name of the domain e.g. text, media, price... |
||
90 | * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item for chaining method calls |
||
91 | */ |
||
92 | public function setDomain( $domain ) |
||
93 | { |
||
94 | if( (string) $domain !== $this->getDomain() ) |
||
95 | { |
||
96 | $this->values['attribute.domain'] = (string) $domain; |
||
97 | $this->setModified(); |
||
98 | } |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | |||
104 | /** |
||
105 | * Returns the type id of the attribute. |
||
106 | * |
||
107 | * @return string|null Type of the attribute |
||
108 | */ |
||
109 | public function getTypeId() |
||
110 | { |
||
111 | if( isset( $this->values['attribute.typeid'] ) ) { |
||
112 | return (string) $this->values['attribute.typeid']; |
||
113 | } |
||
114 | |||
115 | return null; |
||
116 | } |
||
117 | |||
118 | |||
119 | /** |
||
120 | * Sets the new type of the attribute. |
||
121 | * |
||
122 | * @param string $typeid Type of the attribute |
||
123 | * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item for chaining method calls |
||
124 | */ |
||
125 | public function setTypeId( $typeid ) |
||
126 | { |
||
127 | if( (string) $typeid !== $this->getTypeId() ) |
||
128 | { |
||
129 | $this->values['attribute.typeid'] = (string) $typeid; |
||
130 | $this->setModified(); |
||
131 | } |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | |||
137 | /** |
||
138 | * Returns the type code of the attribute item. |
||
139 | * |
||
140 | * @return string|null Type code of the attribute item |
||
141 | */ |
||
142 | public function getType() |
||
143 | { |
||
144 | if( isset( $this->values['attribute.type'] ) ) { |
||
145 | return (string) $this->values['attribute.type']; |
||
146 | } |
||
147 | |||
148 | return null; |
||
149 | } |
||
150 | |||
151 | |||
152 | /** |
||
153 | * Returns the localized name of the type |
||
154 | * |
||
155 | * @return string|null Localized name of the type |
||
156 | */ |
||
157 | public function getTypeName() |
||
158 | { |
||
159 | if( isset( $this->values['attribute.typename'] ) ) { |
||
160 | return (string) $this->values['attribute.typename']; |
||
161 | } |
||
162 | |||
163 | return null; |
||
164 | } |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Returns a unique code of the attribute item. |
||
169 | * |
||
170 | * @return string Returns the code of the attribute item |
||
171 | */ |
||
172 | public function getCode() |
||
173 | { |
||
174 | if( isset( $this->values['attribute.code'] ) ) { |
||
175 | return (string) $this->values['attribute.code']; |
||
176 | } |
||
177 | |||
178 | return ''; |
||
179 | } |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Sets a unique code for the attribute item. |
||
184 | * |
||
185 | * @param string $code Code of the attribute item |
||
186 | * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item for chaining method calls |
||
187 | */ |
||
188 | public function setCode( $code ) |
||
189 | { |
||
190 | if( strlen( $code ) > 255 ) { |
||
191 | throw new \Aimeos\MShop\Attribute\Exception( sprintf( 'Code must not be longer than 255 characters' ) ); |
||
192 | } |
||
193 | |||
194 | if( (string) $code !== $this->getCode() ) |
||
195 | { |
||
196 | $this->values['attribute.code'] = (string) $code; |
||
197 | $this->setModified(); |
||
198 | } |
||
199 | |||
200 | return $this; |
||
201 | } |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Returns the name of the attribute item. |
||
206 | * |
||
207 | * @return string Label of the attribute item |
||
208 | */ |
||
209 | public function getLabel() |
||
210 | { |
||
211 | if( isset( $this->values['attribute.label'] ) ) { |
||
212 | return (string) $this->values['attribute.label']; |
||
213 | } |
||
214 | |||
215 | return ''; |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Sets the new label of the attribute item. |
||
221 | * |
||
222 | * @param string $label Type label of the attribute item |
||
223 | * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item for chaining method calls |
||
224 | */ |
||
225 | public function setLabel( $label ) |
||
226 | { |
||
227 | if( (string) $label !== $this->getLabel() ) |
||
228 | { |
||
229 | $this->values['attribute.label'] = (string) $label; |
||
230 | $this->setModified(); |
||
231 | } |
||
232 | |||
233 | return $this; |
||
234 | } |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Returns the status (enabled/disabled) of the attribute item. |
||
239 | * |
||
240 | * @return integer Returns the status of the item |
||
241 | */ |
||
242 | public function getStatus() |
||
243 | { |
||
244 | if( isset( $this->values['attribute.status'] ) ) { |
||
245 | return (int) $this->values['attribute.status']; |
||
246 | } |
||
247 | |||
248 | return 0; |
||
249 | } |
||
250 | |||
251 | |||
252 | /** |
||
253 | * Sets the new status of the attribute item. |
||
254 | * |
||
255 | * @param integer $status Status of the item |
||
256 | * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item for chaining method calls |
||
257 | */ |
||
258 | public function setStatus( $status ) |
||
259 | { |
||
260 | if( (int) $status !== $this->getStatus() ) |
||
261 | { |
||
262 | $this->values['attribute.status'] = (int) $status; |
||
263 | $this->setModified(); |
||
264 | } |
||
265 | |||
266 | return $this; |
||
267 | } |
||
268 | |||
269 | |||
270 | /** |
||
271 | * Gets the position of the attribute item. |
||
272 | * |
||
273 | * @return integer Position of the attribute item |
||
274 | */ |
||
275 | public function getPosition() |
||
276 | { |
||
277 | if( isset( $this->values['attribute.position'] ) ) { |
||
278 | return (int) $this->values['attribute.position']; |
||
279 | } |
||
280 | |||
281 | return 0; |
||
282 | } |
||
283 | |||
284 | |||
285 | /** |
||
286 | * Sets the position of the attribute item |
||
287 | * |
||
288 | * @param integer $pos Position of the attribute item |
||
289 | * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item for chaining method calls |
||
290 | */ |
||
291 | public function setPosition( $pos ) |
||
292 | { |
||
293 | if( (int) $pos !== $this->getPosition() ) |
||
294 | { |
||
295 | $this->values['attribute.position'] = (int) $pos; |
||
296 | $this->setModified(); |
||
297 | } |
||
298 | |||
299 | return $this; |
||
300 | } |
||
301 | |||
302 | |||
303 | /** |
||
304 | * Returns the item type |
||
305 | * |
||
306 | * @return string Item type, subtypes are separated by slashes |
||
307 | */ |
||
308 | public function getResourceType() |
||
309 | { |
||
310 | return 'attribute'; |
||
311 | } |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Tests if the item is available based on status, time, language and currency |
||
316 | * |
||
317 | * @return boolean True if available, false if not |
||
318 | */ |
||
319 | public function isAvailable() |
||
320 | { |
||
321 | return parent::isAvailable() && (bool) $this->getStatus(); |
||
322 | } |
||
323 | |||
324 | |||
325 | /** |
||
326 | * Sets the item values from the given array. |
||
327 | * |
||
328 | * @param array $list Associative list of item keys and their values |
||
329 | * @return array Associative list of keys and their values that are unknown |
||
330 | */ |
||
331 | public function fromArray( array $list ) |
||
332 | { |
||
333 | $unknown = []; |
||
334 | $list = parent::fromArray( $list ); |
||
335 | unset( $list['attribute.type'], $list['attribute.typename'] ); |
||
336 | |||
337 | foreach( $list as $key => $value ) |
||
338 | { |
||
339 | switch( $key ) |
||
340 | { |
||
341 | case 'attribute.domain': $this->setDomain( $value ); break; |
||
342 | case 'attribute.code': $this->setCode( $value ); break; |
||
343 | case 'attribute.status': $this->setStatus( $value ); break; |
||
344 | case 'attribute.typeid': $this->setTypeId( $value ); break; |
||
345 | case 'attribute.position': $this->setPosition( $value ); break; |
||
346 | case 'attribute.label': $this->setLabel( $value ); break; |
||
347 | default: $unknown[$key] = $value; |
||
348 | } |
||
349 | } |
||
350 | |||
351 | return $unknown; |
||
352 | } |
||
353 | |||
354 | |||
355 | /** |
||
356 | * Returns the item values as array. |
||
357 | * |
||
358 | * @param boolean True to return private properties, false for public only |
||
359 | * @return array Associative list of item properties and their values |
||
360 | */ |
||
361 | public function toArray( $private = false ) |
||
362 | { |
||
363 | $list = parent::toArray( $private ); |
||
364 | |||
365 | $list['attribute.domain'] = $this->getDomain(); |
||
366 | $list['attribute.code'] = $this->getCode(); |
||
367 | $list['attribute.status'] = $this->getStatus(); |
||
368 | $list['attribute.type'] = $this->getType(); |
||
369 | $list['attribute.typename'] = $this->getTypeName(); |
||
370 | $list['attribute.position'] = $this->getPosition(); |
||
371 | $list['attribute.label'] = $this->getLabel(); |
||
372 | |||
373 | if( $private === true ) { |
||
374 | $list['attribute.typeid'] = $this->getTypeId(); |
||
375 | } |
||
376 | |||
377 | return $list; |
||
378 | } |
||
379 | |||
380 | } |
||
381 |