forked from openeuropa/oe_theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageValueObject.php
173 lines (147 loc) · 3.85 KB
/
ImageValueObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types = 1);
namespace Drupal\oe_theme\ValueObject;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
/**
* Handle information about an image, such as source, alt, name and responsive.
*/
class ImageValueObject extends ValueObjectBase implements ImageValueObjectInterface {
/**
* Image Source.
*
* @var string
*/
protected $src;
/**
* The alt of the image.
*
* @var string
*/
protected $alt;
/**
* The name of the Image.
*
* @var string
*/
protected $name;
/**
* The parameter 'responsive' of the image.
*
* @var bool
*/
protected $responsive;
/**
* ImageValueObject constructor.
*
* @param string $src
* Image URL, including Drupal schema if internal.
* @param string $alt
* Image alt text.
* @param string $name
* Name of the image, e.g. "example.jpg".
* @param bool $responsive
* Responsiveness of the image.
*/
private function __construct(string $src, string $alt = '', string $name = '', bool $responsive = TRUE) {
$this->src = $src;
$this->alt = $alt;
$this->name = $name;
$this->responsive = $responsive;
}
/**
* {@inheritdoc}
*/
public static function fromArray(array $values = []): ValueObjectInterface {
$values += ['alt' => '', 'name' => '', 'responsive' => TRUE];
return new static(
$values['src'],
$values['alt'],
$values['name'],
$values['responsive']
);
}
/**
* {@inheritdoc}
*/
public function getSource(): string {
return $this->src;
}
/**
* {@inheritdoc}
*/
public function getName(): string {
return $this->name;
}
/**
* {@inheritdoc}
*/
public function getAlt(): string {
return $this->alt;
}
/**
* {@inheritdoc}
*/
public function isResponsive(): bool {
return $this->responsive;
}
/**
* {@inheritdoc}
*/
public function getArray(): array {
return [
'name' => $this->getName(),
'src' => $this->getSource(),
'alt' => $this->getAlt(),
'responsive' => $this->isResponsive(),
];
}
/**
* Construct object from a Drupal image field.
*
* @param \Drupal\image\Plugin\Field\FieldType\ImageItem $image_item
* Field holding the image.
*
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
*
* @return $this
*/
public static function fromImageItem(ImageItem $image_item): ValueObjectInterface {
$image_file = $image_item->get('entity')->getValue();
$image_object = new static(
file_create_url($image_file->get('uri')->getString()),
$image_item->get('alt')->getString(),
$image_item->get('title')->getString()
);
$image_object->addCacheableDependency($image_file);
return $image_object;
}
/**
* Construct object from a Drupal image field and image style.
*
* @param \Drupal\image\Plugin\Field\FieldType\ImageItem $image_item
* The field image item instance.
* @param string $style_name
* The image style name.
*
* @return $this
* An image value object instance.
*
* @throws \InvalidArgumentException
* Thrown when the image style is not found.
*/
public static function fromStyledImageItem(ImageItem $image_item, string $style_name): ValueObjectInterface {
$image_file = $image_item->get('entity')->getValue();
$style = \Drupal::entityTypeManager()->getStorage('image_style')->load($style_name);
if (!$style) {
throw new \InvalidArgumentException(sprintf('Could not load image style with name "%s".', $style_name));
}
$image_object = new static(
$style->buildUrl($image_file->get('uri')->getString()),
$image_item->get('alt')->getString(),
$image_item->get('title')->getString()
);
$image_object->addCacheableDependency($image_file);
$image_object->addCacheableDependency($style);
return $image_object;
}
}