Completed
Push — master ( ab8a43...3303a4 )
by Aimeos
11:36
created

Standard::isAvailable()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 7
nop 0
dl 0
loc 7
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MShop
8
 * @subpackage Text
9
 */
10
11
12
namespace Aimeos\MShop\Text\Item;
13
14
15
/**
16
 * Default text manager implementation.
17
 *
18
 * @package MShop
19
 * @subpackage Text
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\ListRef\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Text\Item\Iface
24
{
25
	private $values;
26
27
28
	/**
29
	 * Initializes the text item object with the given values.
30
	 *
31
	 * @param array $values Associative list of key/value pairs
32
	 * @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items
33
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
34
	 */
35
	public function __construct( array $values = [], array $listItems = [], array $refItems = [] )
36
	{
37
		parent::__construct( 'text.', $values, $listItems, $refItems );
38
39
		$this->values = $values;
40
	}
41
42
43
	/**
44
	 * Returns the ISO language code.
45
	 *
46
	 * @return string|null ISO language code (e.g. de or de_DE)
47
	 */
48
	public function getLanguageId()
49
	{
50
		if( isset( $this->values['text.languageid'] ) ) {
51
			return (string) $this->values['text.languageid'];
52
		}
53
	}
54
55
56
	/**
57
	 * Sets the ISO language code.
58
	 *
59
	 * @param string|null $id ISO language code (e.g. de or de_DE)
60
	 * @return \Aimeos\MShop\Text\Item\Iface Text item for chaining method calls
61
	 * @throws \Aimeos\MShop\Exception If the language ID is invalid
62
	 */
63
	public function setLanguageId( $id )
64
	{
65
		if( $id !== $this->getLanguageId() )
66
		{
67
			$this->values['text.languageid'] = $this->checkLanguageId( $id );
68
			$this->setModified();
69
		}
70
71
		return $this;
72
	}
73
74
75
	/**
76
	 * Returns the type ID of the text item.
77
	 *
78
	 * @return string|null Type ID of the text item
79
	 */
80
	public function getTypeId()
81
	{
82
		if( isset( $this->values['text.typeid'] ) ) {
83
			return (string) $this->values['text.typeid'];
84
		}
85
	}
86
87
88
	/**
89
	 *  Sets the type ID of the text item.
90
	 *
91
	 * @param string $typeid Type ID of the text type
92
	 * @return \Aimeos\MShop\Text\Item\Iface Text item for chaining method calls
93
	 */
94
	public function setTypeId( $typeid )
95
	{
96
		if( (string) $typeid !== $this->getTypeId() )
97
		{
98
			$this->values['text.typeid'] = (string) $typeid;
99
			$this->setModified();
100
		}
101
102
		return $this;
103
	}
104
105
106
	/**
107
	 * Returns the type of the text item.
108
	 *
109
	 * @return string|null Type of the text item
110
	 */
111
	public function getType()
112
	{
113
		if( isset( $this->values['text.type'] ) ) {
114
			return (string) $this->values['text.type'];
115
		}
116
	}
117
118
119
	/**
120
	 * Returns the localized name of the type
121
	 *
122
	 * @return string|null Localized name of the type
123
	 */
124
	public function getTypeName()
125
	{
126
		if( isset( $this->values['text.typename'] ) ) {
127
			return (string) $this->values['text.typename'];
128
		}
129
	}
130
131
132
	/**
133
	 * Returns the domain of the text item.
134
	 *
135
	 * @return string Domain of the text item
136
	 */
137
	public function getDomain()
138
	{
139
		if( isset( $this->values['text.domain'] ) ) {
140
			return (string) $this->values['text.domain'];
141
		}
142
143
		return '';
144
	}
145
146
147
	/**
148
	 * Sets the domain of the text item.
149
	 *
150
	 * @param string $domain Domain of the text item
151
	 * @return \Aimeos\MShop\Text\Item\Iface Text item for chaining method calls
152
	 */
153
	public function setDomain( $domain )
154
	{
155
		if( (string) $domain !== $this->getDomain() )
156
		{
157
			$this->values['text.domain'] = (string) $domain;
158
			$this->setModified();
159
		}
160
161
		return $this;
162
	}
163
164
165
	/**
166
	 * Returns the content of the text item.
167
	 *
168
	 * @return string Content of the text item
169
	 */
170
	public function getContent()
171
	{
172
		if( isset( $this->values['text.content'] ) ) {
173
			return (string) $this->values['text.content'];
174
		}
175
176
		return '';
177
	}
178
179
180
	/**
181
	 * Sets the content of the text item.
182
	 *
183
	 * @param string $text Content of the text item
184
	 * @return \Aimeos\MShop\Text\Item\Iface Text item for chaining method calls
185
	 */
186
	public function setContent( $text )
187
	{
188
		if( (string) $text !== $this->getContent() )
189
		{
190
			ini_set( 'mbstring.substitute_character', 'none' );
191
			$this->values['text.content'] = @mb_convert_encoding( (string) $text, 'UTF-8', 'UTF-8' );
192
			$this->setModified();
193
		}
194
195
		return $this;
196
	}
197
198
199
	/**
200
	 * Returns the name of the attribute item.
201
	 *
202
	 * @return string Label of the attribute item
203
	 */
204
	public function getLabel()
205
	{
206
		if( isset( $this->values['text.label'] ) ) {
207
			return (string) $this->values['text.label'];
208
		}
209
210
		return '';
211
	}
212
213
214
	/**
215
	 * Sets the new label of the attribute item.
216
	 *
217
	 * @param string $label Type label of the attribute item
218
	 * @return \Aimeos\MShop\Text\Item\Iface Text item for chaining method calls
219
	 */
220
	public function setLabel( $label )
221
	{
222
		if( (string) $label !== $this->getLabel() )
223
		{
224
			$this->values['text.label'] = (string) $label;
225
			$this->setModified();
226
		}
227
228
		return $this;
229
	}
230
231
232
	/**
233
	 * Returns the status of the text item.
234
	 *
235
	 * @return integer Status of the text item
236
	 */
237
	public function getStatus()
238
	{
239
		if( isset( $this->values['text.status'] ) ) {
240
			return (int) $this->values['text.status'];
241
		}
242
243
		return 0;
244
	}
245
246
247
	/**
248
	 * Sets the status of the text item.
249
	 *
250
	 * @param integer $status true/false for enabled/disabled
251
	 * @return \Aimeos\MShop\Text\Item\Iface Text item for chaining method calls
252
	 */
253
	public function setStatus( $status )
254
	{
255
		if( (int) $status !== $this->getStatus() )
256
		{
257
			$this->values['text.status'] = (int) $status;
258
			$this->setModified();
259
		}
260
261
		return $this;
262
	}
263
264
265
	/**
266
	 * Returns the item type
267
	 *
268
	 * @return string Item type, subtypes are separated by slashes
269
	 */
270
	public function getResourceType()
271
	{
272
		return 'text';
273
	}
274
275
276
	/**
277
	 * Tests if the item is available based on status, time, language and currency
278
	 *
279
	 * @return boolean True if available, false if not
280
	 */
281
	public function isAvailable()
282
	{
283
		return parent::isAvailable() && (bool) $this->getStatus()
284
			&& ( $this->values['languageid'] === null
285
			|| $this->getLanguageId() === null
286
			|| $this->getLanguageId() === $this->values['languageid'] );
287
	}
288
289
290
	/**
291
	 * Sets the item values from the given array.
292
	 *
293
	 * @param array $list Associative list of item keys and their values
294
	 * @return array Associative list of keys and their values that are unknown
295
	 */
296
	public function fromArray( array $list )
297
	{
298
		$unknown = [];
299
		$list = parent::fromArray( $list );
300
		unset( $list['text.type'], $list['text.typename'] );
301
302
		foreach( $list as $key => $value )
303
		{
304
			switch( $key )
305
			{
306
				case 'text.languageid': $this->setLanguageId( $value ); break;
307
				case 'text.typeid': $this->setTypeId( $value ); break;
308
				case 'text.label': $this->setLabel( $value ); break;
309
				case 'text.domain': $this->setDomain( $value ); break;
310
				case 'text.content': $this->setContent( $value ); break;
311
				case 'text.status': $this->setStatus( $value ); break;
312
				default: $unknown[$key] = $value;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
313
			}
314
		}
315
316
		return $unknown;
317
	}
318
319
320
	/**
321
	 * Returns the item values as array.
322
	 *
323
	 * @param boolean True to return private properties, false for public only
324
	 * @return array Associative list of item properties and their values
325
	 */
326
	public function toArray( $private = false )
327
	{
328
		$list = parent::toArray( $private );
329
330
		$list['text.languageid'] = $this->getLanguageId();
331
		$list['text.typename'] = $this->getTypeName();
332
		$list['text.type'] = $this->getType();
333
		$list['text.label'] = $this->getLabel();
334
		$list['text.domain'] = $this->getDomain();
335
		$list['text.content'] = $this->getContent();
336
		$list['text.status'] = $this->getStatus();
337
338
		if( $private === true ) {
339
			$list['text.typeid'] = $this->getTypeId();
340
		}
341
342
		return $list;
343
	}
344
}
345