1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JeroenDesloovere\VCard\Model; |
4
|
|
|
|
5
|
|
|
use JeroenDesloovere\VCard\Exception\EmptyUrlException; |
6
|
|
|
use JeroenDesloovere\VCard\Exception\InvalidImageException; |
7
|
|
|
use JeroenDesloovere\VCard\Exception\InvalidUrlException; |
8
|
|
|
use JeroenDesloovere\VCard\Util\GeneralUtil; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class VCardMedia |
12
|
|
|
* |
13
|
|
|
* @package JeroenDesloovere\VCard\Model |
14
|
|
|
*/ |
15
|
|
|
class VCardMedia |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
protected $raw; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
protected $fileType; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
protected $url; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return null|string |
34
|
|
|
*/ |
35
|
|
|
public function getRaw(): ?string |
36
|
|
|
{ |
37
|
|
|
return $this->raw; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param null|string $raw |
42
|
|
|
*/ |
43
|
|
|
public function setRaw(?string $raw): void |
44
|
|
|
{ |
45
|
|
|
$this->raw = $raw; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return null|string |
50
|
|
|
*/ |
51
|
|
|
public function getFileType(): ?string |
52
|
|
|
{ |
53
|
|
|
return $this->fileType; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param null|string $fileType |
58
|
|
|
*/ |
59
|
|
|
public function setFileType(?string $fileType): void |
60
|
|
|
{ |
61
|
|
|
$this->fileType = $fileType; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return null|string |
66
|
|
|
*/ |
67
|
|
|
public function getUrl(): ?string |
68
|
|
|
{ |
69
|
|
|
return $this->url; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param null|string $url |
74
|
|
|
*/ |
75
|
|
|
public function setUrl(?string $url): void |
76
|
|
|
{ |
77
|
|
|
$this->url = $url; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param null|string $value |
82
|
|
|
* @param bool $isRawValue |
83
|
|
|
*/ |
84
|
|
|
public function parser(?string $value, bool $isRawValue): void |
85
|
|
|
{ |
86
|
|
|
if ($isRawValue) { |
87
|
|
|
$this->setRaw($value); |
88
|
|
|
} else { |
89
|
|
|
$this->setUrl($value); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add a photo or logo (depending on property name) |
95
|
|
|
* |
96
|
|
|
* @param string $url image url or filename |
97
|
|
|
* @param bool $include Do we include the image in our vcard or not? |
98
|
|
|
* |
99
|
|
|
* @throws EmptyUrlException |
100
|
|
|
* @throws InvalidImageException |
101
|
|
|
* @throws InvalidUrlException |
102
|
|
|
*/ |
103
|
|
|
public function addUrlMedia(string $url, bool $include = true): void |
104
|
|
|
{ |
105
|
|
|
$mimeType = GeneralUtil::getMimeType($url); |
106
|
|
|
if (!\is_string($mimeType) || 0 !== strpos($mimeType, 'image/')) { |
107
|
|
|
throw new InvalidImageException(); |
108
|
|
|
} |
109
|
|
|
$fileType = strtoupper(substr($mimeType, 6)); |
110
|
|
|
|
111
|
|
|
if ($fileType) { |
112
|
|
|
$this->setFileType($fileType); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($include) { |
116
|
|
|
$value = file_get_contents($url); |
117
|
|
|
|
118
|
|
|
if ($value === false) { |
119
|
|
|
throw new EmptyUrlException(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->setRaw($value); |
123
|
|
|
} else { |
124
|
|
|
$this->setUrl($url); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add a photo or logo (depending on property name) |
130
|
|
|
* |
131
|
|
|
* @param string $raw |
132
|
|
|
* @param null|string $fileType |
133
|
|
|
*/ |
134
|
|
|
public function addRawMedia(string $raw, ?string $fileType = null): void |
135
|
|
|
{ |
136
|
|
|
$this->setRaw($raw); |
137
|
|
|
$this->setFileType($fileType); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $property |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function builderUrl(string $property): array |
146
|
|
|
{ |
147
|
|
|
$url = $this->getUrl(); |
148
|
|
|
|
149
|
|
|
$fileType = $this->getFileType(); |
150
|
|
|
|
151
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL) !== false) { |
152
|
|
|
$propertySuffix = ';VALUE=URL'; |
153
|
|
|
$propertySuffix .= ';TYPE='.strtoupper($fileType); |
154
|
|
|
|
155
|
|
|
$property .= $propertySuffix; |
156
|
|
|
$fileValue = $url; |
157
|
|
|
} else { |
158
|
|
|
$fileValue = $url; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return [ |
162
|
|
|
'key' => $property, |
163
|
|
|
'value' => $fileValue, |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $property |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function builderRaw(string $property): array |
173
|
|
|
{ |
174
|
|
|
$raw = $this->getRaw(); |
175
|
|
|
$fileType = $this->getFileType(); |
176
|
|
|
|
177
|
|
|
$raw = base64_encode($raw); |
178
|
|
|
|
179
|
|
|
if ($fileType !== null) { |
180
|
|
|
$property .= ';ENCODING=b;TYPE='.$fileType; |
181
|
|
|
} else { |
182
|
|
|
$property .= ';ENCODING=b'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return [ |
186
|
|
|
'key' => $property, |
187
|
|
|
'value' => $raw, |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|