Completed
Push — master ( 4dbc3b...796c61 )
by Aimeos
03:24
created

Standard::transform()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
nc 4
nop 1
dl 0
loc 22
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2020
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\MW\View\Helper\Image;
12
13
14
/**
15
 * View helper class for creating an HTML image tag
16
 *
17
 * @package MW
18
 * @subpackage View
19
 */
20
class Standard
21
	extends \Aimeos\MW\View\Helper\Base
22
	implements \Aimeos\MW\View\Helper\Image\Iface
23
{
24
	/**
25
	 * Returns the HTML image tag for the given media item
26
	 *
27
	 * @param \Aimeos\MShop\Media\Item\Iface $media Media item
28
	 * @return string HTML image tag
29
	 */
30
	public function transform( \Aimeos\MShop\Media\Item\Iface $media ) : string
31
	{
32
		$view = $this->getView();
33
		$enc = $view->encoder();
34
35
		$sources = [];
36
		foreach( $media->getPreviews() as $type => $path ) {
37
			$sources[$type] = $this->content( $path );
0 ignored issues
show
Bug introduced by
The method content() does not exist on Aimeos\MW\View\Helper\Image\Standard. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
			/** @scrutinizer ignore-call */ 
38
   $sources[$type] = $this->content( $path );
Loading history...
38
		}
39
40
		$variant = '';
41
		foreach( $media->getRefItems( 'attribute', null, 'variant' ) as $id => $item ) {
42
			$variant .= ' data-variant-' . $item->getType() . '="' . $enc->attr( $id ) . '"';
43
		}
44
45
		return '<img class="item"
46
			itemprop="image" itemscope="" itemtype="http://schema.org/ImageObject"
47
			src="' . $enc->attr( $view->content( $media->getPreview() ) ) . '"
48
			srcset="' .  $enc->attr( $view->imageset( $media->getPreviews() ) ) . '"
49
			data-image="' . $enc->attr( $view->content( $media->getPreview() ) ) . '"
50
			data-sources="' . $enc->attr( json_encode( $sources, JSON_FORCE_OBJECT ) ) . '"
51
			alt="' . $enc->html( $media->getName() ) . '"' . $variant . '
52
		/>';
53
	}
54
}
55