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