-
Notifications
You must be signed in to change notification settings - Fork 2
/
Image.php
95 lines (88 loc) · 2.43 KB
/
Image.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
<?php
namespace Lucinda\RSS;
/**
* Encapsulates a RSS image tag according to specifications:
* https://www.rssboard.org/rss-profile#element-channel-image
*/
class Image implements Tag
{
private string $url;
private string $title;
private string $link;
private ?int $width;
private ?int $height;
private ?string $description;
/**
* Image constructor.
*
* @param string $url URL of website presenting image
* @param string $title Image textual description.
* @param string $link URL pointing to image source.
* @throws Exception
*/
public function __construct(string $url, string $title, string $link)
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new Exception("Invalid image url attribute");
}
$this->url = $url;
$this->title = $title;
if (!filter_var($link, FILTER_VALIDATE_URL)) {
throw new Exception("Invalid image link attribute");
}
$this->link = $link;
}
/**
* Sets image width in pixels.
*
* @param integer $width Value of image width
* @throws Exception
*/
public function setWidth(int $width): void
{
if ($width<=0) {
throw new Exception("Image width should be greater than zero");
}
$this->width = $width;
}
/**
* Sets image height in pixels.
*
* @param integer $height Value of image height
* @throws Exception
*/
public function setHeight(int $height): void
{
if ($height<=0) {
throw new Exception("Image height should be greater than zero");
}
$this->height = $height;
}
/**
* Sets image description (equivalent of 'title' attribute of img HTML tag)
*
* @param string $description Value of image textual description
*/
public function setDescription(string $description): void
{
$escaped = new Escape($description);
$this->description = (string) $escaped;
}
/**
* {@inheritDoc}
*
* @see Tag::__toString()
*/
public function __toString(): string
{
$output = "";
$parameters = get_object_vars($this);
foreach ($parameters as $key=>$value) {
if (empty($value)) {
continue;
}
$output .= "<".$key.">".$value."</".$key.">";
}
return "<image>".$output."</image>";
}
}