Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

Traits::deleteAddressItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\AddressRef;
12
13
14
/**
15
 * Common trait for items containing address items
16
 *
17
 * @package MShop
18
 * @subpackage Common
19
 */
20
trait Traits
21
{
22
	private $addrMax = 0;
23
	private $addrItems = [];
24
	private $addrRmItems = [];
25
	private $addrSorted;
26
27
28
	/**
29
	 * Adds a new address item or overwrite an existing one
30
	 *
31
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $item New or existing address item
32
	 * @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
0 ignored issues
show
Documentation introduced by
Should the return type not be Traits?

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.

Loading history...
33
	 */
34
	public function addAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item )
35
	{
36
		$id = $item->getId() ?: 'id-' . $this->addrMax++;
37
		$this->addrItems[$id] = $item;
38
		$this->addrSorted = null;
39
40
		return $this;
41
	}
42
43
44
	/**
45
	 * Removes an existing address item
46
	 *
47
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $item Existing address item
48
	 * @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
0 ignored issues
show
Documentation introduced by
Should the return type not be Traits?

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.

Loading history...
49
	 * @throws \Aimeos\MShop\Exception If given address item isn't found
50
	 */
51
	public function deleteAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item )
52
	{
53
		foreach( $this->addrItems as $key => $addrItem )
54
		{
55
			if( $addrItem === $item )
56
			{
57
				$this->addrRmItems[$item->getId()] = $item;
58
				unset( $this->addrItems[$key] );
59
60
				return $this;
61
			}
62
		}
63
64
		throw new \Aimeos\MShop\Exception( sprintf( 'Address item for removal not found' ) );
65
	}
66
67
68
	/**
69
	 * Removes a list of existing address items
70
	 *
71
	 * @param \Aimeos\MShop\Common\Item\Address\Iface[] $items Existing address items
72
	 * @return \Aimeos\MShop\Common\Item\Iface Self object for method chaining
0 ignored issues
show
Documentation introduced by
Should the return type not be Traits?

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.

Loading history...
73
	 * @throws \Aimeos\MShop\Exception If an item isn't a address item or isn't found
74
	 */
75
	public function deleteAddressItems( array $items )
76
	{
77
		foreach( $items as $item ) {
78
			$this->deleteAddressItem( $item );
79
		}
80
81
		return $this;
82
	}
83
84
85
	/**
86
	 * Returns the deleted address items
87
	 *
88
	 * @return \Aimeos\MShop\Common\Item\Address\Iface[] Address items
89
	 */
90
	public function getAddressItemsDeleted()
91
	{
92
		return $this->addrRmItems;
93
	}
94
95
96
	/**
97
	 * Returns the address items of the product
98
	 *
99
	 * @return \Aimeos\MShop\Common\Item\Address\Iface[] Associative list of address IDs as keys and address items as values
100
	 */
101
	public function getAddressItems()
102
	{
103
		if( $this->addrSorted === null )
104
		{
105
			$fcn = function( $a, $b )
106
			{
107
				if( $a->getPosition() == $b->getPosition() ) {
108
					return 0;
109
				}
110
111
				return ( $a->getPosition() < $b->getPosition() ? -1 : 1 );
112
			};
113
114
			uasort( $this->addrItems, $fcn );
115
			$this->addrSorted = true;
116
		}
117
118
		return $this->addrItems;
119
	}
120
121
122
	/**
123
	 * Initializes the address items in the trait
124
	 *
125
	 * @param \Aimeos\MShop\Common\Item\Address\Iface[] $items Address items
126
	 */
127
	protected function initAddressItems( array $items )
128
	{
129
		$this->addrItems = $items;
130
	}
131
}
132