Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license http://framework.zend.com/license/new-bsd New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Feed\Writer\Extension\ITunes;
|
Chris@0
|
11
|
Chris@16
|
12 use Zend\Feed\Uri;
|
Chris@0
|
13 use Zend\Feed\Writer;
|
Chris@0
|
14 use Zend\Stdlib\StringUtils;
|
Chris@0
|
15 use Zend\Stdlib\StringWrapper\StringWrapperInterface;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 */
|
Chris@0
|
19 class Entry
|
Chris@0
|
20 {
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Array of Feed data for rendering by Extension's renderers
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var array
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $data = [];
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Encoding of all text values
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var string
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $encoding = 'UTF-8';
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * The used string wrapper supporting encoding
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var StringWrapperInterface
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $stringWrapper;
|
Chris@0
|
41
|
Chris@0
|
42 public function __construct()
|
Chris@0
|
43 {
|
Chris@0
|
44 $this->stringWrapper = StringUtils::getWrapper($this->encoding);
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Set feed encoding
|
Chris@0
|
49 *
|
Chris@0
|
50 * @param string $enc
|
Chris@0
|
51 * @return Entry
|
Chris@0
|
52 */
|
Chris@0
|
53 public function setEncoding($enc)
|
Chris@0
|
54 {
|
Chris@0
|
55 $this->stringWrapper = StringUtils::getWrapper($enc);
|
Chris@0
|
56 $this->encoding = $enc;
|
Chris@0
|
57 return $this;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Get feed encoding
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return string
|
Chris@0
|
64 */
|
Chris@0
|
65 public function getEncoding()
|
Chris@0
|
66 {
|
Chris@0
|
67 return $this->encoding;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Set a block value of "yes" or "no". You may also set an empty string.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @param string
|
Chris@0
|
74 * @throws Writer\Exception\InvalidArgumentException
|
Chris@0
|
75 */
|
Chris@0
|
76 public function setItunesBlock($value)
|
Chris@0
|
77 {
|
Chris@12
|
78 if (! ctype_alpha($value) && strlen($value) > 0) {
|
Chris@0
|
79 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only'
|
Chris@0
|
80 . ' contain alphabetic characters');
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 if ($this->stringWrapper->strlen($value) > 255) {
|
Chris@0
|
84 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only'
|
Chris@0
|
85 . ' contain a maximum of 255 characters');
|
Chris@0
|
86 }
|
Chris@0
|
87 $this->data['block'] = $value;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Add authors to itunes entry
|
Chris@0
|
92 *
|
Chris@0
|
93 * @param array $values
|
Chris@0
|
94 * @return Entry
|
Chris@0
|
95 */
|
Chris@0
|
96 public function addItunesAuthors(array $values)
|
Chris@0
|
97 {
|
Chris@0
|
98 foreach ($values as $value) {
|
Chris@0
|
99 $this->addItunesAuthor($value);
|
Chris@0
|
100 }
|
Chris@0
|
101 return $this;
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Add author to itunes entry
|
Chris@0
|
106 *
|
Chris@0
|
107 * @param string $value
|
Chris@0
|
108 * @return Entry
|
Chris@0
|
109 * @throws Writer\Exception\InvalidArgumentException
|
Chris@0
|
110 */
|
Chris@0
|
111 public function addItunesAuthor($value)
|
Chris@0
|
112 {
|
Chris@0
|
113 if ($this->stringWrapper->strlen($value) > 255) {
|
Chris@0
|
114 throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "author" may only'
|
Chris@0
|
115 . ' contain a maximum of 255 characters each');
|
Chris@0
|
116 }
|
Chris@12
|
117 if (! isset($this->data['authors'])) {
|
Chris@0
|
118 $this->data['authors'] = [];
|
Chris@0
|
119 }
|
Chris@0
|
120 $this->data['authors'][] = $value;
|
Chris@0
|
121 return $this;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * Set duration
|
Chris@0
|
126 *
|
Chris@0
|
127 * @param int $value
|
Chris@0
|
128 * @return Entry
|
Chris@0
|
129 * @throws Writer\Exception\InvalidArgumentException
|
Chris@0
|
130 */
|
Chris@0
|
131 public function setItunesDuration($value)
|
Chris@0
|
132 {
|
Chris@0
|
133 $value = (string) $value;
|
Chris@12
|
134 if (! ctype_digit($value)
|
Chris@12
|
135 && ! preg_match("/^\d+:[0-5]{1}[0-9]{1}$/", $value)
|
Chris@12
|
136 && ! preg_match("/^\d+:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/", $value)
|
Chris@0
|
137 ) {
|
Chris@0
|
138 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "duration" may only'
|
Chris@0
|
139 . ' be of a specified [[HH:]MM:]SS format');
|
Chris@0
|
140 }
|
Chris@0
|
141 $this->data['duration'] = $value;
|
Chris@0
|
142 return $this;
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Set "explicit" flag
|
Chris@0
|
147 *
|
Chris@0
|
148 * @param bool $value
|
Chris@0
|
149 * @return Entry
|
Chris@0
|
150 * @throws Writer\Exception\InvalidArgumentException
|
Chris@0
|
151 */
|
Chris@0
|
152 public function setItunesExplicit($value)
|
Chris@0
|
153 {
|
Chris@12
|
154 if (! in_array($value, ['yes', 'no', 'clean'])) {
|
Chris@0
|
155 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "explicit" may only'
|
Chris@0
|
156 . ' be one of "yes", "no" or "clean"');
|
Chris@0
|
157 }
|
Chris@0
|
158 $this->data['explicit'] = $value;
|
Chris@0
|
159 return $this;
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * Set keywords
|
Chris@0
|
164 *
|
Chris@16
|
165 * @deprecated since 2.10.0; itunes:keywords is no longer part of the
|
Chris@16
|
166 * iTunes podcast RSS specification.
|
Chris@0
|
167 * @param array $value
|
Chris@0
|
168 * @return Entry
|
Chris@0
|
169 * @throws Writer\Exception\InvalidArgumentException
|
Chris@0
|
170 */
|
Chris@0
|
171 public function setItunesKeywords(array $value)
|
Chris@0
|
172 {
|
Chris@16
|
173 trigger_error(
|
Chris@16
|
174 'itunes:keywords has been deprecated in the iTunes podcast RSS specification,'
|
Chris@16
|
175 . ' and should not be relied on.',
|
Chris@16
|
176 \E_USER_DEPRECATED
|
Chris@16
|
177 );
|
Chris@16
|
178
|
Chris@0
|
179 if (count($value) > 12) {
|
Chris@0
|
180 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
|
Chris@0
|
181 . ' contain a maximum of 12 terms');
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 $concat = implode(',', $value);
|
Chris@0
|
185 if ($this->stringWrapper->strlen($concat) > 255) {
|
Chris@0
|
186 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
|
Chris@0
|
187 . ' have a concatenated length of 255 chars where terms are delimited'
|
Chris@0
|
188 . ' by a comma');
|
Chris@0
|
189 }
|
Chris@0
|
190 $this->data['keywords'] = $value;
|
Chris@0
|
191 return $this;
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@18
|
195 * Set title
|
Chris@18
|
196 *
|
Chris@18
|
197 * @param string $value
|
Chris@18
|
198 * @return Entry
|
Chris@18
|
199 * @throws Writer\Exception\InvalidArgumentException
|
Chris@18
|
200 */
|
Chris@18
|
201 public function setItunesTitle($value)
|
Chris@18
|
202 {
|
Chris@18
|
203 if ($this->stringWrapper->strlen($value) > 255) {
|
Chris@18
|
204 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "title" may only'
|
Chris@18
|
205 . ' contain a maximum of 255 characters');
|
Chris@18
|
206 }
|
Chris@18
|
207 $this->data['title'] = $value;
|
Chris@18
|
208 return $this;
|
Chris@18
|
209 }
|
Chris@18
|
210
|
Chris@18
|
211 /**
|
Chris@0
|
212 * Set subtitle
|
Chris@0
|
213 *
|
Chris@0
|
214 * @param string $value
|
Chris@0
|
215 * @return Entry
|
Chris@0
|
216 * @throws Writer\Exception\InvalidArgumentException
|
Chris@0
|
217 */
|
Chris@0
|
218 public function setItunesSubtitle($value)
|
Chris@0
|
219 {
|
Chris@0
|
220 if ($this->stringWrapper->strlen($value) > 255) {
|
Chris@0
|
221 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "subtitle" may only'
|
Chris@0
|
222 . ' contain a maximum of 255 characters');
|
Chris@0
|
223 }
|
Chris@0
|
224 $this->data['subtitle'] = $value;
|
Chris@0
|
225 return $this;
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 /**
|
Chris@0
|
229 * Set summary
|
Chris@0
|
230 *
|
Chris@0
|
231 * @param string $value
|
Chris@0
|
232 * @return Entry
|
Chris@0
|
233 * @throws Writer\Exception\InvalidArgumentException
|
Chris@0
|
234 */
|
Chris@0
|
235 public function setItunesSummary($value)
|
Chris@0
|
236 {
|
Chris@0
|
237 if ($this->stringWrapper->strlen($value) > 4000) {
|
Chris@0
|
238 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "summary" may only'
|
Chris@0
|
239 . ' contain a maximum of 4000 characters');
|
Chris@0
|
240 }
|
Chris@0
|
241 $this->data['summary'] = $value;
|
Chris@0
|
242 return $this;
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@16
|
246 * Set entry image (icon)
|
Chris@16
|
247 *
|
Chris@16
|
248 * @param string $value
|
Chris@17
|
249 * @return Entry
|
Chris@16
|
250 * @throws Writer\Exception\InvalidArgumentException
|
Chris@16
|
251 */
|
Chris@16
|
252 public function setItunesImage($value)
|
Chris@16
|
253 {
|
Chris@16
|
254 if (! is_string($value) || ! Uri::factory($value)->isValid()) {
|
Chris@16
|
255 throw new Writer\Exception\InvalidArgumentException(
|
Chris@16
|
256 'invalid parameter: "image" may only be a valid URI/IRI'
|
Chris@16
|
257 );
|
Chris@16
|
258 }
|
Chris@16
|
259
|
Chris@16
|
260 if (! in_array(substr($value, -3), ['jpg', 'png'])) {
|
Chris@16
|
261 throw new Writer\Exception\InvalidArgumentException(
|
Chris@16
|
262 'invalid parameter: "image" may only use file extension "jpg"'
|
Chris@16
|
263 . ' or "png" which must be the last three characters of the URI'
|
Chris@16
|
264 . ' (i.e. no query string or fragment)'
|
Chris@16
|
265 );
|
Chris@16
|
266 }
|
Chris@16
|
267
|
Chris@16
|
268 $this->data['image'] = $value;
|
Chris@16
|
269 return $this;
|
Chris@16
|
270 }
|
Chris@16
|
271
|
Chris@16
|
272 /**
|
Chris@16
|
273 * Set the episode number
|
Chris@16
|
274 *
|
Chris@16
|
275 * @param int $number
|
Chris@16
|
276 * @return self
|
Chris@16
|
277 * @throws Writer\Exception\InvalidArgumentException
|
Chris@16
|
278 */
|
Chris@16
|
279 public function setItunesEpisode($number)
|
Chris@16
|
280 {
|
Chris@16
|
281 if (! is_numeric($number) || is_float($number)) {
|
Chris@16
|
282 throw new Writer\Exception\InvalidArgumentException(sprintf(
|
Chris@16
|
283 'invalid parameter: "number" may only be an integer; received %s',
|
Chris@16
|
284 is_object($number) ? get_class($number) : gettype($number)
|
Chris@16
|
285 ));
|
Chris@16
|
286 }
|
Chris@16
|
287
|
Chris@16
|
288 $this->data['episode'] = (int) $number;
|
Chris@16
|
289
|
Chris@16
|
290 return $this;
|
Chris@16
|
291 }
|
Chris@16
|
292
|
Chris@16
|
293 /**
|
Chris@16
|
294 * Set the episode type
|
Chris@16
|
295 *
|
Chris@16
|
296 * @param string $type One of "full", "trailer", or "bonus".
|
Chris@16
|
297 * @return self
|
Chris@16
|
298 * @throws Writer\Exception\InvalidArgumentException
|
Chris@16
|
299 */
|
Chris@16
|
300 public function setItunesEpisodeType($type)
|
Chris@16
|
301 {
|
Chris@16
|
302 $validTypes = ['full', 'trailer', 'bonus'];
|
Chris@16
|
303 if (! in_array($type, $validTypes, true)) {
|
Chris@16
|
304 throw new Writer\Exception\InvalidArgumentException(sprintf(
|
Chris@16
|
305 'invalid parameter: "episodeType" MUST be one of the strings [%s]; received %s',
|
Chris@16
|
306 implode(', ', $validTypes),
|
Chris@16
|
307 is_object($type) ? get_class($type) : var_export($type, true)
|
Chris@16
|
308 ));
|
Chris@16
|
309 }
|
Chris@16
|
310
|
Chris@16
|
311 $this->data['episodeType'] = $type;
|
Chris@16
|
312
|
Chris@16
|
313 return $this;
|
Chris@16
|
314 }
|
Chris@16
|
315
|
Chris@16
|
316 /**
|
Chris@16
|
317 * Set the status of closed captioning
|
Chris@16
|
318 *
|
Chris@16
|
319 * @param bool $status
|
Chris@16
|
320 * @return self
|
Chris@16
|
321 * @throws Writer\Exception\InvalidArgumentException
|
Chris@16
|
322 */
|
Chris@16
|
323 public function setItunesIsClosedCaptioned($status)
|
Chris@16
|
324 {
|
Chris@16
|
325 if (! is_bool($status)) {
|
Chris@16
|
326 throw new Writer\Exception\InvalidArgumentException(sprintf(
|
Chris@16
|
327 'invalid parameter: "isClosedCaptioned" MUST be a boolean; received %s',
|
Chris@16
|
328 is_object($status) ? get_class($status) : var_export($status, true)
|
Chris@16
|
329 ));
|
Chris@16
|
330 }
|
Chris@16
|
331
|
Chris@16
|
332 if (! $status) {
|
Chris@16
|
333 return $this;
|
Chris@16
|
334 }
|
Chris@16
|
335
|
Chris@16
|
336 $this->data['isClosedCaptioned'] = true;
|
Chris@16
|
337
|
Chris@16
|
338 return $this;
|
Chris@16
|
339 }
|
Chris@16
|
340
|
Chris@16
|
341 /**
|
Chris@16
|
342 * Set the season number to which the episode belongs
|
Chris@16
|
343 *
|
Chris@16
|
344 * @param int $number
|
Chris@16
|
345 * @return self
|
Chris@16
|
346 * @throws Writer\Exception\InvalidArgumentException
|
Chris@16
|
347 */
|
Chris@16
|
348 public function setItunesSeason($number)
|
Chris@16
|
349 {
|
Chris@16
|
350 if (! is_numeric($number) || is_float($number)) {
|
Chris@16
|
351 throw new Writer\Exception\InvalidArgumentException(sprintf(
|
Chris@16
|
352 'invalid parameter: "season" may only be an integer; received %s',
|
Chris@16
|
353 is_object($number) ? get_class($number) : gettype($number)
|
Chris@16
|
354 ));
|
Chris@16
|
355 }
|
Chris@16
|
356
|
Chris@16
|
357 $this->data['season'] = (int) $number;
|
Chris@16
|
358
|
Chris@16
|
359 return $this;
|
Chris@16
|
360 }
|
Chris@16
|
361
|
Chris@16
|
362 /**
|
Chris@0
|
363 * Overloading to itunes specific setters
|
Chris@0
|
364 *
|
Chris@0
|
365 * @param string $method
|
Chris@0
|
366 * @param array $params
|
Chris@0
|
367 * @throws Writer\Exception\BadMethodCallException
|
Chris@0
|
368 * @return mixed
|
Chris@0
|
369 */
|
Chris@0
|
370 public function __call($method, array $params)
|
Chris@0
|
371 {
|
Chris@0
|
372 $point = lcfirst(substr($method, 9));
|
Chris@12
|
373 if (! method_exists($this, 'setItunes' . ucfirst($point))
|
Chris@12
|
374 && ! method_exists($this, 'addItunes' . ucfirst($point))
|
Chris@0
|
375 ) {
|
Chris@0
|
376 throw new Writer\Exception\BadMethodCallException(
|
Chris@0
|
377 'invalid method: ' . $method
|
Chris@0
|
378 );
|
Chris@0
|
379 }
|
Chris@12
|
380 if (! array_key_exists($point, $this->data)
|
Chris@0
|
381 || empty($this->data[$point])
|
Chris@0
|
382 ) {
|
Chris@0
|
383 return;
|
Chris@0
|
384 }
|
Chris@0
|
385 return $this->data[$point];
|
Chris@0
|
386 }
|
Chris@0
|
387 }
|