Passed
Push — master ( 892c73...5dcb5e )
by Aimeos
04:29
created

Standard::fromArray()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 10
nc 6
nop 2
dl 0
loc 19
rs 8.8333
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), 2014-2018
6
 * @package MShop
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\MShop\Common\Item\Property;
12
13
14
/**
15
 * Default property item implementation.
16
 *
17
 * @package MShop
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Common\Item\Property\Iface
23
{
24
	private $prefix;
25
	private $values;
26
27
28
	/**
29
	 * Initializes the property item object with the given values
30
	 *
31
	 * @param string $prefix Property prefix when converting to array
32
	 * @param array $values Initial values of the list type item
33
	 */
34
	public function __construct( $prefix, array $values = [] )
35
	{
36
		parent::__construct( $prefix, $values );
37
38
		$this->prefix = $prefix;
39
		$this->values = $values;
40
	}
41
42
43
	/**
44
	 * Returns the language ID of the property item.
45
	 *
46
	 * @return string|null Language ID of the property item
47
	 */
48
	public function getLanguageId()
49
	{
50
		if( isset( $this->values[$this->prefix . 'languageid'] ) ) {
51
			return (string) $this->values[$this->prefix . 'languageid'];
52
		}
53
54
		return null;
55
	}
56
57
58
	/**
59
	 *  Sets the language ID of the property item.
60
	 *
61
	 * @param string|null $id Language ID of the property item
62
	 * @return \Aimeos\MShop\Common\Item\Property\Iface Common property item for chaining method calls
63
	 */
64
	public function setLanguageId( $id )
65
	{
66
		if( $id !== $this->getLanguageId() )
67
		{
68
			$this->values[$this->prefix . 'languageid'] = $this->checkLanguageId( $id );
69
			$this->setModified();
70
		}
71
72
		return $this;
73
	}
74
75
76
	/**
77
	 * Returns the parent id of the property item
78
	 *
79
	 * @return string|null Parent ID of the property item
80
	 */
81
	public function getParentId()
82
	{
83
		if( isset( $this->values[$this->prefix . 'parentid'] ) ) {
84
			return (string) $this->values[$this->prefix . 'parentid'];
85
		}
86
	}
87
88
89
	/**
90
	 * Sets the new parent ID of the property item
91
	 *
92
	 * @param string $id Parent ID of the property item
93
	 * @return \Aimeos\MShop\Common\Item\Property\Iface Common property item for chaining method calls
94
	 */
95
	public function setParentId( $id )
96
	{
97
		if( (string) $id !== $this->getParentId() )
98
		{
99
			$this->values[$this->prefix . 'parentid'] = (string) $id;
100
			$this->setModified();
101
		}
102
103
		return $this;
104
	}
105
106
107
	/**
108
	 * Returns the type code of the property item.
109
	 *
110
	 * @return string|null Type code of the property item
111
	 */
112
	public function getType()
113
	{
114
		if( isset( $this->values[$this->prefix . 'type'] ) ) {
115
			return (string) $this->values[$this->prefix . 'type'];
116
		}
117
	}
118
119
120
	/**
121
	 * Sets the new type of the property item
122
	 *
123
	 * @param string $type Type of the property item
124
	 * @return \Aimeos\MShop\Common\Item\Property\Iface Common property item for chaining method calls
125
	 */
126
	public function setType( $type )
127
	{
128
		if( (string) $type !== $this->getType() )
129
		{
130
			$this->values[$this->prefix . 'type'] = (string) $type;
131
			$this->setModified();
132
		}
133
134
		return $this;
135
	}
136
137
138
	/**
139
	 * Returns the value of the property item.
140
	 *
141
	 * @return string Value of the property item
142
	 */
143
	public function getValue()
144
	{
145
		if( isset( $this->values[$this->prefix . 'value'] ) ) {
146
			return (string) $this->values[$this->prefix . 'value'];
147
		}
148
149
		return '';
150
	}
151
152
153
	/**
154
	 * Sets the new value of the property item.
155
	 *
156
	 * @param string $value Value of the property item
157
	 * @return \Aimeos\MShop\Common\Item\Property\Iface Common property item for chaining method calls
158
	 */
159
	public function setValue( $value )
160
	{
161
		if( (string) $value !== $this->getValue() )
162
		{
163
			$this->values[$this->prefix . 'value'] = (string) $value;
164
			$this->setModified();
165
		}
166
167
		return $this;
168
	}
169
170
171
	/**
172
	 * Returns the item type
173
	 *
174
	 * @return string Item type, subtypes are separated by slashes
175
	 */
176
	public function getResourceType()
177
	{
178
		return str_replace( '.', '/', rtrim( $this->prefix, '.' ) );
179
	}
180
181
182
	/**
183
	 * Tests if the item is available based on status, time, language and currency
184
	 *
185
	 * @return boolean True if available, false if not
186
	 */
187
	public function isAvailable()
188
	{
189
		return parent::isAvailable() && ( $this->values['languageid'] === null
190
			|| $this->getLanguageId() === null
191
			|| $this->getLanguageId() === $this->values['languageid'] );
192
	}
193
194
195
	/*
196
	 * Sets the item values from the given array and removes that entries from the list
197
	 *
198
	 * @param array &$list Associative list of item keys and their values
199
	 * @param boolean True to set private properties too, false for public only
200
	 * @return \Aimeos\MShop\Common\Item\Property\Iface Property item for chaining method calls
201
	 */
202
	public function fromArray( array &$list, $private = false )
203
	{
204
		$item = parent::fromArray( $list, $private );
205
206
		foreach( $list as $key => $value )
207
		{
208
			switch( $key )
209
			{
210
				case $this->prefix . 'parentid': !$private ?: $item = $item->setParentId( $value ); break;
211
				case $this->prefix . 'languageid': $item = $item->setLanguageId( $value ); break;
212
				case $this->prefix . 'value': $item = $item->setValue( $value ); break;
213
				case $this->prefix . 'type': $item = $item->setType( $value ); break;
214
				default: continue 2;
215
			}
216
217
			unset( $list[$key] );
218
		}
219
220
		return $item;
221
	}
222
223
224
	/**
225
	 * Returns the item values as array.
226
	 *
227
	 * @param boolean True to return private properties, false for public only
228
	 * @return array Associative list of item properties and their values
229
	 */
230
	public function toArray( $private = false )
231
	{
232
		$list = parent::toArray( $private );
233
234
		$list[$this->prefix . 'languageid'] = $this->getLanguageId();
235
		$list[$this->prefix . 'value'] = $this->getValue();
236
		$list[$this->prefix . 'type'] = $this->getType();
237
238
		if( $private === true ) {
239
			$list[$this->prefix . 'parentid'] = $this->getParentId();
240
		}
241
242
		return $list;
243
	}
244
245
}
246