Completed
Push — master ( 110887...ff6c7f )
by Aimeos
10:07
created

src/MShop/Locale/Item/Currency/Standard.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Locale
9
 */
10
11
12
namespace Aimeos\MShop\Locale\Item\Currency;
13
14
15
/**
16
 * Default implementation of a currency item.
17
 *
18
 * @package MShop
19
 * @subpackage Locale
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\Base
23
	implements \Aimeos\MShop\Locale\Item\Currency\Iface
24
{
25
	private $modified = false;
26
	private $values;
27
28
29
	/**
30
	 * Initializes the currency object object.
31
	 *
32
	 * @param array $values Possible params to be set on initialization
33
	 */
34
	public function __construct( array $values = [] )
35
	{
36
		parent::__construct( 'locale.currency.', $values );
37
38
		$this->values = $values;
39
40
		if( isset( $values['locale.currency.id'] ) ) {
41
			$this->setId( $values['locale.currency.id'] );
42
		}
43
	}
44
45
46
	/**
47
	 * Returns the ID of the currency.
48
	 *
49
	 * @return string|null ID of the currency
50
	 */
51
	public function getId()
52
	{
53
		if( isset( $this->values['locale.currency.id'] ) ) {
54
			return (string) $this->values['locale.currency.id'];
55
		}
56
57
		return null;
58
	}
59
60
61
	/**
62
	 * Sets the ID of the currency.
63
	 *
64
	 * @param string $key ID of the currency
65
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface Locale currency item for chaining method calls
66
	 */
67
	public function setId( $key )
68
	{
69
		if( $key !== null && $key !== '' )
70
		{
71
			$this->setCode( $key );
72
			$this->values['locale.currency.id'] = $this->values['locale.currency.code'];
73
			$this->modified = false;
74
		}
75
		else
76
		{
77
			$this->values['locale.currency.id'] = null;
78
			$this->modified = true;
79
		}
80
81
		return $this;
82
	}
83
84
85
	/**
86
	 * Returns the code of the currency.
87
	 *
88
	 * @return string Code of the currency
89
	 */
90
	public function getCode()
91
	{
92
		if( isset( $this->values['locale.currency.code'] ) ) {
93
			return (string) $this->values['locale.currency.code'];
94
		}
95
96
		return '';
97
	}
98
99
100
	/**
101
	 * Sets the code of the currency.
102
	 *
103
	 * @param string $key Code of the currency
104
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface Locale currency item for chaining method calls
105
	 */
106
	public function setCode( $key )
107
	{
108
		if( strlen( $key ) != 3 || ctype_alpha( $key ) === false ) {
109
			throw new \Aimeos\MShop\Locale\Exception( sprintf( 'Invalid characters in ISO currency code "%1$s"', $key ) );
110
		}
111
112
		if( (string) $key !== $this->getCode() )
113
		{
114
			$this->values['locale.currency.code'] = strtoupper( (string) $key );
115
			$this->modified = true;
116
		}
117
118
		return $this;
119
	}
120
121
122
	/**
123
	 * Returns the label or symbol of the currency.
124
	 *
125
	 * @return string Label or symbol of the currency
126
	 */
127
	public function getLabel()
128
	{
129
		if( isset( $this->values['locale.currency.label'] ) ) {
130
			return (string) $this->values['locale.currency.label'];
131
		}
132
133
		return '';
134
	}
135
136
137
	/**
138
	 * Sets the label or symbol of the currency.
139
	 *
140
	 * @param string $label Label or symbol of the currency
141
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface Locale currency item for chaining method calls
142
	 */
143
	public function setLabel( $label )
144
	{
145
		if( (string) $label !== $this->getLabel() )
146
		{
147
			$this->values['locale.currency.label'] = (string) $label;
148
			$this->setModified();
149
		}
150
151
		return $this;
152
	}
153
154
155
	/**
156
	 * Returns the status of the item.
157
	 *
158
	 * @return integer Status of the item
159
	 */
160
	public function getStatus()
161
	{
162
		if( isset( $this->values['locale.currency.status'] ) ) {
163
			return (int) $this->values['locale.currency.status'];
164
		}
165
166
		return 0;
167
	}
168
169
170
	/**
171
	 * Sets the status of the item.
172
	 *
173
	 * @param integer $status Status of the item
174
	 * @return \Aimeos\MShop\Locale\Item\Currency\Iface Locale currency item for chaining method calls
175
	 */
176
	public function setStatus( $status )
177
	{
178
		if( (int) $status !== $this->getStatus() )
179
		{
180
			$this->values['locale.currency.status'] = (int) $status;
181
			$this->setModified();
182
		}
183
184
		return $this;
185
	}
186
187
188
	/**
189
	 * Returns the item type
190
	 *
191
	 * @return string Item type, subtypes are separated by slashes
192
	 */
193
	public function getResourceType()
194
	{
195
		return 'locale/currency';
196
	}
197
198
199
	/**
200
	 * Tests if the item is available based on status, time, language and currency
201
	 *
202
	 * @return boolean True if available, false if not
203
	 */
204
	public function isAvailable()
205
	{
206
		return (bool) $this->getStatus();
207
	}
208
209
210
	/**
211
	 * Sets the item values from the given array.
212
	 *
213
	 * @param array $list Associative list of item keys and their values
214
	 * @return array Associative list of keys and their values that are unknown
215
	 */
216
	public function fromArray( array $list )
217
	{
218
		$unknown = [];
219
		$list = parent::fromArray( $list );
220
221
		foreach( $list as $key => $value )
222
		{
223
			switch( $key )
224
			{
225
				case 'locale.currency.id': $this->setId( $value ); break;
226
				case 'locale.currency.code': $this->setCode( $value ); break;
227
				case 'locale.currency.label': $this->setLabel( $value ); break;
228
				case 'locale.currency.status': $this->setStatus( $value ); break;
229
				default: $unknown[$key] = $value;
0 ignored issues
show
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...
230
			}
231
		}
232
233
		return $unknown;
234
	}
235
236
237
	/**
238
	 * Returns the item values as array.
239
	 *
240
	 * @param boolean True to return private properties, false for public only
241
	 * @return array Associative list of item properties and their values
242
	 */
243
	public function toArray( $private = false )
244
	{
245
		$list = parent::toArray( $private );
246
247
		$list['locale.currency.id'] = $this->getId();
248
		$list['locale.currency.code'] = $this->getCode();
249
		$list['locale.currency.label'] = $this->getLabel();
250
		$list['locale.currency.status'] = $this->getStatus();
251
252
		return $list;
253
	}
254
255
256
	/**
257
	 * Tests if the object was modified.
258
	 *
259
	 * @return boolean True if modified, false if not
260
	 */
261
	public function isModified()
262
	{
263
		return $this->modified || parent::isModified();
264
	}
265
266
}
267