Passed
Push — master ( 8f16ed...3311dc )
by Aimeos
10:04
created

Standard::getKey()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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