Completed
Push — master ( c486c5...7d17f2 )
by Aimeos
10:00
created

Standard::getParentId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
rs 10
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 internal name of the item.
109
	 *
110
	 * @return string Name of the item
111
	 */
112
	public function getLabel()
113
	{
114
		return $this->node->getLabel();
115
	}
116
117
118
	/**
119
	 * Sets the new internal name of the item.
120
	 *
121
	 * @param string $name New name of the item
122
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
123
	 */
124
	public function setLabel( $name )
125
	{
126
		if( $name == $this->getLabel() ) { return $this; }
127
128
		$this->node->setLabel( $name );
129
130
		return $this;
131
	}
132
133
134
	/**
135
	 * Returns the config property of the catalog.
136
	 *
137
	 * @return array Returns the config of the catalog node
138
	 */
139
	public function getConfig()
140
	{
141
		return $this->node->__isset( 'config' ) && is_array( $this->node->config ) ? $this->node->__get( 'config' ) : [];
142
	}
143
144
145
	/**
146
	 * Sets the config property of the catalog item.
147
	 *
148
	 * @param array $options Options to be set for the catalog node
149
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
150
	 */
151
	public function setConfig( array $options )
152
	{
153
		$this->node->__set( 'config', $options );
154
155
		return $this;
156
	}
157
158
159
	/**
160
	 * Returns the code of the item.
161
	 *
162
	 * @return string Code of the item
163
	 */
164
	public function getCode()
165
	{
166
		return $this->node->getCode();
167
	}
168
169
170
	/**
171
	 * Sets the new code of the item.
172
	 *
173
	 * @param string $code New code of the item
174
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
175
	 */
176
	public function setCode( $code )
177
	{
178
		if( $code == $this->getCode() ) { return $this; }
179
180
		$this->node->setCode( $this->checkCode( $code ) );
181
182
		return $this;
183
	}
184
185
186
	/**
187
	 * Returns the status of the item.
188
	 *
189
	 * @return integer Greater than zero if enabled, zero or negative values if disabled
190
	 */
191
	public function getStatus()
192
	{
193
		return $this->node->getStatus();
194
	}
195
196
	/**
197
	 * Sets the new status of the item.
198
	 *
199
	 * @param integer $status True if enabled, false if not
200
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
201
	 */
202
	public function setStatus( $status )
203
	{
204
		if( $status === $this->getStatus() ) { return $this; }
205
206
		$this->node->setStatus( $status );
207
208
		return $this;
209
	}
210
211
	/**
212
	 * Returns the URL target specific for that category
213
	 *
214
	 * @return string URL target specific for that category
215
	 */
216
	public function getTarget()
217
	{
218
		return ( $this->node->__isset( 'target' ) ? $this->node->__get( 'target' ) : '' );
219
	}
220
221
	/**
222
	 * Sets a new URL target specific for that category
223
	 *
224
	 * @param string $value New URL target specific for that category
225
	 * @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...
226
	 */
227
	public function setTarget( $value )
228
	{
229
		if( $value === $this->getTarget() ) { return $this; }
230
231
		$this->node->__set( 'target', $value );
232
233
		return $this;
234
	}
235
236
	/**
237
	 * Returns modification time of the order item base product.
238
	 *
239
	 * @return string Returns modification time of the order base item
240
	 */
241
	public function getTimeModified()
242
	{
243
		return ( $this->node->__isset( 'mtime' ) ? $this->node->__get( 'mtime' ) : null );
244
	}
245
246
247
	/**
248
	 * Returns the create date of the item.
249
	 *
250
	 * @return string ISO date in YYYY-MM-DD hh:mm:ss format
251
	 */
252
	public function getTimeCreated()
253
	{
254
		return ( $this->node->__isset( 'ctime' ) ? $this->node->__get( 'ctime' ) : null );
255
	}
256
257
258
	/**
259
	 * Returns the editor code of editor who created/modified the item at last.
260
	 *
261
	 * @return string Editor who created/modified the item at last
262
	 */
263
	public function getEditor()
264
	{
265
		return ( $this->node->__isset( 'editor' ) ? $this->node->__get( 'editor' ) : null );
266
	}
267
268
269
	/**
270
	 * Sets the item values from the given array.
271
	 *
272
	 * @param array $list Associative list of item keys and their values
273
	 * @return array Associative list of keys and their values that are unknown
274
	 */
275
	public function fromArray( array $list )
276
	{
277
		$unknown = [];
278
		$list = parent::fromArray( $list );
279
280
		foreach( $list as $key => $value )
281
		{
282
			switch( $key )
283
			{
284
				case 'catalog.id': $this->node->setId( $value ); break;
285
				case 'catalog.code': $this->node->setCode( $value ); break;
286
				case 'catalog.label': $this->node->setLabel( $value ); break;
287
				case 'catalog.status': $this->node->setStatus( $value ); break;
288
				case 'catalog.config': $this->setConfig( $value ); break;
289
				case 'catalog.target': $this->setTarget( $value ); break;
290
				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...
291
			}
292
		}
293
294
		return $unknown;
295
	}
296
297
298
	/**
299
	 * Returns the public values of the node as array.
300
	 *
301
	 * @param boolean True to return private properties, false for public only
302
	 * @return array Assciative list of key/value pairs
303
	 */
304
	public function toArray( $private = false )
305
	{
306
		$list = array(
307
			'catalog.id' => $this->getId(),
308
			'catalog.code' => $this->getCode(),
309
			'catalog.label' => $this->getLabel(),
310
			'catalog.status' => $this->getStatus(),
311
			'catalog.config' => $this->getConfig(),
312
			'catalog.target' => $this->getTarget(),
313
			'catalog.hasChildren' => $this->hasChildren()
314
		);
315
316
		if( $private === true )
317
		{
318
			$list['catalog.parentid'] = $this->getParentId();
319
			$list['catalog.siteid'] = $this->getSiteId();
320
			$list['catalog.ctime'] = $this->getTimeCreated();
321
			$list['catalog.mtime'] = $this->getTimeModified();
322
			$list['catalog.editor'] = $this->getEditor();
323
		}
324
325
		return $list;
326
	}
327
328
329
	/**
330
	 * Checks, whether this node was modified.
331
	 *
332
	 * @return boolean True if the content of the node is modified, false if not
333
	 */
334
	public function isModified()
335
	{
336
		return $this->node->isModified();
337
	}
338
339
	/**
340
	 * Returns a child of this node identified by its index.
341
	 *
342
	 * @param integer $index Index of child node
343
	 * @return \Aimeos\MShop\Catalog\Item\Iface Selected node
344
	 */
345
	public function getChild( $index )
346
	{
347
		if( isset( $this->children[$index] ) ) {
348
			return $this->children[$index];
349
		}
350
351
		throw new \Aimeos\MShop\Catalog\Exception( sprintf( 'Child node with index "%1$d" not available', $index ) );
352
	}
353
354
	/**
355
	 * Returns all children of this node.
356
	 *
357
	 * @return array Numerically indexed list of nodes
358
	 */
359
	public function getChildren()
360
	{
361
		return $this->children;
362
	}
363
364
	/**
365
	 * Tests if a node has children.
366
	 *
367
	 * @return boolean True if node has children, false if not
368
	 */
369
	public function hasChildren()
370
	{
371
		if( count( $this->children ) > 0 ) {
372
			return true;
373
		}
374
375
		return $this->node->hasChildren();
376
	}
377
378
	/**
379
	 * Adds a child node to this node.
380
	 *
381
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Child node to add
382
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
383
	 */
384
	public function addChild( \Aimeos\MShop\Catalog\Item\Iface $item )
385
	{
386
		// don't set the modified flag as it's only for the values
387
		$this->children[] = $item;
388
389
		return $this;
390
	}
391
392
393
	/**
394
	 * Returns the internal node.
395
	 *
396
	 * @return \Aimeos\MW\Tree\Node\Iface Internal node object
397
	 */
398
	public function getNode()
399
	{
400
		return $this->node;
401
	}
402
403
404
	/**
405
	 * Returns the item type
406
	 *
407
	 * @return string Item type, subtypes are separated by slashes
408
	 */
409
	public function getResourceType()
410
	{
411
		return 'catalog';
412
	}
413
}
414