-
Notifications
You must be signed in to change notification settings - Fork 110
/
Traits.php
132 lines (108 loc) · 3.12 KB
/
Traits.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2018
* @package MShop
* @subpackage Common
*/
namespace Aimeos\MShop\Common\Item\PropertyRef;
/**
* Common trait for items containing property items
*
* @package MShop
* @subpackage Common
*/
trait Traits
{
private $propItems = [];
private $propRmItems = [];
private $propMax = 0;
/**
* Adds a new property item or overwrite an existing one
*
* @param \Aimeos\MShop\Product\Item\Property\Iface $item New or existing property item
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
*/
public function addPropertyItem( \Aimeos\MShop\Product\Item\Property\Iface $item )
{
$id = $item->getId() ?: 'id-' . $this->propMax++;
$this->propItems[$id] = $item;
return $this;
}
/**
* Removes an existing property item
*
* @param \Aimeos\MShop\Product\Item\Property\Iface $item Existing property item
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
* @throws \Aimeos\MShop\Exception If given property item isn't found
*/
public function deletePropertyItem( \Aimeos\MShop\Product\Item\Property\Iface $item )
{
foreach( $this->propItems as $key => $pitem )
{
if( $pitem === $item )
{
$this->propRmItems[$item->getId()] = $item;
unset( $this->propItems[$key] );
return $this;
}
}
throw new \Aimeos\MShop\Exception( sprintf( 'Property item for removal not found' ) );
}
/**
* Removes a list of existing property items
*
* @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Existing property items
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
* @throws \Aimeos\MShop\Exception If a property item isn't found
*/
public function deletePropertyItems( array $items )
{
foreach( $items as $item )
{
if( !( $item instanceof \Aimeos\MShop\Common\Item\Property\Iface ) ) {
throw new \Aimeos\MShop\Exception( sprintf( 'Not a property item' ) );
}
$this->deletePropertyItem( $item );
}
return $this;
}
/**
* Returns the deleted property items
*
* @return \Aimeos\MShop\Common\Item\Property\Iface[] Property items
*/
public function getPropertyItemsDeleted()
{
return $this->propRmItems;
}
/**
* Returns the property items of the product
*
* @param string|null $type Name of the property item type or null for all
* @param boolean $active True to return only active items, false to return all
* @return \Aimeos\MShop\Product\Item\Property\Iface[] Associative list of property IDs as keys and property items as values
*/
public function getPropertyItems( $type = null, $active = true )
{
$list = [];
foreach( $this->propItems as $propId => $propItem )
{
if( ( $type === null || $propItem->getType() === $type )
&& ( $active === false || $propItem->isAvailable() )
) {
$list[$propId] = $propItem;
}
}
return $list;
}
/**
* Sets the property items in the trait
*
* @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Property items
*/
protected function setPropertyItems( array $items )
{
$this->propItems = $items;
}
}