Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\media\OEmbed;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\Core\Cache\Cache;
|
Chris@17
|
6 use Drupal\Core\Cache\CacheableDependencyInterface;
|
Chris@17
|
7 use Drupal\Core\Cache\CacheableDependencyTrait;
|
Chris@17
|
8 use Drupal\Core\Url;
|
Chris@17
|
9
|
Chris@17
|
10 /**
|
Chris@17
|
11 * Value object representing an oEmbed resource.
|
Chris@17
|
12 *
|
Chris@17
|
13 * Data received from an oEmbed provider could be insecure. For example,
|
Chris@17
|
14 * resources of the 'rich' type provide an HTML representation which is not
|
Chris@17
|
15 * sanitized by this object in any way. Any values you retrieve from this object
|
Chris@17
|
16 * should be treated as potentially dangerous user input and carefully validated
|
Chris@17
|
17 * and sanitized before being displayed or otherwise manipulated by your code.
|
Chris@17
|
18 *
|
Chris@17
|
19 * Valid resource types are defined in the oEmbed specification and represented
|
Chris@17
|
20 * by the TYPE_* constants in this class.
|
Chris@17
|
21 *
|
Chris@17
|
22 * @see https://oembed.com/#section2
|
Chris@17
|
23 *
|
Chris@17
|
24 * @internal
|
Chris@17
|
25 * This class is an internal part of the oEmbed system and should only be
|
Chris@17
|
26 * instantiated by
|
Chris@17
|
27 * \Drupal\media\OEmbed\ResourceFetcherInterface::fetchResource().
|
Chris@17
|
28 */
|
Chris@17
|
29 class Resource implements CacheableDependencyInterface {
|
Chris@17
|
30
|
Chris@17
|
31 use CacheableDependencyTrait;
|
Chris@17
|
32
|
Chris@17
|
33 /**
|
Chris@17
|
34 * The resource type for link resources.
|
Chris@17
|
35 */
|
Chris@17
|
36 const TYPE_LINK = 'link';
|
Chris@17
|
37
|
Chris@17
|
38 /**
|
Chris@17
|
39 * The resource type for photo resources.
|
Chris@17
|
40 */
|
Chris@17
|
41 const TYPE_PHOTO = 'photo';
|
Chris@17
|
42
|
Chris@17
|
43 /**
|
Chris@17
|
44 * The resource type for rich resources.
|
Chris@17
|
45 */
|
Chris@17
|
46 const TYPE_RICH = 'rich';
|
Chris@17
|
47
|
Chris@17
|
48 /**
|
Chris@17
|
49 * The resource type for video resources.
|
Chris@17
|
50 */
|
Chris@17
|
51 const TYPE_VIDEO = 'video';
|
Chris@17
|
52
|
Chris@17
|
53 /**
|
Chris@17
|
54 * The resource type. Can be one of the static::TYPE_* constants.
|
Chris@17
|
55 *
|
Chris@17
|
56 * @var string
|
Chris@17
|
57 */
|
Chris@17
|
58 protected $type;
|
Chris@17
|
59
|
Chris@17
|
60 /**
|
Chris@17
|
61 * The resource provider.
|
Chris@17
|
62 *
|
Chris@17
|
63 * @var \Drupal\media\OEmbed\Provider
|
Chris@17
|
64 */
|
Chris@17
|
65 protected $provider;
|
Chris@17
|
66
|
Chris@17
|
67 /**
|
Chris@17
|
68 * A text title, describing the resource.
|
Chris@17
|
69 *
|
Chris@17
|
70 * @var string
|
Chris@17
|
71 */
|
Chris@17
|
72 protected $title;
|
Chris@17
|
73
|
Chris@17
|
74 /**
|
Chris@17
|
75 * The name of the author/owner of the resource.
|
Chris@17
|
76 *
|
Chris@17
|
77 * @var string
|
Chris@17
|
78 */
|
Chris@17
|
79 protected $authorName;
|
Chris@17
|
80
|
Chris@17
|
81 /**
|
Chris@17
|
82 * A URL for the author/owner of the resource.
|
Chris@17
|
83 *
|
Chris@17
|
84 * @var string
|
Chris@17
|
85 */
|
Chris@17
|
86 protected $authorUrl;
|
Chris@17
|
87
|
Chris@17
|
88 /**
|
Chris@17
|
89 * A URL to a thumbnail image representing the resource.
|
Chris@17
|
90 *
|
Chris@17
|
91 * The thumbnail must respect any maxwidth and maxheight parameters passed
|
Chris@17
|
92 * to the oEmbed endpoint. If this parameter is present, thumbnail_width and
|
Chris@17
|
93 * thumbnail_height must also be present.
|
Chris@17
|
94 *
|
Chris@17
|
95 * @var string
|
Chris@17
|
96 *
|
Chris@17
|
97 * @see \Drupal\media\OEmbed\UrlResolverInterface::getResourceUrl()
|
Chris@17
|
98 * @see https://oembed.com/#section2
|
Chris@17
|
99 */
|
Chris@17
|
100 protected $thumbnailUrl;
|
Chris@17
|
101
|
Chris@17
|
102 /**
|
Chris@17
|
103 * The width of the thumbnail, in pixels.
|
Chris@17
|
104 *
|
Chris@17
|
105 * If this parameter is present, thumbnail_url and thumbnail_height must also
|
Chris@17
|
106 * be present.
|
Chris@17
|
107 *
|
Chris@17
|
108 * @var int
|
Chris@17
|
109 */
|
Chris@17
|
110 protected $thumbnailWidth;
|
Chris@17
|
111
|
Chris@17
|
112 /**
|
Chris@17
|
113 * The height of the thumbnail, in pixels.
|
Chris@17
|
114 *
|
Chris@17
|
115 * If this parameter is present, thumbnail_url and thumbnail_width must also
|
Chris@17
|
116 * be present.
|
Chris@17
|
117 *
|
Chris@17
|
118 * @var int
|
Chris@17
|
119 */
|
Chris@17
|
120 protected $thumbnailHeight;
|
Chris@17
|
121
|
Chris@17
|
122 /**
|
Chris@17
|
123 * The width of the resource, in pixels.
|
Chris@17
|
124 *
|
Chris@17
|
125 * @var int
|
Chris@17
|
126 */
|
Chris@17
|
127 protected $width;
|
Chris@17
|
128
|
Chris@17
|
129 /**
|
Chris@17
|
130 * The height of the resource, in pixels.
|
Chris@17
|
131 *
|
Chris@17
|
132 * @var int
|
Chris@17
|
133 */
|
Chris@17
|
134 protected $height;
|
Chris@17
|
135
|
Chris@17
|
136 /**
|
Chris@17
|
137 * The resource URL. Only applies to 'photo' and 'link' resources.
|
Chris@17
|
138 *
|
Chris@17
|
139 * @var string
|
Chris@17
|
140 */
|
Chris@17
|
141 protected $url;
|
Chris@17
|
142
|
Chris@17
|
143 /**
|
Chris@17
|
144 * The HTML representation of the resource.
|
Chris@17
|
145 *
|
Chris@17
|
146 * Only applies to 'rich' and 'video' resources.
|
Chris@17
|
147 *
|
Chris@17
|
148 * @var string
|
Chris@17
|
149 */
|
Chris@17
|
150 protected $html;
|
Chris@17
|
151
|
Chris@17
|
152 /**
|
Chris@17
|
153 * Resource constructor.
|
Chris@17
|
154 *
|
Chris@17
|
155 * @param \Drupal\media\OEmbed\Provider $provider
|
Chris@17
|
156 * (optional) The resource provider.
|
Chris@17
|
157 * @param string $title
|
Chris@17
|
158 * (optional) A text title, describing the resource.
|
Chris@17
|
159 * @param string $author_name
|
Chris@17
|
160 * (optional) The name of the author/owner of the resource.
|
Chris@17
|
161 * @param string $author_url
|
Chris@17
|
162 * (optional) A URL for the author/owner of the resource.
|
Chris@17
|
163 * @param int $cache_age
|
Chris@17
|
164 * (optional) The suggested cache lifetime for this resource, in seconds.
|
Chris@17
|
165 * @param string $thumbnail_url
|
Chris@17
|
166 * (optional) A URL to a thumbnail image representing the resource. If this
|
Chris@17
|
167 * parameter is present, $thumbnail_width and $thumbnail_height must also be
|
Chris@17
|
168 * present.
|
Chris@17
|
169 * @param int $thumbnail_width
|
Chris@17
|
170 * (optional) The width of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
171 * present, $thumbnail_url and $thumbnail_height must also be present.
|
Chris@17
|
172 * @param int $thumbnail_height
|
Chris@17
|
173 * (optional) The height of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
174 * present, $thumbnail_url and $thumbnail_width must also be present.
|
Chris@17
|
175 */
|
Chris@17
|
176 protected function __construct(Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
|
Chris@17
|
177 $this->provider = $provider;
|
Chris@17
|
178 $this->title = $title;
|
Chris@17
|
179 $this->authorName = $author_name;
|
Chris@17
|
180 $this->authorUrl = $author_url;
|
Chris@17
|
181
|
Chris@17
|
182 if (isset($cache_age) && is_numeric($cache_age)) {
|
Chris@17
|
183 // If the cache age is too big, it can overflow the 'expire' column of
|
Chris@17
|
184 // database cache backends, causing SQL exceptions. To prevent that,
|
Chris@17
|
185 // arbitrarily limit the cache age to 5 years. That should be enough.
|
Chris@17
|
186 $this->cacheMaxAge = Cache::mergeMaxAges((int) $cache_age, 157680000);
|
Chris@17
|
187 }
|
Chris@17
|
188
|
Chris@17
|
189 if ($thumbnail_url) {
|
Chris@17
|
190 $this->thumbnailUrl = $thumbnail_url;
|
Chris@17
|
191 $this->setThumbnailDimensions($thumbnail_width, $thumbnail_height);
|
Chris@17
|
192 }
|
Chris@17
|
193 }
|
Chris@17
|
194
|
Chris@17
|
195 /**
|
Chris@17
|
196 * Creates a link resource.
|
Chris@17
|
197 *
|
Chris@17
|
198 * @param string $url
|
Chris@17
|
199 * (optional) The URL of the resource.
|
Chris@17
|
200 * @param \Drupal\media\OEmbed\Provider $provider
|
Chris@17
|
201 * (optional) The resource provider.
|
Chris@17
|
202 * @param string $title
|
Chris@17
|
203 * (optional) A text title, describing the resource.
|
Chris@17
|
204 * @param string $author_name
|
Chris@17
|
205 * (optional) The name of the author/owner of the resource.
|
Chris@17
|
206 * @param string $author_url
|
Chris@17
|
207 * (optional) A URL for the author/owner of the resource.
|
Chris@17
|
208 * @param int $cache_age
|
Chris@17
|
209 * (optional) The suggested cache lifetime for this resource, in seconds.
|
Chris@17
|
210 * @param string $thumbnail_url
|
Chris@17
|
211 * (optional) A URL to a thumbnail image representing the resource. If this
|
Chris@17
|
212 * parameter is present, $thumbnail_width and $thumbnail_height must also be
|
Chris@17
|
213 * present.
|
Chris@17
|
214 * @param int $thumbnail_width
|
Chris@17
|
215 * (optional) The width of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
216 * present, $thumbnail_url and $thumbnail_height must also be present.
|
Chris@17
|
217 * @param int $thumbnail_height
|
Chris@17
|
218 * (optional) The height of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
219 * present, $thumbnail_url and $thumbnail_width must also be present.
|
Chris@17
|
220 *
|
Chris@17
|
221 * @return static
|
Chris@17
|
222 */
|
Chris@17
|
223 public static function link($url = NULL, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
|
Chris@17
|
224 $resource = new static($provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
|
Chris@17
|
225 $resource->type = self::TYPE_LINK;
|
Chris@17
|
226 $resource->url = $url;
|
Chris@17
|
227
|
Chris@17
|
228 return $resource;
|
Chris@17
|
229 }
|
Chris@17
|
230
|
Chris@17
|
231 /**
|
Chris@17
|
232 * Creates a photo resource.
|
Chris@17
|
233 *
|
Chris@17
|
234 * @param string $url
|
Chris@17
|
235 * The URL of the photo.
|
Chris@17
|
236 * @param int $width
|
Chris@17
|
237 * The width of the photo, in pixels.
|
Chris@17
|
238 * @param int $height
|
Chris@17
|
239 * The height of the photo, in pixels.
|
Chris@17
|
240 * @param \Drupal\media\OEmbed\Provider $provider
|
Chris@17
|
241 * (optional) The resource provider.
|
Chris@17
|
242 * @param string $title
|
Chris@17
|
243 * (optional) A text title, describing the resource.
|
Chris@17
|
244 * @param string $author_name
|
Chris@17
|
245 * (optional) The name of the author/owner of the resource.
|
Chris@17
|
246 * @param string $author_url
|
Chris@17
|
247 * (optional) A URL for the author/owner of the resource.
|
Chris@17
|
248 * @param int $cache_age
|
Chris@17
|
249 * (optional) The suggested cache lifetime for this resource, in seconds.
|
Chris@17
|
250 * @param string $thumbnail_url
|
Chris@17
|
251 * (optional) A URL to a thumbnail image representing the resource. If this
|
Chris@17
|
252 * parameter is present, $thumbnail_width and $thumbnail_height must also be
|
Chris@17
|
253 * present.
|
Chris@17
|
254 * @param int $thumbnail_width
|
Chris@17
|
255 * (optional) The width of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
256 * present, $thumbnail_url and $thumbnail_height must also be present.
|
Chris@17
|
257 * @param int $thumbnail_height
|
Chris@17
|
258 * (optional) The height of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
259 * present, $thumbnail_url and $thumbnail_width must also be present.
|
Chris@17
|
260 *
|
Chris@17
|
261 * @return static
|
Chris@17
|
262 */
|
Chris@17
|
263 public static function photo($url, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
|
Chris@17
|
264 if (empty($url)) {
|
Chris@17
|
265 throw new \InvalidArgumentException('Photo resources must provide a URL.');
|
Chris@17
|
266 }
|
Chris@17
|
267
|
Chris@17
|
268 $resource = static::link($url, $provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
|
Chris@17
|
269 $resource->type = self::TYPE_PHOTO;
|
Chris@17
|
270 $resource->setDimensions($width, $height);
|
Chris@17
|
271
|
Chris@17
|
272 return $resource;
|
Chris@17
|
273 }
|
Chris@17
|
274
|
Chris@17
|
275 /**
|
Chris@17
|
276 * Creates a rich resource.
|
Chris@17
|
277 *
|
Chris@17
|
278 * @param string $html
|
Chris@17
|
279 * The HTML representation of the resource.
|
Chris@17
|
280 * @param int $width
|
Chris@17
|
281 * The width of the resource, in pixels.
|
Chris@17
|
282 * @param int $height
|
Chris@17
|
283 * The height of the resource, in pixels.
|
Chris@17
|
284 * @param \Drupal\media\OEmbed\Provider $provider
|
Chris@17
|
285 * (optional) The resource provider.
|
Chris@17
|
286 * @param string $title
|
Chris@17
|
287 * (optional) A text title, describing the resource.
|
Chris@17
|
288 * @param string $author_name
|
Chris@17
|
289 * (optional) The name of the author/owner of the resource.
|
Chris@17
|
290 * @param string $author_url
|
Chris@17
|
291 * (optional) A URL for the author/owner of the resource.
|
Chris@17
|
292 * @param int $cache_age
|
Chris@17
|
293 * (optional) The suggested cache lifetime for this resource, in seconds.
|
Chris@17
|
294 * @param string $thumbnail_url
|
Chris@17
|
295 * (optional) A URL to a thumbnail image representing the resource. If this
|
Chris@17
|
296 * parameter is present, $thumbnail_width and $thumbnail_height must also be
|
Chris@17
|
297 * present.
|
Chris@17
|
298 * @param int $thumbnail_width
|
Chris@17
|
299 * (optional) The width of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
300 * present, $thumbnail_url and $thumbnail_height must also be present.
|
Chris@17
|
301 * @param int $thumbnail_height
|
Chris@17
|
302 * (optional) The height of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
303 * present, $thumbnail_url and $thumbnail_width must also be present.
|
Chris@17
|
304 *
|
Chris@17
|
305 * @return static
|
Chris@17
|
306 */
|
Chris@17
|
307 public static function rich($html, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
|
Chris@17
|
308 if (empty($html)) {
|
Chris@17
|
309 throw new \InvalidArgumentException('The resource must provide an HTML representation.');
|
Chris@17
|
310 }
|
Chris@17
|
311
|
Chris@17
|
312 $resource = new static($provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
|
Chris@17
|
313 $resource->type = self::TYPE_RICH;
|
Chris@17
|
314 $resource->html = $html;
|
Chris@17
|
315 $resource->setDimensions($width, $height);
|
Chris@17
|
316
|
Chris@17
|
317 return $resource;
|
Chris@17
|
318 }
|
Chris@17
|
319
|
Chris@17
|
320 /**
|
Chris@17
|
321 * Creates a video resource.
|
Chris@17
|
322 *
|
Chris@17
|
323 * @param string $html
|
Chris@17
|
324 * The HTML required to display the video.
|
Chris@17
|
325 * @param int $width
|
Chris@17
|
326 * The width of the video, in pixels.
|
Chris@17
|
327 * @param int $height
|
Chris@17
|
328 * The height of the video, in pixels.
|
Chris@17
|
329 * @param \Drupal\media\OEmbed\Provider $provider
|
Chris@17
|
330 * (optional) The resource provider.
|
Chris@17
|
331 * @param string $title
|
Chris@17
|
332 * (optional) A text title, describing the resource.
|
Chris@17
|
333 * @param string $author_name
|
Chris@17
|
334 * (optional) The name of the author/owner of the resource.
|
Chris@17
|
335 * @param string $author_url
|
Chris@17
|
336 * (optional) A URL for the author/owner of the resource.
|
Chris@17
|
337 * @param int $cache_age
|
Chris@17
|
338 * (optional) The suggested cache lifetime for this resource, in seconds.
|
Chris@17
|
339 * @param string $thumbnail_url
|
Chris@17
|
340 * (optional) A URL to a thumbnail image representing the resource. If this
|
Chris@17
|
341 * parameter is present, $thumbnail_width and $thumbnail_height must also be
|
Chris@17
|
342 * present.
|
Chris@17
|
343 * @param int $thumbnail_width
|
Chris@17
|
344 * (optional) The width of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
345 * present, $thumbnail_url and $thumbnail_height must also be present.
|
Chris@17
|
346 * @param int $thumbnail_height
|
Chris@17
|
347 * (optional) The height of the thumbnail, in pixels. If this parameter is
|
Chris@17
|
348 * present, $thumbnail_url and $thumbnail_width must also be present.
|
Chris@17
|
349 *
|
Chris@17
|
350 * @return static
|
Chris@17
|
351 */
|
Chris@17
|
352 public static function video($html, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
|
Chris@17
|
353 $resource = static::rich($html, $width, $height, $provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
|
Chris@17
|
354 $resource->type = self::TYPE_VIDEO;
|
Chris@17
|
355
|
Chris@17
|
356 return $resource;
|
Chris@17
|
357 }
|
Chris@17
|
358
|
Chris@17
|
359 /**
|
Chris@17
|
360 * Returns the resource type.
|
Chris@17
|
361 *
|
Chris@17
|
362 * @return string
|
Chris@17
|
363 * The resource type. Will be one of the self::TYPE_* constants.
|
Chris@17
|
364 */
|
Chris@17
|
365 public function getType() {
|
Chris@17
|
366 return $this->type;
|
Chris@17
|
367 }
|
Chris@17
|
368
|
Chris@17
|
369 /**
|
Chris@17
|
370 * Returns the title of the resource.
|
Chris@17
|
371 *
|
Chris@17
|
372 * @return string|null
|
Chris@17
|
373 * The title of the resource, if known.
|
Chris@17
|
374 */
|
Chris@17
|
375 public function getTitle() {
|
Chris@17
|
376 return $this->title;
|
Chris@17
|
377 }
|
Chris@17
|
378
|
Chris@17
|
379 /**
|
Chris@17
|
380 * Returns the name of the resource author.
|
Chris@17
|
381 *
|
Chris@17
|
382 * @return string|null
|
Chris@17
|
383 * The name of the resource author, if known.
|
Chris@17
|
384 */
|
Chris@17
|
385 public function getAuthorName() {
|
Chris@17
|
386 return $this->authorName;
|
Chris@17
|
387 }
|
Chris@17
|
388
|
Chris@17
|
389 /**
|
Chris@17
|
390 * Returns the URL of the resource author.
|
Chris@17
|
391 *
|
Chris@17
|
392 * @return \Drupal\Core\Url|null
|
Chris@17
|
393 * The absolute URL of the resource author, or NULL if none is provided.
|
Chris@17
|
394 */
|
Chris@17
|
395 public function getAuthorUrl() {
|
Chris@17
|
396 return $this->authorUrl ? Url::fromUri($this->authorUrl)->setAbsolute() : NULL;
|
Chris@17
|
397 }
|
Chris@17
|
398
|
Chris@17
|
399 /**
|
Chris@17
|
400 * Returns the resource provider, if known.
|
Chris@17
|
401 *
|
Chris@17
|
402 * @return \Drupal\media\OEmbed\Provider|null
|
Chris@17
|
403 * The resource provider, or NULL if the provider is not known.
|
Chris@17
|
404 */
|
Chris@17
|
405 public function getProvider() {
|
Chris@17
|
406 return $this->provider;
|
Chris@17
|
407 }
|
Chris@17
|
408
|
Chris@17
|
409 /**
|
Chris@17
|
410 * Returns the URL of the resource's thumbnail image.
|
Chris@17
|
411 *
|
Chris@17
|
412 * @return \Drupal\Core\Url|null
|
Chris@17
|
413 * The absolute URL of the thumbnail image, or NULL if there isn't one.
|
Chris@17
|
414 */
|
Chris@17
|
415 public function getThumbnailUrl() {
|
Chris@17
|
416 return $this->thumbnailUrl ? Url::fromUri($this->thumbnailUrl)->setAbsolute() : NULL;
|
Chris@17
|
417 }
|
Chris@17
|
418
|
Chris@17
|
419 /**
|
Chris@17
|
420 * Returns the width of the resource's thumbnail image.
|
Chris@17
|
421 *
|
Chris@17
|
422 * @return int|null
|
Chris@17
|
423 * The thumbnail width in pixels, or NULL if there is no thumbnail.
|
Chris@17
|
424 */
|
Chris@17
|
425 public function getThumbnailWidth() {
|
Chris@17
|
426 return $this->thumbnailWidth;
|
Chris@17
|
427 }
|
Chris@17
|
428
|
Chris@17
|
429 /**
|
Chris@17
|
430 * Returns the height of the resource's thumbnail image.
|
Chris@17
|
431 *
|
Chris@17
|
432 * @return int|null
|
Chris@17
|
433 * The thumbnail height in pixels, or NULL if there is no thumbnail.
|
Chris@17
|
434 */
|
Chris@17
|
435 public function getThumbnailHeight() {
|
Chris@17
|
436 return $this->thumbnailHeight;
|
Chris@17
|
437 }
|
Chris@17
|
438
|
Chris@17
|
439 /**
|
Chris@17
|
440 * Returns the width of the resource.
|
Chris@17
|
441 *
|
Chris@17
|
442 * @return int|null
|
Chris@17
|
443 * The width of the resource in pixels, or NULL if the resource has no
|
Chris@17
|
444 * dimensions
|
Chris@17
|
445 */
|
Chris@17
|
446 public function getWidth() {
|
Chris@17
|
447 return $this->width;
|
Chris@17
|
448 }
|
Chris@17
|
449
|
Chris@17
|
450 /**
|
Chris@17
|
451 * Returns the height of the resource.
|
Chris@17
|
452 *
|
Chris@17
|
453 * @return int|null
|
Chris@17
|
454 * The height of the resource in pixels, or NULL if the resource has no
|
Chris@17
|
455 * dimensions.
|
Chris@17
|
456 */
|
Chris@17
|
457 public function getHeight() {
|
Chris@17
|
458 return $this->height;
|
Chris@17
|
459 }
|
Chris@17
|
460
|
Chris@17
|
461 /**
|
Chris@17
|
462 * Returns the URL of the resource. Only applies to 'photo' resources.
|
Chris@17
|
463 *
|
Chris@17
|
464 * @return \Drupal\Core\Url|null
|
Chris@17
|
465 * The resource URL, if it has one.
|
Chris@17
|
466 */
|
Chris@17
|
467 public function getUrl() {
|
Chris@17
|
468 if ($this->url) {
|
Chris@17
|
469 return Url::fromUri($this->url)->setAbsolute();
|
Chris@17
|
470 }
|
Chris@17
|
471 return NULL;
|
Chris@17
|
472 }
|
Chris@17
|
473
|
Chris@17
|
474 /**
|
Chris@17
|
475 * Returns the HTML representation of the resource.
|
Chris@17
|
476 *
|
Chris@17
|
477 * Only applies to 'rich' and 'video' resources.
|
Chris@17
|
478 *
|
Chris@17
|
479 * @return string|null
|
Chris@17
|
480 * The HTML representation of the resource, if it has one.
|
Chris@17
|
481 */
|
Chris@17
|
482 public function getHtml() {
|
Chris@17
|
483 return isset($this->html) ? (string) $this->html : NULL;
|
Chris@17
|
484 }
|
Chris@17
|
485
|
Chris@17
|
486 /**
|
Chris@17
|
487 * Sets the thumbnail dimensions.
|
Chris@17
|
488 *
|
Chris@17
|
489 * @param int $width
|
Chris@17
|
490 * The width of the resource.
|
Chris@17
|
491 * @param int $height
|
Chris@17
|
492 * The height of the resource.
|
Chris@17
|
493 *
|
Chris@17
|
494 * @throws \InvalidArgumentException
|
Chris@17
|
495 * If either $width or $height are not numbers greater than zero.
|
Chris@17
|
496 */
|
Chris@17
|
497 protected function setThumbnailDimensions($width, $height) {
|
Chris@17
|
498 $width = (int) $width;
|
Chris@17
|
499 $height = (int) $height;
|
Chris@17
|
500
|
Chris@17
|
501 if ($width > 0 && $height > 0) {
|
Chris@17
|
502 $this->thumbnailWidth = $width;
|
Chris@17
|
503 $this->thumbnailHeight = $height;
|
Chris@17
|
504 }
|
Chris@17
|
505 else {
|
Chris@17
|
506 throw new \InvalidArgumentException('The thumbnail dimensions must be numbers greater than zero.');
|
Chris@17
|
507 }
|
Chris@17
|
508 }
|
Chris@17
|
509
|
Chris@17
|
510 /**
|
Chris@17
|
511 * Sets the dimensions.
|
Chris@17
|
512 *
|
Chris@17
|
513 * @param int $width
|
Chris@17
|
514 * The width of the resource.
|
Chris@17
|
515 * @param int $height
|
Chris@17
|
516 * The height of the resource.
|
Chris@17
|
517 *
|
Chris@17
|
518 * @throws \InvalidArgumentException
|
Chris@17
|
519 * If either $width or $height are not numbers greater than zero.
|
Chris@17
|
520 */
|
Chris@17
|
521 protected function setDimensions($width, $height) {
|
Chris@17
|
522 $width = (int) $width;
|
Chris@17
|
523 $height = (int) $height;
|
Chris@17
|
524
|
Chris@17
|
525 if ($width > 0 && $height > 0) {
|
Chris@17
|
526 $this->width = $width;
|
Chris@17
|
527 $this->height = $height;
|
Chris@17
|
528 }
|
Chris@17
|
529 else {
|
Chris@17
|
530 throw new \InvalidArgumentException('The dimensions must be numbers greater than zero.');
|
Chris@17
|
531 }
|
Chris@17
|
532 }
|
Chris@17
|
533
|
Chris@17
|
534 }
|