Passed
Push — master ( ce725d...c4b0c0 )
by Aimeos
06:00
created

Standard::getFileSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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-2021
7
 * @package MShop
8
 * @subpackage Media
9
 */
10
11
12
namespace Aimeos\MShop\Media\Item;
13
14
use \Aimeos\MShop\Common\Item\ListsRef;
15
use \Aimeos\MShop\Common\Item\PropertyRef;
16
17
18
/**
19
 * Default implementation of the media item.
20
 *
21
 * @package MShop
22
 * @subpackage Media
23
 */
24
class Standard
25
	extends \Aimeos\MShop\Common\Item\Base
26
	implements \Aimeos\MShop\Media\Item\Iface
27
{
28
	use ListsRef\Traits, PropertyRef\Traits {
29
		PropertyRef\Traits::__clone as __cloneProperty;
30
		ListsRef\Traits::__clone insteadof PropertyRef\Traits;
31
		ListsRef\Traits::__clone as __cloneList;
32
		ListsRef\Traits::getName as getNameList;
33
	}
34
35
36
	private $langid;
37
38
39
	/**
40
	 * Initializes the media item object.
41
	 *
42
	 * @param array $values Initial values of the media item
43
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
44
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
45
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
46
	 */
47
	public function __construct( array $values = [], array $listItems = [],
48
		array $refItems = [], array $propItems = [] )
49
	{
50
		if( isset( $values['media.preview'] ) && !isset( $values['media.previews'] ) ) {
51
			$values['media.previews'] = [1 => $values['media.preview']];
52
		}
53
54
		parent::__construct( 'media.', $values );
55
56
		$this->langid = ( isset( $values['.languageid'] ) ? $values['.languageid'] : null );
57
		$this->initListItems( $listItems, $refItems );
58
		$this->initPropertyItems( $propItems );
59
	}
60
61
62
	/**
63
	 * Creates a deep clone of all objects
64
	 */
65
	public function __clone()
66
	{
67
		parent::__clone();
68
		$this->__cloneList();
69
		$this->__cloneProperty();
70
	}
71
72
73
	/**
74
	 * Returns the name of the file system the referenced file is stored.
75
	 *
76
	 * @return string Name of the file system
77
	 */
78
	public function getFileSystem() : string
79
	{
80
		return $this->get( 'media.filesystem', 'fs-media' );
81
	}
82
83
84
	/**
85
	 * Sets the name of the file system the referenced file is stored.
86
	 *
87
	 * @param string $value Name of the file system
88
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
89
	 */
90
	public function setFileSystem( string $value ) : \Aimeos\MShop\Media\Item\Iface
91
	{
92
		return $this->set( 'media.filesystem', $value );
93
	}
94
95
96
	/**
97
	 * Returns the ISO language code.
98
	 *
99
	 * @return string|null ISO language code (e.g. de or de_DE)
100
	 */
101
	public function getLanguageId() : ?string
102
	{
103
		return $this->get( 'media.languageid' );
104
	}
105
106
107
	/**
108
	 * Sets the ISO language code.
109
	 *
110
	 * @param string|null $id ISO language code (e.g. de or de_DE)
111
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
112
	 * @throws \Aimeos\MShop\Exception If the language ID is invalid
113
	 */
114
	public function setLanguageId( ?string $id ) : \Aimeos\MShop\Media\Item\Iface
115
	{
116
		return $this->set( 'media.languageid', $this->checkLanguageId( $id ) );
117
	}
118
119
120
	/**
121
	 * Returns the type code of the media item.
122
	 *
123
	 * @return string|null Type code of the media item
124
	 */
125
	public function getType() : ?string
126
	{
127
		return $this->get( 'media.type', 'default' );
128
	}
129
130
131
	/**
132
	 * Sets the new type of the media.
133
	 *
134
	 * @param string $type Type of the media
135
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
136
	 */
137
	public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface
138
	{
139
		return $this->set( 'media.type', $this->checkCode( $type ) );
140
	}
141
142
143
	/**
144
	 * Returns the domain of the media item, if available.
145
	 *
146
	 * @return string Domain the media item belongs to
147
	 */
148
	public function getDomain() : string
149
	{
150
		return (string) $this->get( 'media.domain', '' );
151
	}
152
153
154
	/**
155
	 * Sets the domain of the media item.
156
	 *
157
	 * @param string $domain Domain of media item
158
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
159
	 */
160
	public function setDomain( string $domain ) : \Aimeos\MShop\Common\Item\Iface
161
	{
162
		return $this->set( 'media.domain', (string) $domain );
163
	}
164
165
166
	/**
167
	 * Returns the label of the media item.
168
	 *
169
	 * @return string Label of the media item
170
	 */
171
	public function getLabel() : string
172
	{
173
		return (string) $this->get( 'media.label', '' );
174
	}
175
176
177
	/**
178
	 * Sets the new label of the media item.
179
	 *
180
	 * @param string $label Label of the media item
181
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
182
	 */
183
	public function setLabel( ?string $label ) : \Aimeos\MShop\Media\Item\Iface
184
	{
185
		return $this->set( 'media.label', (string) $label );
186
	}
187
188
189
	/**
190
	 * Returns the status of the media item.
191
	 *
192
	 * @return int Status of the item
193
	 */
194
	public function getStatus() : int
195
	{
196
		return (int) $this->get( 'media.status', 1 );
197
	}
198
199
200
	/**
201
	 * Sets the new status of the media item.
202
	 *
203
	 * @param int $status Status of the item
204
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
205
	 */
206
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
207
	{
208
		return $this->set( 'media.status', $status );
209
	}
210
211
212
	/**
213
	 * Returns the mime type of the media item.
214
	 *
215
	 * @return string Mime type of the media item
216
	 */
217
	public function getMimeType() : string
218
	{
219
		return (string) $this->get( 'media.mimetype', '' );
220
	}
221
222
223
	/**
224
	 * Sets the new mime type of the media.
225
	 *
226
	 * @param string $mimetype Mime type of the media item
227
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
228
	 */
229
	public function setMimeType( string $mimetype ) : \Aimeos\MShop\Media\Item\Iface
230
	{
231
		if( preg_match( '/^[a-z\-]+\/[a-zA-Z0-9\.\-\+]+$/', $mimetype ) !== 1 ) {
232
			throw new \Aimeos\MShop\Media\Exception( sprintf( 'Invalid mime type "%1$s"', $mimetype ) );
233
		}
234
235
		return $this->set( 'media.mimetype', (string) $mimetype );
236
	}
237
238
239
	/**
240
	 * Returns the url of the media item.
241
	 *
242
	 * @return string URL of the media file
243
	 */
244
	public function getUrl() : string
245
	{
246
		return (string) $this->get( 'media.url', '' );
247
	}
248
249
250
	/**
251
	 * Sets the new url of the media item.
252
	 *
253
	 * @param string|null $url URL of the media file
254
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
255
	 */
256
	public function setUrl( ?string $url ) : \Aimeos\MShop\Media\Item\Iface
257
	{
258
		return $this->set( 'media.url', (string) $url );
259
	}
260
261
262
	/**
263
	 * Returns the preview url of the media item.
264
	 *
265
	 * @param bool|int $size TRUE for the largest image, FALSE for the smallest or a concrete image width
266
	 * @return string Preview URL of the media file
267
	 */
268
	public function getPreview( $width = false ) : string
269
	{
270
		if( ( $list = (array) $this->get( 'media.previews', [] ) ) === [] ) {
271
			return '';
272
		}
273
274
		if( $width === false ) {
275
			return (string) reset( $list );
276
		} elseif( $width === true ) {
277
			return (string) end( $list );
278
		} elseif( isset( $list[$width] ) ) {
279
			return (string) $list[$width];
280
		}
281
282
		$before = $after = [];
283
284
		foreach( $list as $idx => $path )
285
		{
286
			if( $idx < $width ) {
287
				$before[$idx] = $path;
288
			} else {
289
				$after[$idx] = $path;
290
			}
291
		}
292
293
		if( ( $path = array_shift( $after ) ) !== null ) {
294
			return (string) $path;
295
		} elseif( ( $path = array_pop( $before ) ) !== null ) {
296
			return (string) $path;
297
		}
298
299
		return '';
300
	}
301
302
303
	/**
304
	 * Returns all preview urls for images of different sizes.
305
	 *
306
	 * @return array Associative list of widths in pixels as keys and urls as values
307
	 */
308
	public function getPreviews() : array
309
	{
310
		return (array) $this->get( 'media.previews', [] );
311
	}
312
313
314
	/**
315
	 * Sets the new preview url of the media item.
316
	 *
317
	 * @param string $url Preview URL of the media file
318
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
319
	 */
320
	public function setPreview( string $url ) : \Aimeos\MShop\Media\Item\Iface
321
	{
322
		return $this->set( 'media.previews', [1 => $url] );
323
	}
324
325
326
	/**
327
	 * Sets the new preview urls for images of different sizes.
328
	 *
329
	 * @param array $url List of preview URLs with widths of the media file in pixels as keys
330
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
331
	 */
332
	public function setPreviews( array $urls ) : \Aimeos\MShop\Media\Item\Iface
333
	{
334
		return $this->set( 'media.previews', $urls );
335
	}
336
337
338
	/**
339
	 * Returns the localized text type of the item or the internal label if no name is available.
340
	 *
341
	 * @param string $type Text type to be returned
342
	 * @param string|null $langId Two letter ISO Language code of the text
343
	 * @return string Specified text type or label of the item
344
	 */
345
	public function getName( string $type = 'name', string $langId = null ) : string
346
	{
347
		foreach( $this->getPropertyItems( $type ) as $propItem )
348
		{
349
			if( $propItem->getLanguageId() === $langId || $langId === null ) {
350
				return $propItem->getValue();
351
			}
352
		}
353
354
		return $this->getNameList( $type );
355
	}
356
357
358
	/**
359
	 * Returns the item type
360
	 *
361
	 * @return string Item type, subtypes are separated by slashes
362
	 */
363
	public function getResourceType() : string
364
	{
365
		return 'media';
366
	}
367
368
369
	/**
370
	 * Tests if the item is available based on status, time, language and currency
371
	 *
372
	 * @return bool True if available, false if not
373
	 */
374
	public function isAvailable() : bool
375
	{
376
		return parent::isAvailable() && $this->getStatus() > 0
377
			&& ( $this->langid === null || $this->getLanguageId() === null
378
			|| $this->getLanguageId() === $this->langid );
379
	}
380
381
382
	/*
383
	 * Sets the item values from the given array and removes that entries from the list
384
	 *
385
	 * @param array &$list Associative list of item keys and their values
386
	 * @param bool True to set private properties too, false for public only
387
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
388
	 */
389
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
390
	{
391
		$item = parent::fromArray( $list, $private );
392
393
		foreach( $list as $key => $value )
394
		{
395
			switch( $key )
396
			{
397
				case 'media.filesystem': $item = $item->setFileSystem( $value ); break;
398
				case 'media.domain': $item = $item->setDomain( $value ); break;
399
				case 'media.label': $item = $item->setLabel( $value ); break;
400
				case 'media.languageid': $item = $item->setLanguageId( $value ); break;
401
				case 'media.mimetype': $item = $item->setMimeType( $value ); break;
402
				case 'media.type': $item = $item->setType( $value ); break;
403
				case 'media.url': $item = $item->setUrl( $value ); break;
404
				case 'media.preview': $item = $item->setPreview( $value ); break;
405
				case 'media.previews': $item = $item->setPreviews( (array) $value ); break;
406
				case 'media.status': $item = $item->setStatus( (int) $value ); break;
407
				default: continue 2;
408
			}
409
410
			unset( $list[$key] );
411
		}
412
413
		return $item;
414
	}
415
416
417
	/**
418
	 * Returns the item values as array.
419
	 *
420
	 * @param bool True to return private properties, false for public only
421
	 * @return array Associative list of item properties and their values
422
	 */
423
	public function toArray( bool $private = false ) : array
424
	{
425
		$list = parent::toArray( $private );
426
427
		$list['media.filesystem'] = $this->getFileSystem();
428
		$list['media.domain'] = $this->getDomain();
429
		$list['media.label'] = $this->getLabel();
430
		$list['media.languageid'] = $this->getLanguageId();
431
		$list['media.mimetype'] = $this->getMimeType();
432
		$list['media.type'] = $this->getType();
433
		$list['media.preview'] = $this->getPreview();
434
		$list['media.previews'] = $this->getPreviews();
435
		$list['media.url'] = $this->getUrl();
436
		$list['media.status'] = $this->getStatus();
437
438
		return $list;
439
	}
440
}
441