Completed
Push — master ( 7f402b...a6068b )
by Aimeos
10:08
created

Traits::deletePropertyItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 15
rs 9.4285
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\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
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...
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
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...
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
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...
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 );
0 ignored issues
show
Documentation introduced by
$item is of type object<Aimeos\MShop\Common\Item\Property\Iface>, but the function expects a object<Aimeos\MShop\Product\Item\Property\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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