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