Standard::getFileSystem()   A
last analyzed

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 Aimeos (aimeos.org), 2015-2025
6
 * @package MShop
7
 * @subpackage Media
8
 */
9
10
11
namespace Aimeos\MShop\Media\Item;
12
13
use \Aimeos\MShop\Common\Item\ListsRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\ListsRef was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use \Aimeos\MShop\Common\Item\PropertyRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\PropertyRef was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use \Aimeos\MShop\Common\Item\TypeRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\TypeRef was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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, TypeRef\Traits {
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\ListsRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \Aimeos\MShop\Common\Item\PropertyRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \Aimeos\MShop\Common\Item\TypeRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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