Standard::getCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2025
7
 * @package MShop
8
 * @subpackage Catalog
9
 */
10
11
12
namespace Aimeos\MShop\Catalog\Item;
13
14
use \Aimeos\MShop\Common\Item\Config;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\Config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use \Aimeos\MShop\Common\Item\ListsRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\ListsRef was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
18
/**
19
 * Generic interface for catalog items.
20
 *
21
 * @package MShop
22
 * @subpackage Catalog
23
 */
24
class Standard
25
	extends \Aimeos\MShop\Common\Item\Base
26
	implements \Aimeos\MShop\Catalog\Item\Iface
27
{
28
	use Config\Traits, ListsRef\Traits {
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\Config\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \Aimeos\MShop\Common\Item\ListsRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
		ListsRef\Traits::__clone as __cloneList;
30
		ListsRef\Traits::getName as getNameList;
31
	}
32
33
34
	private \Aimeos\MW\Tree\Node\Iface $node;
35
	private array $deletedItems = [];
36
	private array $children;
37
38
39
	/**
40
	 * Initializes the catalog item.
41
	 *
42
	 * @param \Aimeos\MW\Tree\Node\Iface $node Tree node
43
	 * @param array $values Assoicative list of key/value pairs
44
	 * @param \Aimeos\MShop\Catalog\Item\Iface[] $children List of children of the item
45
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
46
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
47
	 */
48
	public function __construct( \Aimeos\MW\Tree\Node\Iface $node, array $values = [], array $children = [],
49
		array $listItems = [], array $refItems = [] )
50
	{
51
		parent::__construct( '', $values );
52
53
		map( $children )->implements( \Aimeos\MShop\Catalog\Item\Iface::class, true );
54
55
		$this->initListItems( $listItems, $refItems );
56
		$this->children = $children;
57
		$this->node = $node;
58
	}
59
60
61
	/**
62
	 * Clones internal objects of the catalog item.
63
	 */
64
	public function __clone()
65
	{
66
		parent::__clone();
67
		$this->__cloneList();
68
		$this->node = clone $this->node;
69
	}
70
71
72
	/**
73
	 * Tests if the item property for the given name is available
74
	 *
75
	 * @param string $name Name of the property
76
	 * @return bool True if the property exists, false if not
77
	 */
78
	public function __isset( string $name ) : bool
79
	{
80
		if( $name === 'children' ) {
81
			return true;
82
		}
83
84
		return parent::__isset( $name ) ?: isset( $this->node->$name );
85
	}
86
87
88
	/**
89
	 * Returns the item property for the given name
90
	 *
91
	 * @param string $name Name of the property
92
	 * @param mixed $default Default value if property is unknown
93
	 * @return mixed|null Property value or default value if property is unknown
94
	 */
95
	public function get( string $name, $default = null )
96
	{
97
		if( $name === 'children' ) {
98
			return $this->children;
99
		}
100
101
		if( ( $value = parent::get( $name ) ) !== null ) {
102
			return $value;
103
		}
104
105
		return $this->node->$name ?? $default;
106
	}
107
108
109
	/**
110
	 * Returns the unique ID of the node.
111
	 *
112
	 * @return string|null Unique ID of the node
113
	 */
114
	public function getId() : ?string
115
	{
116
		return $this->node->getId();
117
	}
118
119
120
	/**
121
	 * Sets the unique ID of the node.
122
	 *
123
	 * @param string|null $id Unique ID of the node
124
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
125
	 */
126
	public function setId( ?string $id = null ) : \Aimeos\MShop\Common\Item\Iface
127
	{
128
		$this->node->setId( $id );
129
		return $this;
130
	}
131
132
133
	/**
134
	 * Returns the site ID of the item.
135
	 *
136
	 * @return string Site ID of the item
137
	 */
138
	public function getSiteId() : string
139
	{
140
		return (string) $this->node->siteid;
141
	}
142
143
144
	/**
145
	 * Returns the internal name of the item.
146
	 *
147
	 * @return string Name of the item
148
	 */
149
	public function getLabel() : string
150
	{
151
		return $this->node->getLabel();
152
	}
153
154
155
	/**
156
	 * Sets the new internal name of the item.
157
	 *
158
	 * @param string $name New name of the item
159
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
160
	 */
161
	public function setLabel( string $name ) : \Aimeos\MShop\Common\Item\Tree\Iface
162
	{
163
		$this->node->setLabel( $name );
164
		return $this;
165
	}
166
167
168
	/**
169
	 * Returns the localized text type of the item or the internal label if no name is available.
170
	 *
171
	 * @param string $type Text type to be returned
172
	 * @param string|null $langId Two letter ISO Language code of the text
173
	 * @return string Specified text type or label of the item
174
	 */
175
	public function getName( string $type = 'name', ?string $langId = null ) : string
176
	{
177
		$name = $this->getNameList( $type, $langId );
178
179
		if( $type === 'url' && $name === $this->getLabel() ) {
180
			return $this->getUrl();
181
		}
182
183
		return $name;
184
	}
185
186
187
	/**
188
	 * Returns the URL segment for the catalog item.
189
	 *
190
	 * @return string URL segment of the catalog item
191
	 */
192
	public function getUrl() : string
193
	{
194
		return (string) ( $this->node->url ?: \Aimeos\Base\Str::slug( $this->getLabel() ) );
195
	}
196
197
198
	/**
199
	 * Sets a new URL segment for the catalog.
200
	 *
201
	 * @param string|null $url New URL segment of the catalog item
202
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
203
	 */
204
	public function setUrl( ?string $url ) : \Aimeos\MShop\Catalog\Item\Iface
205
	{
206
		$this->node->url = (string) $url;
207
		return $this;
208
	}
209
210
211
	/**
212
	 * Returns the config property of the catalog.
213
	 *
214
	 * @return array Returns the config of the catalog node
215
	 */
216
	public function getConfig() : array
217
	{
218
		return (array) parent::get( 'config', [] );
219
	}
220
221
222
	/**
223
	 * Sets the config property of the catalog item.
224
	 *
225
	 * @param array $options Options to be set for the catalog node
226
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
227
	 */
228
	public function setConfig( array $options ) : \Aimeos\MShop\Common\Item\Iface
229
	{
230
		parent::set( 'config', $options );
231
		return $this;
232
	}
233
234
235
	/**
236
	 * Returns the code of the item.
237
	 *
238
	 * @return string Code of the item
239
	 */
240
	public function getCode() : string
241
	{
242
		return $this->node->getCode();
243
	}
244
245
246
	/**
247
	 * Sets the new code of the item.
248
	 *
249
	 * @param string $code New code of the item
250
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
251
	 */
252
	public function setCode( string $code ) : \Aimeos\MShop\Common\Item\Tree\Iface
253
	{
254
		$this->node->setCode( \Aimeos\Utils::code( $code ) );
255
		return $this;
256
	}
257
258
259
	/**
260
	 * Returns the status of the item.
261
	 *
262
	 * @return int Greater than zero if enabled, zero or negative values if disabled
263
	 */
264
	public function getStatus() : int
265
	{
266
		return $this->node->getStatus();
267
	}
268
269
270
	/**
271
	 * Sets the new status of the item.
272
	 *
273
	 * @param int $status True if enabled, false if not
274
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
275
	 */
276
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
277
	{
278
		$this->node->setStatus( $status );
279
		return $this;
280
	}
281
282
283
	/**
284
	 * Returns the URL target specific for that category
285
	 *
286
	 * @return string URL target specific for that category
287
	 */
288
	public function getTarget() : string
289
	{
290
		return (string) $this->node->target;
291
	}
292
293
294
	/**
295
	 * Sets a new URL target specific for that category
296
	 *
297
	 * @param string $value New URL target specific for that category
298
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
299
	 */
300
	public function setTarget( ?string $value ) : \Aimeos\MShop\Catalog\Item\Iface
301
	{
302
		$this->node->target = (string) $value;
303
		return $this;
304
	}
305
306
307
	/**
308
	 * Returns modify date/time of the order item base product.
309
	 *
310
	 * @return string|null Returns modify date/time of the order base item
311
	 */
312
	public function getTimeModified() : ?string
313
	{
314
		return $this->node->mtime;
315
	}
316
317
318
	/**
319
	 * Returns the create date of the item.
320
	 *
321
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
322
	 */
323
	public function getTimeCreated() : ?string
324
	{
325
		return $this->node->ctime;
326
	}
327
328
329
	/**
330
	 * Returns the editor code of editor who created/modified the item at last.
331
	 *
332
	 * @return string Editor who created/modified the item at last
333
	 */
334
	public function editor() : string
335
	{
336
		return (string) $this->node->editor;
337
	}
338
339
340
	/**
341
	 * Adds a child node to this node.
342
	 *
343
	 * @param \Aimeos\MShop\Common\Item\Tree\Iface $item Child node to add
344
	 * @return \Aimeos\MShop\Common\Item\Tree\Iface Tree item for chaining method calls
345
	 */
346
	public function addChild( \Aimeos\MShop\Common\Item\Tree\Iface $item ) : \Aimeos\MShop\Common\Item\Tree\Iface
347
	{
348
		// don't set the modified flag as it's only for the values
349
		$this->children[] = $item;
350
351
		return $this;
352
	}
353
354
355
	/**
356
	 * Removes a child node from this node.
357
	 *
358
	 * @param \Aimeos\MShop\Common\Item\Tree\Iface $item Child node to remove
359
	 * @return \Aimeos\MShop\Common\Item\Tree\Iface Tree item for chaining method calls
360
	 */
361
	public function deleteChild( \Aimeos\MShop\Common\Item\Tree\Iface $item ) : \Aimeos\MShop\Common\Item\Tree\Iface
362
	{
363
		foreach( $this->children as $idx => $child )
364
		{
365
			if( $child === $item )
366
			{
367
				$this->deletedItems[] = $item;
368
				unset( $this->children[$idx] );
369
			}
370
		}
371
372
		return $this;
373
	}
374
375
376
	/**
377
	 * Returns a child of this node identified by its index.
378
	 *
379
	 * @param int $index Index of child node
380
	 * @return \Aimeos\MShop\Catalog\Item\Iface Selected node
381
	 */
382
	public function getChild( int $index ) : \Aimeos\MShop\Common\Item\Tree\Iface
383
	{
384
		if( isset( $this->children[$index] ) ) {
385
			return $this->children[$index];
386
		}
387
388
		throw new \Aimeos\MShop\Catalog\Exception( sprintf( 'Child node with index "%1$d" not available', $index ) );
389
	}
390
391
392
	/**
393
	 * Returns all children of this node.
394
	 *
395
	 * @return \Aimeos\Map Numerically indexed list of children implementing \Aimeos\MShop\Catalog\Item\Iface
396
	 */
397
	public function getChildren() : \Aimeos\Map
398
	{
399
		return map( $this->children );
400
	}
401
402
403
	/**
404
	 * Returns the deleted children.
405
	 *
406
	 * @return \Aimeos\Map List of removed children implementing \Aimeos\MShop\Catalog\Item\Iface
407
	 */
408
	public function getChildrenDeleted() : \Aimeos\Map
409
	{
410
		return map( $this->deletedItems );
411
	}
412
413
414
	/**
415
	 * Tests if a node has children.
416
	 *
417
	 * @return bool True if node has children, false if not
418
	 */
419
	public function hasChildren() : bool
420
	{
421
		if( count( $this->children ) > 0 ) {
422
			return true;
423
		}
424
425
		return $this->node->hasChildren();
426
	}
427
428
429
	/**
430
	 * Returns the internal node.
431
	 *
432
	 * For internal use only!
433
	 *
434
	 * @return \Aimeos\MW\Tree\Node\Iface Internal node object
435
	 */
436
	public function getNode() : \Aimeos\MW\Tree\Node\Iface
437
	{
438
		return $this->node;
439
	}
440
441
442
	/**
443
	 * Returns the level of the item in the tree
444
	 *
445
	 * For internal use only!
446
	 *
447
	 * @return int Level of the item starting with "0" for the root node
448
	 */
449
	public function getLevel() : int
450
	{
451
		return $this->node->level ?: 0;
452
	}
453
454
455
	/**
456
	 * Returns the ID of the parent category
457
	 *
458
	 * For internal use only!
459
	 *
460
	 * @return string|null Unique ID of the parent category
461
	 */
462
	public function getParentId() : ?string
463
	{
464
		return $this->node->parentid;
465
	}
466
467
468
	/**
469
	 * Tests if the item is available based on status, time, language and currency
470
	 *
471
	 * @return bool True if available, false if not
472
	 */
473
	public function isAvailable() : bool
474
	{
475
		return parent::isAvailable() && $this->getStatus() > 0;
476
	}
477
478
479
	/**
480
	 * Checks, whether this node was modified.
481
	 *
482
	 * @return bool True if the content of the node is modified, false if not
483
	 */
484
	public function isModified() : bool
485
	{
486
		return parent::isModified() || $this->node->isModified();
487
	}
488
489
490
	/*
491
	 * Sets the item values from the given array and removes that entries from the list
492
	 *
493
	 * @param array &$list Associative list of item keys and their values
494
	 * @param bool True to set private properties too, false for public only
495
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item for chaining method calls
496
	 */
497
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
498
	{
499
		$item = parent::fromArray( $list, $private );
500
501
		foreach( $list as $key => $value )
502
		{
503
			switch( $key )
504
			{
505
				case 'catalog.url': $item->setUrl( $value ); break;
506
				case 'catalog.code': $item->setCode( $value ); break;
507
				case 'catalog.label': $item->setLabel( $value ); break;
508
				case 'catalog.target': $item->setTarget( $value ); break;
509
				case 'catalog.status': $item->setStatus( (int) $value ); break;
510
				case 'catalog.config': $item->setConfig( (array) $value ); break;
511
				case 'catalog.id': !$private ?: $item->setId( $value ); break;
512
				default: continue 2;
513
			}
514
515
			unset( $list[$key] );
516
		}
517
518
		return $item;
519
	}
520
521
522
	/**
523
	 * Returns the public values of the node as array.
524
	 *
525
	 * @param bool True to return private properties, false for public only
526
	 * @return array Assciative list of key/value pairs
527
	 */
528
	public function toArray( bool $private = false ) : array
529
	{
530
		$list = [
531
			'catalog.url' => $this->getUrl(),
532
			'catalog.code' => $this->getCode(),
533
			'catalog.label' => $this->getLabel(),
534
			'catalog.config' => $this->getConfig(),
535
			'catalog.status' => $this->getStatus(),
536
			'catalog.target' => $this->getTarget(),
537
			'catalog.hasChildren' => $this->hasChildren(),
538
		];
539
540
		if( $private === true )
541
		{
542
			$list['catalog.id'] = $this->getId();
543
			$list['catalog.level'] = $this->getLevel();
544
			$list['catalog.siteid'] = $this->getSiteId();
545
			$list['catalog.parentid'] = $this->getParentId();
546
			$list['catalog.ctime'] = $this->getTimeCreated();
547
			$list['catalog.mtime'] = $this->getTimeModified();
548
			$list['catalog.editor'] = $this->editor();
549
		}
550
551
		return $list;
552
	}
553
554
555
	/**
556
	 * Returns the node and its children as list
557
	 *
558
	 * @return \Aimeos\Map List of IDs as keys and items implementing \Aimeos\MShop\Catalog\Item\Iface
559
	 */
560
	public function toList() : \Aimeos\Map
561
	{
562
		$list = map( [$this->getId() => $this] );
563
564
		foreach( $this->getChildren() as $child ) {
565
			$list = $list->union( $child->toList() );
566
		}
567
568
		return $list;
569
	}
570
}
571