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 (! 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 * @param array $value
228 * @throws Writer\Exception\InvalidArgumentException
230 public function setItunesKeywords(array $value)
232 if (count($value) > 12) {
233 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
234 . ' contain a maximum of 12 terms');
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'
242 $this->data['keywords'] = $value;
249 * @param string $value
251 * @throws Writer\Exception\InvalidArgumentException
253 public function setItunesNewFeedUrl($value)
255 if (! Uri::factory($value)->isValid()) {
256 throw new Writer\Exception\InvalidArgumentException('invalid parameter: "newFeedUrl" may only'
257 . ' be a valid URI/IRI');
259 $this->data['newFeedUrl'] = $value;
266 * @param array $values
269 public function addItunesOwners(array $values)
271 foreach ($values as $value) {
272 $this->addItunesOwner($value);
280 * @param array $value
282 * @throws Writer\Exception\InvalidArgumentException
284 public function addItunesOwner(array $value)
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"');
290 if ($this->stringWrapper->strlen($value['name']) > 255
291 || $this->stringWrapper->strlen($value['email']) > 255
293 throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "owner" may only'
294 . ' contain a maximum of 255 characters each for "name" and "email"');
296 if (! isset($this->data['owners'])) {
297 $this->data['owners'] = [];
299 $this->data['owners'][] = $value;
306 * @param string $value
308 * @throws Writer\Exception\InvalidArgumentException
310 public function setItunesSubtitle($value)
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');
316 $this->data['subtitle'] = $value;
323 * @param string $value
325 * @throws Writer\Exception\InvalidArgumentException
327 public function setItunesSummary($value)
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');
333 $this->data['summary'] = $value;
338 * Overloading: proxy to internal setters
340 * @param string $method
341 * @param array $params
343 * @throws Writer\Exception\BadMethodCallException
345 public function __call($method, array $params)
347 $point = lcfirst(substr($method, 9));
348 if (! method_exists($this, 'setItunes' . ucfirst($point))
349 && ! method_exists($this, 'addItunes' . ucfirst($point))
351 throw new Writer\Exception\BadMethodCallException(
352 'invalid method: ' . $method
355 if (! array_key_exists($point, $this->data) || empty($this->data[$point])) {
358 return $this->data[$point];