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
10 namespace Zend\Feed\Writer\Extension\ITunes;
14 use Zend\Stdlib\StringUtils;
15 use Zend\Stdlib\StringWrapper\StringWrapperInterface;
20 * Array of Feed data for rendering by Extension's renderers
27 * Encoding of all text values
31 protected $encoding = 'UTF-8';
34 * The used string wrapper supporting encoding
36 * @var StringWrapperInterface
38 protected $stringWrapper;
43 public function __construct()
45 $this->stringWrapper = StringUtils::getWrapper($this->encoding);
54 public function setEncoding($enc)
56 $this->stringWrapper = StringUtils::getWrapper($enc);
57 $this->encoding = $enc;
66 public function getEncoding()
68 return $this->encoding;
72 * Set a block value of "yes" or "no". You may also set an empty string.
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;
95 * @param array $values
98 public function addItunesAuthors(array $values)
100 foreach ($values as $value) {
101 $this->addItunesAuthor($value);
109 * @param string $value
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;
127 * Set feed categories
129 * @param array $values
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;
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;
164 * Set feed image (icon)
166 * @param string $value
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;
186 * Set feed cumulative duration
188 * @param string $value
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;
207 * Set "explicit" flag
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;
226 * @deprecated since 2.10.0; itunes:keywords is no longer part of the
227 * iTunes podcast RSS specification.
228 * @param array $value
230 * @throws Writer\Exception\InvalidArgumentException
232 public function setItunesKeywords(array $value)
235 'itunes:keywords has been deprecated in the iTunes podcast RSS specification,'
236 . ' and should not be relied on.',
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'
250 $this->data['keywords'] = $value;
257 * @param string $value
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;
274 * @param array $values
277 public function addItunesOwners(array $values)
279 foreach ($values as $value) {
280 $this->addItunesOwner($value);
288 * @param array $value
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;
314 * @param string $value
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;
331 * @param string $value
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;
348 * @param string $type
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;
367 * Set "completion" status (whether more episodes will be released)
369 * @param bool $status
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)
386 $this->data['complete'] = 'Yes';
391 * Overloading: proxy to internal setters
393 * @param string $method
394 * @param array $params
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])) {
412 return $this->data[$point];