Passed
Push — master ( 316811...ac49e1 )
by Aimeos
10:42
created

Standard::getSiteCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MShop
8
 * @subpackage Locale
9
 */
10
11
12
namespace Aimeos\MShop\Locale\Item;
13
14
use \Aimeos\MShop\Locale\Manager\Base as Locale;
15
16
17
/**
18
 * Common locale class containing the site, language and currency information.
19
 *
20
 * @package MShop
21
 * @subpackage Locale
22
 */
23
class Standard
24
	extends \Aimeos\MShop\Common\Item\Base
25
	implements \Aimeos\MShop\Locale\Item\Iface
26
{
27
	private $siteItem;
28
	private $sites;
29
30
31
	/**
32
	 * Initializes the object with the locale values.
33
	 *
34
	 * @param array $values Values to be set on initialisation
35
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface|null $siteItem Site item object
36
	 * @param string[] $sitePath List of site IDs up to the root site item
37
	 * @param string[]|string Site ID prefix or list of site IDs
38
	 */
39
	public function __construct( array $values = [], \Aimeos\MShop\Locale\Item\Site\Iface $siteItem = null, array $sites = [] )
40
	{
41
		parent::__construct( 'locale.', $values );
42
43
		$this->siteItem = $siteItem;
44
		$this->sites = $sites;
45
	}
46
47
48
	/**
49
	 * Clones internal objects of the locale item.
50
	 */
51
	public function __clone()
52
	{
53
		$this->siteItem = ( isset( $this->siteItem ) ? clone $this->siteItem : null );
54
	}
55
56
57
	/**
58
	 * Returns the site code of the item.
59
	 *
60
	 * @return string|null Site code or NULL if not available
61
	 */
62
	public function getSiteCode() : ?string
63
	{
64
		return $this->get( 'locale.sitecode' );
65
	}
66
67
68
	/**
69
	 * Returns the site item object.
70
	 *
71
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface Site item object
72
	 * @throws \Aimeos\MShop\Locale\Exception if site object isn't available
73
	 */
74
	public function getSiteItem() : \Aimeos\MShop\Locale\Item\Site\Iface
75
	{
76
		if( $this->siteItem === null ) {
77
			throw new \Aimeos\MShop\Locale\Exception( 'No site item available' );
78
		}
79
80
		return $this->siteItem;
81
	}
82
83
84
	/**
85
	 * Returns the list site IDs up to the root site item.
86
	 *
87
	 * @return array List of site IDs
88
	 */
89
	public function getSitePath() : array
90
	{
91
		return (array) ( $this->sites[Locale::SITE_PATH] ?? ( $this->sites[Locale::SITE_ONE] ?? [] ) );
92
	}
93
94
95
	/**
96
	 * Returns the site IDs for the locale site constants.
97
	 *
98
	 * @param int $level Site level constant from \Aimeos\MShop\Locale\Manager\Base
99
	 * @return array|string Associative list of site constant as key and sites as values or site ID
100
	 */
101
	public function getSites( int $level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL )
102
	{
103
		if( $level === Locale::SITE_ALL ) {
104
			return $this->sites;
105
		}
106
107
		return $this->sites[$level] ?? ( $this->sites[Locale::SITE_ONE] ?? [] );
108
	}
109
110
111
	/**
112
	 * Returns the Site ID of the item.
113
	 *
114
	 * @return string Site ID (or null for global site)
115
	 */
116
	public function getSiteId() : string
117
	{
118
		return $this->get( 'locale.siteid', '' );
119
	}
120
121
122
	/**
123
	 * Sets the identifier of the shop instance.
124
	 *
125
	 * @param string $id ID of the shop instance
126
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item for chaining method calls
127
	 */
128
	public function setSiteId( string $id ) : \Aimeos\MShop\Locale\Item\Iface
129
	{
130
		return $this->set( 'locale.siteid', (string) $id );
131
	}
132
133
134
	/**
135
	 * Returns the ISO language code.
136
	 *
137
	 * @return string|null ISO language code (e.g. de or de_DE)
138
	 */
139
	public function getLanguageId() : ?string
140
	{
141
		return $this->get( 'locale.languageid' );
142
	}
143
144
145
	/**
146
	 * Sets the ISO language code.
147
	 *
148
	 * @param string|null $id ISO language code (e.g. de or de_DE)
149
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item for chaining method calls
150
	 * @throws \Aimeos\MShop\Exception If the language ID is invalid
151
	 */
152
	public function setLanguageId( ?string $id ) : \Aimeos\MShop\Locale\Item\Iface
153
	{
154
		return $this->set( 'locale.languageid', $this->checkLanguageId( $id ) );
155
	}
156
157
158
	/**
159
	 * Returns the currency ID.
160
	 *
161
	 * @return string|null Three letter ISO currency code (e.g. EUR)
162
	 */
163
	public function getCurrencyId() : ?string
164
	{
165
		return $this->get( 'locale.currencyid' );
166
	}
167
168
169
	/**
170
	 * Sets the currency ID.
171
	 *
172
	 * @param string|null $currencyid Three letter ISO currency code (e.g. EUR)
173
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item for chaining method calls
174
	 * @throws \Aimeos\MShop\Exception If the currency ID is invalid
175
	 */
176
	public function setCurrencyId( ?string $currencyid ) : \Aimeos\MShop\Locale\Item\Iface
177
	{
178
		return $this->set( 'locale.currencyid', $this->checkCurrencyId( $currencyid ) );
179
	}
180
181
182
	/**
183
	 * Returns the position of the item.
184
	 *
185
	 * @return int Position of the item relative to the other items
186
	 */
187
	public function getPosition() : int
188
	{
189
		return (int) $this->get( 'locale.position', 0 );
190
	}
191
192
193
	/**
194
	 * Sets the position of the item.
195
	 *
196
	 * @param int $pos Position of the item
197
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item for chaining method calls
198
	 */
199
	public function setPosition( int $pos ) : \Aimeos\MShop\Common\Item\Iface
200
	{
201
		return $this->set( 'locale.position', $pos );
202
	}
203
204
205
	/**
206
	 * Returns the status property of the locale item
207
	 *
208
	 * @return int Returns the status of the locale item
209
	 */
210
	public function getStatus() : int
211
	{
212
		return $this->get( 'locale.status', 1 );
213
	}
214
215
216
	/**
217
	 * Sets the status property
218
	 *
219
	 * @param int $status The status of the locale item
220
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item for chaining method calls
221
	 */
222
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
223
	{
224
		return $this->set( 'locale.status', $status );
225
	}
226
227
228
	/**
229
	 * Returns the item type
230
	 *
231
	 * @return string Item type, subtypes are separated by slashes
232
	 */
233
	public function getResourceType() : string
234
	{
235
		return 'locale';
236
	}
237
238
239
	/**
240
	 * Tests if the item is available based on status, time, language and currency
241
	 *
242
	 * @return bool True if available, false if not
243
	 */
244
	public function isAvailable() : bool
245
	{
246
		return parent::isAvailable() && $this->getStatus() > 0;
247
	}
248
249
250
	/*
251
	 * Sets the item values from the given array and removes that entries from the list
252
	 *
253
	 * @param array &$list Associative list of item keys and their values
254
	 * @param bool True to set private properties too, false for public only
255
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item for chaining method calls
256
	 */
257
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
258
	{
259
		$item = parent::fromArray( $list, $private );
260
261
		foreach( $list as $key => $value )
262
		{
263
			switch( $key )
264
			{
265
				case 'locale.siteid': $item = $item->setSiteId( $value ); break;
266
				case 'locale.languageid': $item = $item->setLanguageId( $value ); break;
267
				case 'locale.currencyid': $item = $item->setCurrencyId( $value ); break;
268
				case 'locale.position': $item = $item->setPosition( (int) $value ); break;
269
				case 'locale.status': $item = $item->setStatus( (int) $value ); break;
270
				default: continue 2;
271
			}
272
273
			unset( $list[$key] );
274
		}
275
276
		return $item;
277
	}
278
279
280
	/**
281
	 * Returns the item values as array.
282
	 *
283
	 * @param bool True to return private properties, false for public only
284
	 * @return array Associative list of item properties and their values
285
	 */
286
	public function toArray( bool $private = false ) : array
287
	{
288
		$list = parent::toArray( $private );
289
290
		$list['locale.currencyid'] = $this->getCurrencyId();
291
		$list['locale.languageid'] = $this->getLanguageId();
292
		$list['locale.position'] = $this->getPosition();
293
		$list['locale.sitecode'] = $this->getSiteCode();
294
		$list['locale.siteid'] = $this->getSiteId();
295
		$list['locale.status'] = $this->getStatus();
296
297
		return $list;
298
	}
299
300
}
301