composer package updates
[openemr.git] / vendor / zendframework / zend-feed / src / Writer / Extension / ITunes / Feed.php
blob3aa30995b01d6605935300b12d719138cc087077
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
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 */
10 namespace Zend\Feed\Writer\Extension\ITunes;
12 use Zend\Feed\Uri;
13 use Zend\Feed\Writer;
14 use Zend\Stdlib\StringUtils;
15 use Zend\Stdlib\StringWrapper\StringWrapperInterface;
17 class Feed
19 /**
20 * Array of Feed data for rendering by Extension's renderers
22 * @var array
24 protected $data = [];
26 /**
27 * Encoding of all text values
29 * @var string
31 protected $encoding = 'UTF-8';
33 /**
34 * The used string wrapper supporting encoding
36 * @var StringWrapperInterface
38 protected $stringWrapper;
40 /**
41 * Constructor
43 public function __construct()
45 $this->stringWrapper = StringUtils::getWrapper($this->encoding);
48 /**
49 * Set feed encoding
51 * @param string $enc
52 * @return Feed
54 public function setEncoding($enc)
56 $this->stringWrapper = StringUtils::getWrapper($enc);
57 $this->encoding = $enc;
58 return $this;
61 /**
62 * Get feed encoding
64 * @return string
66 public function getEncoding()
68 return $this->encoding;
71 /**
72 * Set a block value of "yes" or "no". You may also set an empty string.
74 * @param string
75 * @return Feed
76 * @throws Writer\Exception\InvalidArgumentException
78 public function setItunesBlock($value)
80 if (! ctype_alpha($value) && strlen($value) > 0) {
81 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only'
82 . ' contain alphabetic characters');
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');
88 $this->data['block'] = $value;
89 return $this;
92 /**
93 * Add feed authors
95 * @param array $values
96 * @return Feed
98 public function addItunesAuthors(array $values)
100 foreach ($values as $value) {
101 $this->addItunesAuthor($value);
103 return $this;
107 * Add feed author
109 * @param string $value
110 * @return Feed
111 * @throws Writer\Exception\InvalidArgumentException
113 public function addItunesAuthor($value)
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');
119 if (! isset($this->data['authors'])) {
120 $this->data['authors'] = [];
122 $this->data['authors'][] = $value;
123 return $this;
127 * Set feed categories
129 * @param array $values
130 * @return Feed
131 * @throws Writer\Exception\InvalidArgumentException
133 public function setItunesCategories(array $values)
135 if (! isset($this->data['categories'])) {
136 $this->data['categories'] = [];
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');
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');
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');
156 $this->data['categories'][$key][] = $val;
160 return $this;
164 * Set feed image (icon)
166 * @param string $value
167 * @return Feed
168 * @throws Writer\Exception\InvalidArgumentException
170 public function setItunesImage($value)
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');
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)');
181 $this->data['image'] = $value;
182 return $this;
186 * Set feed cumulative duration
188 * @param string $value
189 * @return Feed
190 * @throws Writer\Exception\InvalidArgumentException
192 public function setItunesDuration($value)
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)
199 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "duration" may only'
200 . ' be of a specified [[HH:]MM:]SS format');
202 $this->data['duration'] = $value;
203 return $this;
207 * Set "explicit" flag
209 * @param bool $value
210 * @return Feed
211 * @throws Writer\Exception\InvalidArgumentException
213 public function setItunesExplicit($value)
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"');
219 $this->data['explicit'] = $value;
220 return $this;
224 * Set feed keywords
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
232 public function setItunesKeywords(array $value)
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
240 if (count($value) > 12) {
241 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
242 . ' contain a maximum of 12 terms');
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');
250 $this->data['keywords'] = $value;
251 return $this;
255 * Set new feed URL
257 * @param string $value
258 * @return Feed
259 * @throws Writer\Exception\InvalidArgumentException
261 public function setItunesNewFeedUrl($value)
263 if (! Uri::factory($value)->isValid()) {
264 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "newFeedUrl" may only'
265 . ' be a valid URI/IRI');
267 $this->data['newFeedUrl'] = $value;
268 return $this;
272 * Add feed owners
274 * @param array $values
275 * @return Feed
277 public function addItunesOwners(array $values)
279 foreach ($values as $value) {
280 $this->addItunesOwner($value);
282 return $this;
286 * Add feed owner
288 * @param array $value
289 * @return Feed
290 * @throws Writer\Exception\InvalidArgumentException
292 public function addItunesOwner(array $value)
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"');
298 if ($this->stringWrapper->strlen($value['name']) > 255
299 || $this->stringWrapper->strlen($value['email']) > 255
301 throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "owner" may only'
302 . ' contain a maximum of 255 characters each for "name" and "email"');
304 if (! isset($this->data['owners'])) {
305 $this->data['owners'] = [];
307 $this->data['owners'][] = $value;
308 return $this;
312 * Set feed subtitle
314 * @param string $value
315 * @return Feed
316 * @throws Writer\Exception\InvalidArgumentException
318 public function setItunesSubtitle($value)
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');
324 $this->data['subtitle'] = $value;
325 return $this;
329 * Set feed summary
331 * @param string $value
332 * @return Feed
333 * @throws Writer\Exception\InvalidArgumentException
335 public function setItunesSummary($value)
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');
341 $this->data['summary'] = $value;
342 return $this;
346 * Set podcast type
348 * @param string $type
349 * @return Feed
350 * @throws Writer\Exception\InvalidArgumentException
352 public function setItunesType($type)
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)
362 $this->data['type'] = $type;
363 return $this;
367 * Set "completion" status (whether more episodes will be released)
369 * @param bool $status
370 * @return Feed
371 * @throws Writer\Exception\InvalidArgumentException
373 public function setItunesComplete($status)
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)
382 if (! $status) {
383 return $this;
386 $this->data['complete'] = 'Yes';
387 return $this;
391 * Overloading: proxy to internal setters
393 * @param string $method
394 * @param array $params
395 * @return mixed
396 * @throws Writer\Exception\BadMethodCallException
398 public function __call($method, array $params)
400 $point = lcfirst(substr($method, 9));
401 if (! method_exists($this, 'setItunes' . ucfirst($point))
402 && ! method_exists($this, 'addItunes' . ucfirst($point))
404 throw new Writer\Exception\BadMethodCallException(
405 'invalid method: ' . $method
409 if (! array_key_exists($point, $this->data) || empty($this->data[$point])) {
410 return;
412 return $this->data[$point];