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
|
|
|
* Creates a deep clone of all objects |
29
|
|
|
*/ |
30
|
|
|
public function __clone() |
31
|
|
|
{ |
32
|
|
|
parent::__clone(); |
33
|
|
|
|
34
|
|
|
foreach( $this->propItems as $key => $item ) { |
35
|
|
|
$this->propItems[$key] = clone $item; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
foreach( $this->propRmItems as $key => $item ) { |
39
|
|
|
$this->propRmItems[$key] = clone $item; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Adds a new property item or overwrite an existing one |
46
|
|
|
* |
47
|
|
|
* @param \Aimeos\MShop\Common\Item\Property\Iface $item New or existing property item |
48
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining |
49
|
|
|
*/ |
50
|
|
|
public function addPropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) |
51
|
|
|
{ |
52
|
|
|
$id = $item->getId() ?: '_' . $item->getType() . '_' . $item->getLanguageId() . '_' . $item->getValue(); |
53
|
|
|
$this->propItems[$id] = $item; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Removes an existing property item |
61
|
|
|
* |
62
|
|
|
* @param \Aimeos\MShop\Common\Item\Property\Iface $item Existing property item |
63
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining |
64
|
|
|
* @throws \Aimeos\MShop\Exception If given property item isn't found |
65
|
|
|
*/ |
66
|
|
|
public function deletePropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) |
67
|
|
|
{ |
68
|
|
|
$id = '_' . $item->getType() . '_' . $item->getLanguageId() . '_' . $item->getValue(); |
69
|
|
|
|
70
|
|
|
if( !isset( $this->propItems[$id] ) && !isset( $this->propItems[$item->getId()] ) ) { |
71
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Property item for removal not found' ) ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
unset( $this->propItems[$id], $this->propItems[$item->getId()] ); |
75
|
|
|
$this->propRmItems[$id] = $item; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Removes a list of existing property items |
83
|
|
|
* |
84
|
|
|
* @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Existing property items |
85
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining |
86
|
|
|
* @throws \Aimeos\MShop\Exception If an item isn't a property item or isn't found |
87
|
|
|
*/ |
88
|
|
|
public function deletePropertyItems( array $items ) |
89
|
|
|
{ |
90
|
|
|
foreach( $items as $item ) { |
91
|
|
|
$this->deletePropertyItem( $item ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the deleted property items |
100
|
|
|
* |
101
|
|
|
* @return \Aimeos\MShop\Common\Item\Property\Iface[] Property items |
102
|
|
|
*/ |
103
|
|
|
public function getPropertyItemsDeleted() |
104
|
|
|
{ |
105
|
|
|
return $this->propRmItems; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns the property values for the given type |
111
|
|
|
* |
112
|
|
|
* @param string $type Type of the properties |
113
|
|
|
* @return array List of property values |
114
|
|
|
*/ |
115
|
|
|
public function getProperties( $type ) |
116
|
|
|
{ |
117
|
|
|
$list = []; |
118
|
|
|
|
119
|
|
|
foreach( $this->getPropertyItems( $type ) as $id => $item ) { |
120
|
|
|
$list[$id] = $item->getValue(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $list; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the property item for the given type, language and value |
129
|
|
|
* |
130
|
|
|
* @param string $type Name of the property type |
131
|
|
|
* @param string $langId ISO language code (e.g. "en" or "en_US") or null if not language specific |
132
|
|
|
* @param string $value Value of the property |
133
|
|
|
* @param boolean $active True to return only active items, false to return all |
134
|
|
|
* @return \Aimeos\MShop\Common\Item\Property\Iface|null Matching property item or null if none |
135
|
|
|
*/ |
136
|
|
|
public function getPropertyItem( $type, $langId, $value, $active = true ) |
137
|
|
|
{ |
138
|
|
|
foreach( $this->propItems as $propItem ) |
139
|
|
|
{ |
140
|
|
|
if( $propItem->getType() === $type && $propItem->getLanguageId() === $langId |
141
|
|
|
&& $propItem->getValue() === $value && ( $active === false || $propItem->isAvailable() ) |
142
|
|
|
) { |
143
|
|
|
return $propItem; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the property items of the product |
151
|
|
|
* |
152
|
|
|
* @param array|string|null $type Name of the property item type or null for all |
153
|
|
|
* @param boolean $active True to return only active items, false to return all |
154
|
|
|
* @return \Aimeos\MShop\Common\Item\Property\Iface[] Associative list of property IDs as keys and property items as values |
155
|
|
|
*/ |
156
|
|
|
public function getPropertyItems( $type = null, $active = true ) |
157
|
|
|
{ |
158
|
|
|
$list = []; |
159
|
|
|
|
160
|
|
|
foreach( $this->propItems as $propId => $propItem ) |
161
|
|
|
{ |
162
|
|
|
if( ( $type === null || in_array( $propItem->getType(), (array) $type ) ) |
163
|
|
|
&& ( $active === false || $propItem->isAvailable() ) |
164
|
|
|
) { |
165
|
|
|
$list[$propId] = $propItem; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $list; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Sets the property items in the trait |
175
|
|
|
* |
176
|
|
|
* @param \Aimeos\MShop\Common\Item\Property\Iface[] $items Property items |
177
|
|
|
*/ |
178
|
|
|
protected function initPropertyItems( array $items ) |
179
|
|
|
{ |
180
|
|
|
$this->propItems = $items; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|