1
|
|
|
<?php namespace FlatPlan\Components; |
2
|
|
|
|
3
|
|
|
class Image extends AbstractComponent { |
4
|
|
|
|
5
|
|
|
private $url; |
6
|
|
|
private $role; |
7
|
|
|
private $caption; |
8
|
|
|
private $accessibilityCaption; |
9
|
|
|
private $expicitContent; |
10
|
|
|
|
11
|
|
|
protected $roles = ['figure', 'image', 'logo', 'photo', 'portrait', 'galleryitem']; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param string $url |
15
|
|
|
* @param string $role |
16
|
|
|
* @param string $caption |
17
|
|
|
* @param string $accessibilityCaption |
18
|
|
|
* @param bool $explicitContent |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function __construct($url, $role, $caption, $accessibilityCaption = '', $explicitContent = false) |
22
|
|
|
{ |
23
|
|
|
$this->setUrl($url); |
24
|
|
|
// galleryitems do not have a role attribute. |
25
|
|
|
if ($role !== 'galleryitem') { |
26
|
|
|
$this->setRole($role); |
27
|
|
|
} |
28
|
|
|
$this->setCaption($caption); |
29
|
|
|
$this->setAccessibilityCaption($accessibilityCaption); |
30
|
|
|
$this->setExplicitContent($explicitContent); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setUrl($url) |
34
|
|
|
{ |
35
|
|
|
if (!filter_var($url, FILTER_VALIDATE_URL)) { |
36
|
|
|
throw new \ErrorException('Invalid url supplied.'); |
37
|
|
|
} |
38
|
|
|
$this->url = $url; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getUrl() |
42
|
|
|
{ |
43
|
|
|
return $this->url; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setRole($role) |
47
|
|
|
{ |
48
|
|
|
if (!in_array($role, $this->roles)) { |
49
|
|
|
throw new \ErrorException('Invalid role supplied.'); |
50
|
|
|
} |
51
|
|
|
$this->role = $role; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getRole() |
55
|
|
|
{ |
56
|
|
|
return $this->role; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setCaption($caption) |
60
|
|
|
{ |
61
|
|
|
$this->caption = $caption; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getCaption() |
65
|
|
|
{ |
66
|
|
|
return $this->caption; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setAccessibilityCaption($accessibilityCaption) |
70
|
|
|
{ |
71
|
|
|
$this->accessibilityCaption = $accessibilityCaption; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getAccessibilityCaption() |
75
|
|
|
{ |
76
|
|
|
return $this->accessibilityCaption; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setExplicitContent($expicitContent) |
80
|
|
|
{ |
81
|
|
|
$this->expicitContent = $expicitContent; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getExplicitContent() |
85
|
|
|
{ |
86
|
|
|
return $this->expicitContent; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getComponent() |
90
|
|
|
{ |
91
|
|
|
$component = new \stdClass(); |
92
|
|
|
$component->url = $this->getUrl(); |
93
|
|
|
$component->role = $this->getRole(); |
94
|
|
|
$component->caption = $this->getCaption(); |
95
|
|
|
$component->accessibilityCaption = $this->getAccessibilityCaption(); |
96
|
|
|
$component->expicitContent = $this->getExplicitContent(); |
97
|
|
|
return $component; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function __toString() |
101
|
|
|
{ |
102
|
|
|
return json_encode($this->getComponent()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|