Completed
Push — master ( 7d17f2...366ccc )
by Aimeos
08:56
created

Standard::toArray()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 1
rs 8.9713
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-2016
7
 * @package MShop
8
 * @subpackage Catalog
9
 */
10
11
12
namespace Aimeos\MShop\Catalog\Item;
13
14
15
/**
16
 * Generic interface for catalog items.
17
 *
18
 * @package MShop
19
 * @subpackage Catalog
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\ListRef\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Catalog\Item\Iface
24
{
25
	private $node;
26
	private $children;
27
28
29
	/**
30
	 * Initializes the catalog item.
31
	 *
32
	 * @param \Aimeos\MW\Tree\Node\Iface $node Tree node
33
	 * @param \Aimeos\MShop\Catalog\Item\Iface[] $children List of children of the item
34
	 * @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items
35
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
36
	 */
37
	public function __construct( \Aimeos\MW\Tree\Node\Iface $node, array $children = [],
38
		array $listItems = [], array $refItems = [] )
39
	{
40
		parent::__construct( '', [], $listItems, $refItems );
41
42
		\Aimeos\MW\Common\Base::checkClassList( '\\Aimeos\\MShop\\Catalog\\Item\\Iface', $children );
43
44
		$this->children = $children;
45
		$this->node = $node;
46
	}
47
48
49
	/**
50
	 * Clones internal objects of the catalog item.
51
	 */
52
	public function __clone()
53
	{
54
		$this->node = clone $this->node;
55
	}
56
57
58
	/**
59
	 * Returns the unique ID of the node.
60
	 *
61
	 * @return string|null Unique ID of the node
62
	 */
63
	public function getId()
64
	{
65
		return $this->node->getId();
66
	}
67
68
69
	/**
70
	 * Sets the unique ID of the node.
71
	 *
72
	 * @param string|null Unique ID of the node
73
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
74
	 */
75
	public function setId( $id )
76
	{
77
		if( $id === $this->getId() ) { return $this; }
78
79
		$this->node->setId( $id );
80
81
		return $this;
82
	}
83
84
85
	/**
86
	 * Returns the site ID of the item.
87
	 *
88
	 * @return integer|null Site ID of the item
89
	 */
90
	public function getSiteId()
91
	{
92
		return ( $this->node->__isset( 'siteid' ) ? $this->node->__get( 'siteid' ) : null );
93
	}
94
95
96
	/**
97
	 * Returns the ID of the parent category
98
	 *
99
	 * @return string|null Unique ID of the parent category
100
	 */
101
	public function getParentId()
102
	{
103
		return ( $this->node->__isset( 'parentid' ) ? $this->node->__get( 'parentid' ) : null );
104
	}
105
106
107
	/**
108
	 * Returns the level of the item in the tree
109
	 *
110
	 * @return integer Level of the item starting with "0" for the root node
111
	 */
112
	public function getLevel()
113
	{
114
		return ( $this->node->__isset( 'level' ) ? $this->node->__get( 'level' ) : 0 );
115
	}
116
117
118
	/**
119
	 * Returns the internal name of the item.
120
	 *
121
	 * @return string Name of the item
122
	 */
123
	public function getLabel()
124
	{
125
		return $this->node->getLabel();
126
	}
127
128
129
	/**
130
	 * Sets the new internal name of the item.
131
	 *
132
	 * @param string $name New name of the item
133
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
134
	 */
135
	public function setLabel( $name )
136
	{
137
		if( $name == $this->getLabel() ) { return $this; }
138
139
		$this->node->setLabel( $name );
140
141
		return $this;
142
	}
143
144
145
	/**
146
	 * Returns the config property of the catalog.
147
	 *
148
	 * @return array Returns the config of the catalog node
149
	 */
150
	public function getConfig()
151
	{
152
		return $this->node->__isset( 'config' ) && is_array( $this->node->config ) ? $this->node->__get( 'config' ) : [];
153
	}
154
155
156
	/**
157
	 * Sets the config property of the catalog item.
158
	 *
159
	 * @param array $options Options to be set for the catalog node
160
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
161
	 */
162
	public function setConfig( array $options )
163
	{
164
		$this->node->__set( 'config', $options );
165
166
		return $this;
167
	}
168
169
170
	/**
171
	 * Returns the code of the item.
172
	 *
173
	 * @return string Code of the item
174
	 */
175
	public function getCode()
176
	{
177
		return $this->node->getCode();
178
	}
179
180
181
	/**
182
	 * Sets the new code of the item.
183
	 *
184
	 * @param string $code New code of the item
185
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
186
	 */
187
	public function setCode( $code )
188
	{
189
		if( $code == $this->getCode() ) { return $this; }
190
191
		$this->node->setCode( $this->checkCode( $code ) );
192
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Returns the status of the item.
199
	 *
200
	 * @return integer Greater than zero if enabled, zero or negative values if disabled
201
	 */
202
	public function getStatus()
203
	{
204
		return $this->node->getStatus();
205
	}
206
207
	/**
208
	 * Sets the new status of the item.
209
	 *
210
	 * @param integer $status True if enabled, false if not
211
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
212
	 */
213
	public function setStatus( $status )
214
	{
215
		if( $status === $this->getStatus() ) { return $this; }
216
217
		$this->node->setStatus( $status );
218
219
		return $this;
220
	}
221
222
	/**
223
	 * Returns the URL target specific for that category
224
	 *
225
	 * @return string URL target specific for that category
226
	 */
227
	public function getTarget()
228
	{
229
		return ( $this->node->__isset( 'target' ) ? $this->node->__get( 'target' ) : '' );
230
	}
231
232
	/**
233
	 * Sets a new URL target specific for that category
234
	 *
235
	 * @param string $value New URL target specific for that category
236
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
0 ignored issues
show
Documentation introduced by
Should the return type not be Standard?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
237
	 */
238
	public function setTarget( $value )
239
	{
240
		if( $value === $this->getTarget() ) { return $this; }
241
242
		$this->node->__set( 'target', $value );
243
244
		return $this;
245
	}
246
247
	/**
248
	 * Returns modification time of the order item base product.
249
	 *
250
	 * @return string Returns modification time of the order base item
251
	 */
252
	public function getTimeModified()
253
	{
254
		return ( $this->node->__isset( 'mtime' ) ? $this->node->__get( 'mtime' ) : null );
255
	}
256
257
258
	/**
259
	 * Returns the create date of the item.
260
	 *
261
	 * @return string ISO date in YYYY-MM-DD hh:mm:ss format
262
	 */
263
	public function getTimeCreated()
264
	{
265
		return ( $this->node->__isset( 'ctime' ) ? $this->node->__get( 'ctime' ) : null );
266
	}
267
268
269
	/**
270
	 * Returns the editor code of editor who created/modified the item at last.
271
	 *
272
	 * @return string Editor who created/modified the item at last
273
	 */
274
	public function getEditor()
275
	{
276
		return ( $this->node->__isset( 'editor' ) ? $this->node->__get( 'editor' ) : null );
277
	}
278
279
280
	/**
281
	 * Sets the item values from the given array.
282
	 *
283
	 * @param array $list Associative list of item keys and their values
284
	 * @return array Associative list of keys and their values that are unknown
285
	 */
286
	public function fromArray( array $list )
287
	{
288
		$unknown = [];
289
		$list = parent::fromArray( $list );
290
291
		foreach( $list as $key => $value )
292
		{
293
			switch( $key )
294
			{
295
				case 'catalog.id': $this->node->setId( $value ); break;
296
				case 'catalog.code': $this->node->setCode( $value ); break;
297
				case 'catalog.label': $this->node->setLabel( $value ); break;
298
				case 'catalog.status': $this->node->setStatus( $value ); break;
299
				case 'catalog.config': $this->setConfig( $value ); break;
300
				case 'catalog.target': $this->setTarget( $value ); break;
301
				default: $unknown[$key] = $value;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
302
			}
303
		}
304
305
		return $unknown;
306
	}
307
308
309
	/**
310
	 * Returns the public values of the node as array.
311
	 *
312
	 * @param boolean True to return private properties, false for public only
313
	 * @return array Assciative list of key/value pairs
314
	 */
315
	public function toArray( $private = false )
316
	{
317
		$list = array(
318
			'catalog.id' => $this->getId(),
319
			'catalog.code' => $this->getCode(),
320
			'catalog.label' => $this->getLabel(),
321
			'catalog.status' => $this->getStatus(),
322
			'catalog.config' => $this->getConfig(),
323
			'catalog.target' => $this->getTarget(),
324
			'catalog.hasChildren' => $this->hasChildren()
325
		);
326
327
		if( $private === true )
328
		{
329
			$list['catalog.level'] = $this->getLevel();
330
			$list['catalog.parentid'] = $this->getParentId();
331
			$list['catalog.siteid'] = $this->getSiteId();
332
			$list['catalog.ctime'] = $this->getTimeCreated();
333
			$list['catalog.mtime'] = $this->getTimeModified();
334
			$list['catalog.editor'] = $this->getEditor();
335
		}
336
337
		return $list;
338
	}
339
340
341
	/**
342
	 * Checks, whether this node was modified.
343
	 *
344
	 * @return boolean True if the content of the node is modified, false if not
345
	 */
346
	public function isModified()
347
	{
348
		return $this->node->isModified();
349
	}
350
351
	/**
352
	 * Returns a child of this node identified by its index.
353
	 *
354
	 * @param integer $index Index of child node
355
	 * @return \Aimeos\MShop\Catalog\Item\Iface Selected node
356
	 */
357
	public function getChild( $index )
358
	{
359
		if( isset( $this->children[$index] ) ) {
360
			return $this->children[$index];
361
		}
362
363
		throw new \Aimeos\MShop\Catalog\Exception( sprintf( 'Child node with index "%1$d" not available', $index ) );
364
	}
365
366
	/**
367
	 * Returns all children of this node.
368
	 *
369
	 * @return array Numerically indexed list of nodes
370
	 */
371
	public function getChildren()
372
	{
373
		return $this->children;
374
	}
375
376
	/**
377
	 * Tests if a node has children.
378
	 *
379
	 * @return boolean True if node has children, false if not
380
	 */
381
	public function hasChildren()
382
	{
383
		if( count( $this->children ) > 0 ) {
384
			return true;
385
		}
386
387
		return $this->node->hasChildren();
388
	}
389
390
	/**
391
	 * Adds a child node to this node.
392
	 *
393
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Child node to add
394
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
395
	 */
396
	public function addChild( \Aimeos\MShop\Catalog\Item\Iface $item )
397
	{
398
		// don't set the modified flag as it's only for the values
399
		$this->children[] = $item;
400
401
		return $this;
402
	}
403
404
405
	/**
406
	 * Returns the internal node.
407
	 *
408
	 * @return \Aimeos\MW\Tree\Node\Iface Internal node object
409
	 */
410
	public function getNode()
411
	{
412
		return $this->node;
413
	}
414
415
416
	/**
417
	 * Returns the item type
418
	 *
419
	 * @return string Item type, subtypes are separated by slashes
420
	 */
421
	public function getResourceType()
422
	{
423
		return 'catalog';
424
	}
425
}
426