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-2018 |
||
7 | * @package MShop |
||
8 | * @subpackage Common |
||
9 | */ |
||
10 | |||
11 | |||
12 | namespace Aimeos\MShop\Common\Item; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Common methods for all item objects. |
||
17 | * |
||
18 | * @package MShop |
||
19 | * @subpackage Common |
||
20 | */ |
||
21 | abstract class Base |
||
22 | extends \Aimeos\MW\Common\Item\Base |
||
23 | implements \Aimeos\MShop\Common\Item\Iface |
||
24 | { |
||
25 | private $bdata; |
||
26 | private $prefix; |
||
27 | private $available = true; |
||
28 | private $modified = false; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * Initializes the class properties. |
||
33 | * |
||
34 | * @param string $prefix Prefix for the keys returned by toArray() |
||
35 | * @param array $values Associative list of key/value pairs of the item properties |
||
36 | */ |
||
37 | public function __construct( $prefix, array $values ) |
||
38 | { |
||
39 | $this->prefix = (string) $prefix; |
||
40 | $this->bdata = $values; |
||
41 | } |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Creates a deep clone of all objects |
||
46 | */ |
||
47 | public function __clone() |
||
48 | { |
||
49 | } |
||
50 | |||
51 | |||
52 | /** |
||
53 | * Returns the item property for the given name |
||
54 | * |
||
55 | * @param string $name Name of the property |
||
56 | * @return mixed|null Property value or null if property is unknown |
||
57 | */ |
||
58 | public function __get( $name ) |
||
59 | { |
||
60 | if( isset( $this->bdata[$name] ) ) { |
||
61 | return $this->bdata[$name]; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Tests if the item property for the given name is available |
||
68 | * |
||
69 | * @param string $name Name of the property |
||
70 | * @return boolean True if the property exists, false if not |
||
71 | */ |
||
72 | public function __isset( $name ) |
||
73 | { |
||
74 | if( array_key_exists( $name, $this->bdata ) ) { |
||
75 | return true; |
||
76 | } |
||
77 | |||
78 | return false; |
||
79 | } |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Returns the ID of the item if available. |
||
84 | * |
||
85 | * @return string|null ID of the item |
||
86 | */ |
||
87 | public function getId() |
||
88 | { |
||
89 | $key = $this->prefix . 'id'; |
||
90 | |||
91 | if( isset( $this->bdata[$key] ) && $this->bdata[$key] != '' ) { |
||
92 | return (string) $this->bdata[$key]; |
||
93 | } |
||
94 | |||
95 | if( isset( $this->bdata['id'] ) && $this->bdata['id'] != '' ) { |
||
96 | return (string) $this->bdata['id']; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Sets the new ID of the item. |
||
103 | * |
||
104 | * @param string|null $id ID of the item |
||
105 | * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls |
||
106 | */ |
||
107 | public function setId( $id ) |
||
108 | { |
||
109 | $key = $this->prefix . 'id'; |
||
110 | |||
111 | if( ( $this->bdata[$key] = \Aimeos\MShop\Common\Item\Base::checkId( $this->getId(), $id ) ) === null ) { |
||
0 ignored issues
–
show
|
|||
112 | $this->modified = true; |
||
113 | } else { |
||
114 | $this->modified = false; |
||
115 | } |
||
116 | |||
117 | $this->bdata['id'] = $this->bdata[$key]; |
||
118 | return $this; |
||
119 | } |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Returns the site ID of the item. |
||
124 | * |
||
125 | * @return string|null Site ID or null if no site id is available |
||
126 | */ |
||
127 | public function getSiteId() |
||
128 | { |
||
129 | $key = $this->prefix . 'siteid'; |
||
130 | |||
131 | if( isset( $this->bdata[$key] ) ) { |
||
132 | return (string) $this->bdata[$key]; |
||
133 | } |
||
134 | |||
135 | if( isset( $this->bdata['siteid'] ) ) { |
||
136 | return (string) $this->bdata['siteid']; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | |||
141 | /** |
||
142 | * Returns modify date/time of the order coupon. |
||
143 | * |
||
144 | * @return string|null Modification time (YYYY-MM-DD HH:mm:ss) |
||
145 | */ |
||
146 | public function getTimeModified() |
||
147 | { |
||
148 | $key = $this->prefix . 'mtime'; |
||
149 | |||
150 | if( isset( $this->bdata[$key] ) ) { |
||
151 | return (string) $this->bdata[$key]; |
||
152 | } |
||
153 | |||
154 | if( isset( $this->bdata['mtime'] ) ) { |
||
155 | return (string) $this->bdata['mtime']; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | |||
160 | /** |
||
161 | * Returns the create date of the item. |
||
162 | * |
||
163 | * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format |
||
164 | */ |
||
165 | public function getTimeCreated() |
||
166 | { |
||
167 | $key = $this->prefix . 'ctime'; |
||
168 | |||
169 | if( isset( $this->bdata[$key] ) ) { |
||
170 | return (string) $this->bdata[$key]; |
||
171 | } |
||
172 | |||
173 | if( isset( $this->bdata['ctime'] ) ) { |
||
174 | return (string) $this->bdata['ctime']; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Returns the name of editor who created/modified the item at last. |
||
181 | * |
||
182 | * @return string Name of editor who created/modified the item at last |
||
183 | */ |
||
184 | public function getEditor() |
||
185 | { |
||
186 | $key = $this->prefix . 'editor'; |
||
187 | |||
188 | if( isset( $this->bdata[$key] ) ) { |
||
189 | return (string) $this->bdata[$key]; |
||
190 | } |
||
191 | |||
192 | if( isset( $this->bdata['editor'] ) ) { |
||
193 | return (string) $this->bdata['editor']; |
||
194 | } |
||
195 | |||
196 | return ''; |
||
197 | } |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Tests if the item is available based on status, time, language and currency |
||
202 | * |
||
203 | * @return boolean True if available, false if not |
||
204 | */ |
||
205 | public function isAvailable() |
||
206 | { |
||
207 | return $this->available; |
||
208 | } |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Sets the general availability of the item |
||
213 | * |
||
214 | * @return boolean $value True if available, false if not |
||
215 | */ |
||
216 | public function setAvailable( $value ) |
||
217 | { |
||
218 | $this->available = (bool) $value; |
||
219 | } |
||
220 | |||
221 | |||
222 | /** |
||
223 | * Tests if this Item object was modified. |
||
224 | * |
||
225 | * @return boolean True if modified, false if not |
||
226 | */ |
||
227 | public function isModified() |
||
228 | { |
||
229 | return $this->modified; |
||
230 | } |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Sets the modified flag of the object. |
||
235 | * |
||
236 | * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls |
||
237 | */ |
||
238 | public function setModified() |
||
239 | { |
||
240 | $this->modified = true; |
||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | |||
245 | /** |
||
246 | * Sets the item values from the given array and removes that entries from the list |
||
247 | * |
||
248 | * @param array $list Associative list of item keys and their values |
||
249 | * @param boolean True to set private properties too, false for public only |
||
250 | * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls |
||
251 | */ |
||
252 | public function fromArray( array &$list, $private = false ) |
||
253 | { |
||
254 | $item = $this; |
||
255 | |||
256 | if( $private && array_key_exists( $this->prefix . 'id', $list ) ) |
||
257 | { |
||
258 | $item = $item->setId( $list[$this->prefix . 'id'] ); |
||
259 | unset( $list[$this->prefix . 'id'] ); |
||
260 | } |
||
261 | |||
262 | return $item; |
||
263 | } |
||
264 | |||
265 | |||
266 | /** |
||
267 | * Returns the item values as array. |
||
268 | * |
||
269 | * @param boolean True to return private properties, false for public only |
||
270 | * @return array Associative list of item properties and their values |
||
271 | */ |
||
272 | public function toArray( $private = false ) |
||
273 | { |
||
274 | $list = [$this->prefix . 'id' => $this->getId()]; |
||
275 | |||
276 | if( $private === true ) |
||
277 | { |
||
278 | $list[$this->prefix . 'siteid'] = $this->getSiteId(); |
||
279 | $list[$this->prefix . 'ctime'] = $this->getTimeCreated(); |
||
280 | $list[$this->prefix . 'mtime'] = $this->getTimeModified(); |
||
281 | $list[$this->prefix . 'editor'] = $this->getEditor(); |
||
282 | } |
||
283 | |||
284 | return $list; |
||
285 | } |
||
286 | |||
287 | |||
288 | /** |
||
289 | * Checks if the new ID is valid for the item. |
||
290 | * |
||
291 | * @param string $old Current ID of the item |
||
292 | * @param string $new New ID which should be set in the item |
||
293 | * @return string Value of the new ID |
||
294 | * @throws \Aimeos\MShop\Common\Exception if the ID is not null or not the same as the old one |
||
295 | */ |
||
296 | public static function checkId( $old, $new ) |
||
297 | { |
||
298 | if( $new != null && $old != null && $old != $new ) { |
||
299 | throw new \Aimeos\MShop\Exception( sprintf( 'New ID "%1$s" for item differs from old ID "%2$s"', $new, $old ) ); |
||
300 | } |
||
301 | |||
302 | return $new; |
||
303 | } |
||
304 | |||
305 | |||
306 | /** |
||
307 | * Tests if the date parameter represents an ISO format. |
||
308 | * |
||
309 | * @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format or null |
||
310 | * @return string|null Clean date or null for no date |
||
311 | * @throws \Aimeos\MShop\Exception If the date is invalid |
||
312 | */ |
||
313 | protected function checkDateFormat( $date ) |
||
314 | { |
||
315 | $regex = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9](( |T)[0-2][0-9]:[0-5][0-9](:[0-5][0-9])?)?$/'; |
||
316 | |||
317 | if( $date != null ) |
||
318 | { |
||
319 | if( preg_match( $regex, (string) $date ) !== 1 ) { |
||
320 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid characters in date "%1$s". ISO format "YYYY-MM-DD hh:mm:ss" expected.', $date ) ); |
||
321 | } |
||
322 | |||
323 | return str_replace( 'T', ' ', (string) $date ); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | |||
328 | /** |
||
329 | * Tests if the code is valid. |
||
330 | * |
||
331 | * @param string $code New code for an item |
||
332 | * @return string Item code |
||
333 | * @throws \Aimeos\MShop\Exception If the code is invalid |
||
334 | */ |
||
335 | protected function checkCode( $code ) |
||
336 | { |
||
337 | if( strlen( $code ) > 32 ) { |
||
338 | throw new \Aimeos\MShop\Exception( sprintf( 'Code "%1$s" must not be longer than 32 characters', $code ) ); |
||
339 | } |
||
340 | |||
341 | return (string) $code; |
||
342 | } |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Tests if the country ID parameter represents an ISO country format. |
||
347 | * |
||
348 | * @param string|null $countryid Two letter ISO country format, e.g. DE |
||
349 | * @param boolean $null True if null is allowed, false if not |
||
350 | * @return string|null Two letter ISO country ID or null for no country |
||
351 | * @throws \Aimeos\MShop\Exception If the country ID is invalid |
||
352 | */ |
||
353 | protected function checkCountryId( $countryid, $null = true ) |
||
354 | { |
||
355 | if( $null === false && $countryid == null ) { |
||
356 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO country code "%1$s"', '<null>' ) ); |
||
357 | } |
||
358 | |||
359 | if( $countryid != null ) |
||
360 | { |
||
361 | if( preg_match( '/^[A-Za-z]{2}$/', $countryid ) !== 1 ) { |
||
362 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO country code "%1$s"', $countryid ) ); |
||
363 | } |
||
364 | |||
365 | return strtoupper( $countryid ); |
||
366 | } |
||
367 | } |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Tests if the currency ID parameter represents an ISO currency format. |
||
372 | * |
||
373 | * @param string|null $currencyid Three letter ISO currency format, e.g. EUR |
||
374 | * @param boolean $null True if null is allowed, false if not |
||
375 | * @return string|null Three letter ISO currency ID or null for no currency |
||
376 | * @throws \Aimeos\MShop\Exception If the currency ID is invalid |
||
377 | */ |
||
378 | protected function checkCurrencyId( $currencyid, $null = true ) |
||
379 | { |
||
380 | if( $null === false && $currencyid == null ) { |
||
381 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO currency code "%1$s"', '<null>' ) ); |
||
382 | } |
||
383 | |||
384 | if( $currencyid != null ) |
||
385 | { |
||
386 | if( preg_match( '/^[A-Z]{3}$/', $currencyid ) !== 1 ) { |
||
387 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO currency code "%1$s"', $currencyid ) ); |
||
388 | } |
||
389 | |||
390 | return strtoupper( $currencyid ); |
||
391 | } |
||
392 | } |
||
393 | |||
394 | |||
395 | /** |
||
396 | * Tests if the language ID parameter represents an ISO language format. |
||
397 | * |
||
398 | * @param string|null $langid ISO language format, e.g. de or de_DE |
||
399 | * @param boolean $null True if null is allowed, false if not |
||
400 | * @return string|null ISO language ID or null for no language |
||
401 | * @throws \Aimeos\MShop\Exception If the language ID is invalid |
||
402 | */ |
||
403 | protected function checkLanguageId( $langid, $null = true ) |
||
404 | { |
||
405 | if( $null === false && $langid == null ) { |
||
406 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO language code "%1$s"', '<null>' ) ); |
||
407 | } |
||
408 | |||
409 | if( $langid != null ) |
||
410 | { |
||
411 | if( preg_match( '/^[a-zA-Z]{2}(_[a-zA-Z]{2})?$/', $langid ) !== 1 ) { |
||
412 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO language code "%1$s"', $langid ) ); |
||
413 | } |
||
414 | |||
415 | $parts = explode( '_', $langid ); |
||
416 | $parts[0] = strtolower( $parts[0] ); |
||
417 | |||
418 | if( isset( $parts[1] ) ) { |
||
419 | $parts[1] = strtoupper( $parts[1] ); |
||
420 | } |
||
421 | |||
422 | return implode( '_', $parts ); |
||
423 | } |
||
424 | } |
||
425 | |||
426 | |||
427 | /** |
||
428 | * Returns the raw value list. |
||
429 | * |
||
430 | * @return array Associative list of key/value pairs |
||
431 | */ |
||
432 | protected function getRawValues() |
||
433 | { |
||
434 | return $this->bdata; |
||
435 | } |
||
436 | } |
||
437 |
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.