2acf80fc9be99a841608ba0a1b680122682698d1
[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 (! 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      * @param  array $value
227      * @return Feed
228      * @throws Writer\Exception\InvalidArgumentException
229      */
230     public function setItunesKeywords(array $value)
231     {
232         if (count($value) > 12) {
233             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
234             . ' contain a maximum of 12 terms');
235         }
236         $concat = implode(',', $value);
237         if ($this->stringWrapper->strlen($concat) > 255) {
238             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
239             . ' have a concatenated length of 255 chars where terms are delimited'
240             . ' by a comma');
241         }
242         $this->data['keywords'] = $value;
243         return $this;
244     }
245
246     /**
247      * Set new feed URL
248      *
249      * @param  string $value
250      * @return Feed
251      * @throws Writer\Exception\InvalidArgumentException
252      */
253     public function setItunesNewFeedUrl($value)
254     {
255         if (! Uri::factory($value)->isValid()) {
256             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "newFeedUrl" may only'
257             . ' be a valid URI/IRI');
258         }
259         $this->data['newFeedUrl'] = $value;
260         return $this;
261     }
262
263     /**
264      * Add feed owners
265      *
266      * @param  array $values
267      * @return Feed
268      */
269     public function addItunesOwners(array $values)
270     {
271         foreach ($values as $value) {
272             $this->addItunesOwner($value);
273         }
274         return $this;
275     }
276
277     /**
278      * Add feed owner
279      *
280      * @param  array $value
281      * @return Feed
282      * @throws Writer\Exception\InvalidArgumentException
283      */
284     public function addItunesOwner(array $value)
285     {
286         if (! isset($value['name']) || ! isset($value['email'])) {
287             throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "owner" must'
288             . ' be an array containing keys "name" and "email"');
289         }
290         if ($this->stringWrapper->strlen($value['name']) > 255
291             || $this->stringWrapper->strlen($value['email']) > 255
292         ) {
293             throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "owner" may only'
294             . ' contain a maximum of 255 characters each for "name" and "email"');
295         }
296         if (! isset($this->data['owners'])) {
297             $this->data['owners'] = [];
298         }
299         $this->data['owners'][] = $value;
300         return $this;
301     }
302
303     /**
304      * Set feed subtitle
305      *
306      * @param  string $value
307      * @return Feed
308      * @throws Writer\Exception\InvalidArgumentException
309      */
310     public function setItunesSubtitle($value)
311     {
312         if ($this->stringWrapper->strlen($value) > 255) {
313             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "subtitle" may only'
314             . ' contain a maximum of 255 characters');
315         }
316         $this->data['subtitle'] = $value;
317         return $this;
318     }
319
320     /**
321      * Set feed summary
322      *
323      * @param  string $value
324      * @return Feed
325      * @throws Writer\Exception\InvalidArgumentException
326      */
327     public function setItunesSummary($value)
328     {
329         if ($this->stringWrapper->strlen($value) > 4000) {
330             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "summary" may only'
331             . ' contain a maximum of 4000 characters');
332         }
333         $this->data['summary'] = $value;
334         return $this;
335     }
336
337     /**
338      * Overloading: proxy to internal setters
339      *
340      * @param  string $method
341      * @param  array $params
342      * @return mixed
343      * @throws Writer\Exception\BadMethodCallException
344      */
345     public function __call($method, array $params)
346     {
347         $point = lcfirst(substr($method, 9));
348         if (! method_exists($this, 'setItunes' . ucfirst($point))
349             && ! method_exists($this, 'addItunes' . ucfirst($point))
350         ) {
351             throw new Writer\Exception\BadMethodCallException(
352                 'invalid method: ' . $method
353             );
354         }
355         if (! array_key_exists($point, $this->data) || empty($this->data[$point])) {
356             return;
357         }
358         return $this->data[$point];
359     }
360 }