c9228307ae904a1d0f28b8862c8c65d0f9dc47c2
[yaffs-website] / vendor / zendframework / zend-feed / src / Writer / Extension / ITunes / Entry.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\Writer;
13 use Zend\Stdlib\StringUtils;
14 use Zend\Stdlib\StringWrapper\StringWrapperInterface;
15
16 /**
17 */
18 class Entry
19 {
20     /**
21      * Array of Feed data for rendering by Extension's renderers
22      *
23      * @var array
24      */
25     protected $data = [];
26
27     /**
28      * Encoding of all text values
29      *
30      * @var string
31      */
32     protected $encoding = 'UTF-8';
33
34     /**
35      * The used string wrapper supporting encoding
36      *
37      * @var StringWrapperInterface
38      */
39     protected $stringWrapper;
40
41     public function __construct()
42     {
43         $this->stringWrapper = StringUtils::getWrapper($this->encoding);
44     }
45
46     /**
47      * Set feed encoding
48      *
49      * @param  string $enc
50      * @return Entry
51      */
52     public function setEncoding($enc)
53     {
54         $this->stringWrapper = StringUtils::getWrapper($enc);
55         $this->encoding      = $enc;
56         return $this;
57     }
58
59     /**
60      * Get feed encoding
61      *
62      * @return string
63      */
64     public function getEncoding()
65     {
66         return $this->encoding;
67     }
68
69     /**
70      * Set a block value of "yes" or "no". You may also set an empty string.
71      *
72      * @param  string
73      * @return Entry
74      * @throws Writer\Exception\InvalidArgumentException
75      */
76     public function setItunesBlock($value)
77     {
78         if (! ctype_alpha($value) && strlen($value) > 0) {
79             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only'
80             . ' contain alphabetic characters');
81         }
82
83         if ($this->stringWrapper->strlen($value) > 255) {
84             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only'
85             . ' contain a maximum of 255 characters');
86         }
87         $this->data['block'] = $value;
88     }
89
90     /**
91      * Add authors to itunes entry
92      *
93      * @param  array $values
94      * @return Entry
95      */
96     public function addItunesAuthors(array $values)
97     {
98         foreach ($values as $value) {
99             $this->addItunesAuthor($value);
100         }
101         return $this;
102     }
103
104     /**
105      * Add author to itunes entry
106      *
107      * @param  string $value
108      * @return Entry
109      * @throws Writer\Exception\InvalidArgumentException
110      */
111     public function addItunesAuthor($value)
112     {
113         if ($this->stringWrapper->strlen($value) > 255) {
114             throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "author" may only'
115             . ' contain a maximum of 255 characters each');
116         }
117         if (! isset($this->data['authors'])) {
118             $this->data['authors'] = [];
119         }
120         $this->data['authors'][] = $value;
121         return $this;
122     }
123
124     /**
125      * Set duration
126      *
127      * @param  int $value
128      * @return Entry
129      * @throws Writer\Exception\InvalidArgumentException
130      */
131     public function setItunesDuration($value)
132     {
133         $value = (string) $value;
134         if (! ctype_digit($value)
135             && ! preg_match("/^\d+:[0-5]{1}[0-9]{1}$/", $value)
136             && ! preg_match("/^\d+:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/", $value)
137         ) {
138             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "duration" may only'
139             . ' be of a specified [[HH:]MM:]SS format');
140         }
141         $this->data['duration'] = $value;
142         return $this;
143     }
144
145     /**
146      * Set "explicit" flag
147      *
148      * @param  bool $value
149      * @return Entry
150      * @throws Writer\Exception\InvalidArgumentException
151      */
152     public function setItunesExplicit($value)
153     {
154         if (! in_array($value, ['yes', 'no', 'clean'])) {
155             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "explicit" may only'
156             . ' be one of "yes", "no" or "clean"');
157         }
158         $this->data['explicit'] = $value;
159         return $this;
160     }
161
162     /**
163      * Set keywords
164      *
165      * @param  array $value
166      * @return Entry
167      * @throws Writer\Exception\InvalidArgumentException
168      */
169     public function setItunesKeywords(array $value)
170     {
171         if (count($value) > 12) {
172             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
173             . ' contain a maximum of 12 terms');
174         }
175
176         $concat = implode(',', $value);
177         if ($this->stringWrapper->strlen($concat) > 255) {
178             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
179             . ' have a concatenated length of 255 chars where terms are delimited'
180             . ' by a comma');
181         }
182         $this->data['keywords'] = $value;
183         return $this;
184     }
185
186     /**
187      * Set subtitle
188      *
189      * @param  string $value
190      * @return Entry
191      * @throws Writer\Exception\InvalidArgumentException
192      */
193     public function setItunesSubtitle($value)
194     {
195         if ($this->stringWrapper->strlen($value) > 255) {
196             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "subtitle" may only'
197             . ' contain a maximum of 255 characters');
198         }
199         $this->data['subtitle'] = $value;
200         return $this;
201     }
202
203     /**
204      * Set summary
205      *
206      * @param  string $value
207      * @return Entry
208      * @throws Writer\Exception\InvalidArgumentException
209      */
210     public function setItunesSummary($value)
211     {
212         if ($this->stringWrapper->strlen($value) > 4000) {
213             throw new Writer\Exception\InvalidArgumentException('invalid parameter: "summary" may only'
214             . ' contain a maximum of 4000 characters');
215         }
216         $this->data['summary'] = $value;
217         return $this;
218     }
219
220     /**
221      * Overloading to itunes specific setters
222      *
223      * @param  string $method
224      * @param  array $params
225      * @throws Writer\Exception\BadMethodCallException
226      * @return mixed
227      */
228     public function __call($method, array $params)
229     {
230         $point = lcfirst(substr($method, 9));
231         if (! method_exists($this, 'setItunes' . ucfirst($point))
232             && ! method_exists($this, 'addItunes' . ucfirst($point))
233         ) {
234             throw new Writer\Exception\BadMethodCallException(
235                 'invalid method: ' . $method
236             );
237         }
238         if (! array_key_exists($point, $this->data)
239             || empty($this->data[$point])
240         ) {
241             return;
242         }
243         return $this->data[$point];
244     }
245 }