Passed
Push — master ( dfd221...62fc03 )
by Aimeos
05:19
created

Base::checkFlags()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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