Passed
Push — master ( 6cc5b5...1f0321 )
by Aimeos
04:54
created

Base::getAttributeItemsDeleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package MShop
7
 * @subpackage Order
8
 */
9
10
11
namespace Aimeos\MShop\Order\Item\Service;
12
13
14
/**
15
 * Order service item abstract class defining available types.
16
 *
17
 * @package MShop
18
 * @subpackage Order
19
 */
20
abstract class Base extends \Aimeos\MShop\Common\Item\Base implements Iface
21
{
22
	/**
23
	 * Delivery service.
24
	 */
25
	const TYPE_DELIVERY = 'delivery';
26
27
	/**
28
	 * Payment service.
29
	 */
30
	const TYPE_PAYMENT = 'payment';
31
32
33
	private ?array $attributesMap = null;
34
	private array $attrRmItems = [];
35
36
37
	/**
38
	 * Adds new and replaces existing attribute items for the service.
39
	 *
40
	 * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Service\Attribute\Iface[] $attributes List of order service attribute items
41
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
42
	 */
43
	public function addAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Service\Iface
44
	{
45
		map( $attributes )->implements( \Aimeos\MShop\Order\Item\Service\Attribute\Iface::class, true );
46
47
		foreach( $attributes as $attrItem ) {
48
			$this->setAttributeItem( $attrItem );
49
		}
50
51
		return $this;
52
	}
53
54
55
	/**
56
	 * Returns the value or list of values of the attribute item for the ordered service with the given code.
57
	 *
58
	 * @param string $code Code of the service attribute item
59
	 * @param array|string $type Type or list of types of the service attribute items
60
	 * @return array|string|null Value or list of values of the attribute item for the ordered service and the given code
61
	 */
62
	public function getAttribute( string $code, $type = '' )
63
	{
64
		$list = [];
65
		$map = $this->getAttributeMap();
66
67
		foreach( (array) $type as $key )
68
		{
69
			if( isset( $map[$key][$code] ) )
70
			{
71
				foreach( $map[$key][$code] as $item ) {
72
					$list[] = $item->getValue();
73
				}
74
			}
75
		}
76
77
		return count( $list ) > 1 ? $list : ( reset( $list ) ?: null );
78
	}
79
80
81
	/**
82
	 * Returns the attribute item or list of attribute items for the ordered service with the given code.
83
	 *
84
	 * @param string $code Code of the service attribute item
85
	 * @param array|string $type Type of the service attribute item
86
	 * @return \Aimeos\MShop\Order\Item\Service\Attribute\Iface|array|null
87
	 * 	Attribute item or list of items for the ordered service and the given code
88
	 */
89
	public function getAttributeItem( string $code, $type = '' )
90
	{
91
		$list = [];
92
		$map = $this->getAttributeMap();
93
94
		foreach( (array) $type as $key )
95
		{
96
			if( isset( $map[$key][$code] ) )
97
			{
98
				foreach( $map[$key][$code] as $item ) {
99
					$list[] = $item;
100
				}
101
			}
102
		}
103
104
		return count( $list ) > 1 ? $list : ( reset( $list ) ?: null );
105
	}
106
107
108
	/**
109
	 * Returns the list of attribute items for the ordered service.
110
	 *
111
	 * @param string|null $type Filters returned attributes by the given type or null for no filtering
112
	 * @return \Aimeos\Map List of attribute items implementing \Aimeos\MShop\Order\Item\Service\Attribute\Iface
113
	 */
114
	public function getAttributeItems( ?string $type = null ) : \Aimeos\Map
115
	{
116
		if( $type === null ) {
117
			return map( $this->get( '.attributes', [] ) );
118
		}
119
120
		$list = [];
121
122
		foreach( $this->get( '.attributes', [] ) as $attrItem )
123
		{
124
			if( $attrItem->getType() === $type ) {
125
				$list[] = $attrItem;
126
			}
127
		}
128
129
		return map( $list );
130
	}
131
132
133
	/**
134
	 * Returns the deleted attribute items for the service.
135
	 *
136
	 * @return \Aimeos\Map List of items to be removed
137
	 */
138
	public function getAttributeItemsDeleted() : \Aimeos\Map
139
	{
140
		return map( $this->attrRmItems );
141
	}
142
143
144
	/**
145
	 * Adds or replaces the attribute item in the list of service attributes.
146
	 *
147
	 * @param \Aimeos\MShop\Order\Item\Service\Attribute\Iface $item Service attribute item
148
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
149
	 */
150
	public function setAttributeItem( \Aimeos\MShop\Order\Item\Service\Attribute\Iface $item ) : \Aimeos\MShop\Order\Item\Service\Iface
151
	{
152
		$this->getAttributeMap();
153
154
		$type = $item->getType();
155
		$code = $item->getCode();
156
		$attrId = $item->getAttributeId();
157
158
		if( !isset( $this->attributesMap[$type][$code][$attrId] ) )
159
		{
160
			$this->set( '.attributes', map( $this->get( '.attributes', [] ) )->push( $item ) );
161
			$this->attributesMap[$type][$code][$attrId] = $item;
162
		}
163
164
		$this->attributesMap[$type][$code][$attrId]->setValue( $item->getValue() );
165
166
		return $this->setModified();
167
	}
168
169
170
	/**
171
	 * Sets the new list of attribute items for the service.
172
	 *
173
	 * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Service\Attribute\Iface[] $attributes List of order service attribute items
174
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
175
	 */
176
	public function setAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Service\Iface
177
	{
178
		map( $attributes )->implements( \Aimeos\MShop\Order\Item\Service\Attribute\Iface::class, true );
179
180
		$this->attrRmItems = map( $this->get( '.attributes', [] ) )
181
			->diff( $attributes )
182
			->merge( $this->attrRmItems )
183
			->unique()
184
			->toArray();
185
186
		$this->set( '.attributes', iterator_to_array( $attributes ) );
187
		$this->attributesMap = null;
188
189
		return $this;
190
	}
191
192
193
	/**
194
	 * Adds a new transaction to the service.
195
	 *
196
	 * @param \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item Transaction item
197
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
198
	 */
199
	public function addTransaction( \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item ) : \Aimeos\MShop\Order\Item\Service\Iface
200
	{
201
		return $this->set( '.transactions', map( $this->get( '.transactions', [] ) )->push( $item ) );
202
	}
203
204
205
	/**
206
	 * Returns the list of transactions items for the service.
207
	 *
208
	 * @param string|null $type Filters returned transactions by the given type or null for no filtering
209
	 * @return \Aimeos\Map List of transaction items implementing \Aimeos\MShop\Order\Item\Service\Attribute\Iface
210
	 */
211
	public function getTransactions( ?string $type = null ) : \Aimeos\Map
212
	{
213
		return map( $this->get( '.transactions', [] ) );
214
	}
215
216
217
	/**
218
	 * Sets the new list of transactions items for the service.
219
	 *
220
	 * @param iterable $list List of order service transaction items
221
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
222
	 */
223
	public function setTransactions( iterable $list ) : \Aimeos\MShop\Order\Item\Service\Iface
224
	{
225
		return $this->set( '.transactions', $list );
226
	}
227
228
229
	/**
230
	 * Returns the attribute map for the ordered services.
231
	 *
232
	 * @return array Associative list of type and code as key and an \Aimeos\MShop\Order\Item\Service\Attribute\Iface as value
233
	 */
234
	protected function getAttributeMap() : array
235
	{
236
		if( !isset( $this->attributesMap ) )
237
		{
238
			$this->attributesMap = [];
239
240
			foreach( $this->get( '.attributes', [] ) as $item ) {
241
				$this->attributesMap[$item->getType()][$item->getCode()][$item->getAttributeId()] = $item;
242
			}
243
		}
244
245
		return $this->attributesMap;
246
	}
247
}
248