Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

Traits::initPropertyItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 * @package MShop
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\MShop\Common\Item\PropertyRef;
12
13
14
/**
15
 * Common trait for items containing property items
16
 *
17
 * @package MShop
18
 * @subpackage Common
19
 */
20
trait Traits
21
{
22
	private $propItems = [];
23
	private $propRmItems = [];
24
	private $propMax = 0;
25
26
27
	/**
28
	 * Adds a new property item or overwrite an existing one
29
	 *
30
	 * @param \Aimeos\MShop\Common\Item\Property\Iface $item New or existing property item
31
	 * @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
0 ignored issues
show
Documentation introduced by
Should the return type not be Traits?

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...
32
	 */
33
	public function addPropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item )
34
	{
35
		$id = $item->getId() ?: 'id-' . $this->propMax++;
36
		$this->propItems[$id] = $item;
37
38
		return $this;
39
	}
40
41
42
	/**
43
	 * Removes an existing property item
44
	 *
45
	 * @param \Aimeos\MShop\Common\Item\Property\Iface $item Existing property item
46
	 * @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
0 ignored issues
show
Documentation introduced by
Should the return type not be Traits?

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...
47
	 * @throws \Aimeos\MShop\Exception If given property item isn't found
48
	 */
49
	public function deletePropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item )
50
	{
51
		foreach( $this->propItems as $key => $pitem )
52
		{
53
			if( $pitem === $item )
54
			{
55
				$this->propRmItems[$item->getId()] = $item;
56
				unset( $this->propItems[$key] );
57
58
				return $this;
59
			}
60
		}
61
62
		throw new \Aimeos\MShop\Exception( sprintf( 'Property item for removal not found' ) );
63
	}
64
65
66
	/**
67
	 * Removes a list of existing property items
68
	 *
69
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Existing property items
70
	 * @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
0 ignored issues
show
Documentation introduced by
Should the return type not be Traits?

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...
71
	 * @throws \Aimeos\MShop\Exception If an item isn't a property item or isn't found
72
	 */
73
	public function deletePropertyItems( array $items )
74
	{
75
		foreach( $items as $item ) {
76
			$this->deletePropertyItem( $item );
77
		}
78
79
		return $this;
80
	}
81
82
83
	/**
84
	 * Returns the deleted property items
85
	 *
86
	 * @return \Aimeos\MShop\Common\Item\Property\Iface[] Property items
87
	 */
88
	public function getPropertyItemsDeleted()
89
	{
90
		return $this->propRmItems;
91
	}
92
93
94
	/**
95
	 * Returns the property items of the product
96
	 *
97
	 * @param array|string|null $type Name of the property item type or null for all
98
	 * @param boolean $active True to return only active items, false to return all
99
	 * @return \Aimeos\MShop\Common\Item\Property\Iface[] Associative list of property IDs as keys and property items as values
100
	 */
101
	public function getPropertyItems( $type = null, $active = true )
102
	{
103
		$list = [];
104
105
		foreach( $this->propItems as $propId => $propItem )
106
		{
107
			if( ( $type === null || in_array( $propItem->getType(), (array) $type ) )
108
				&& ( $active === false || $propItem->isAvailable() )
109
			) {
110
				$list[$propId] = $propItem;
111
			}
112
		}
113
114
		return $list;
115
	}
116
117
118
	/**
119
	 * Sets the property items in the trait
120
	 *
121
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Property items
122
	 */
123
	protected function initPropertyItems( array $items )
124
	{
125
		$this->propItems = $items;
126
	}
127
}
128