3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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
;
22 * Array of Feed data for rendering by Extension's renderers
26 protected $data = array();
29 * Encoding of all text values
33 protected $encoding = 'UTF-8';
36 * The used string wrapper supporting encoding
38 * @var StringWrapperInterface
40 protected $stringWrapper;
45 public function __construct()
47 $this->stringWrapper
= StringUtils
::getWrapper($this->encoding
);
56 public function setEncoding($enc)
58 $this->stringWrapper
= StringUtils
::getWrapper($enc);
59 $this->encoding
= $enc;
68 public function getEncoding()
70 return $this->encoding
;
74 * Set a block value of "yes" or "no". You may also set an empty string.
78 * @throws Writer\Exception\InvalidArgumentException
80 public function setItunesBlock($value)
82 if (!ctype_alpha($value) && strlen($value) > 0) {
83 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "block" may only'
84 . ' contain alphabetic characters');
86 if ($this->stringWrapper
->strlen($value) > 255) {
87 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "block" may only'
88 . ' contain a maximum of 255 characters');
90 $this->data
['block'] = $value;
97 * @param array $values
100 public function addItunesAuthors(array $values)
102 foreach ($values as $value) {
103 $this->addItunesAuthor($value);
111 * @param string $value
113 * @throws Writer\Exception\InvalidArgumentException
115 public function addItunesAuthor($value)
117 if ($this->stringWrapper
->strlen($value) > 255) {
118 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: any "author" may only'
119 . ' contain a maximum of 255 characters each');
121 if (!isset($this->data
['authors'])) {
122 $this->data
['authors'] = array();
124 $this->data
['authors'][] = $value;
129 * Set feed categories
131 * @param array $values
133 * @throws Writer\Exception\InvalidArgumentException
135 public function setItunesCategories(array $values)
137 if (!isset($this->data
['categories'])) {
138 $this->data
['categories'] = array();
140 foreach ($values as $key => $value) {
141 if (!is_array($value)) {
142 if ($this->stringWrapper
->strlen($value) > 255) {
143 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: any "category" may only'
144 . ' contain a maximum of 255 characters each');
146 $this->data
['categories'][] = $value;
148 if ($this->stringWrapper
->strlen($key) > 255) {
149 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: any "category" may only'
150 . ' contain a maximum of 255 characters each');
152 $this->data
['categories'][$key] = array();
153 foreach ($value as $val) {
154 if ($this->stringWrapper
->strlen($val) > 255) {
155 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: any "category" may only'
156 . ' contain a maximum of 255 characters each');
158 $this->data
['categories'][$key][] = $val;
166 * Set feed image (icon)
168 * @param string $value
170 * @throws Writer\Exception\InvalidArgumentException
172 public function setItunesImage($value)
174 if (!Uri
::factory($value)->isValid()) {
175 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "image" may only'
176 . ' be a valid URI/IRI');
178 if (!in_array(substr($value, -3), array('jpg', 'png'))) {
179 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "image" may only'
180 . ' use file extension "jpg" or "png" which must be the last three'
181 . ' characters of the URI (i.e. no query string or fragment)');
183 $this->data
['image'] = $value;
188 * Set feed cumulative duration
190 * @param string $value
192 * @throws Writer\Exception\InvalidArgumentException
194 public function setItunesDuration($value)
196 $value = (string) $value;
197 if (!ctype_digit($value)
198 && !preg_match("/^\d+:[0-5]{1}[0-9]{1}$/", $value)
199 && !preg_match("/^\d+:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/", $value)
201 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "duration" may only'
202 . ' be of a specified [[HH:]MM:]SS format');
204 $this->data
['duration'] = $value;
209 * Set "explicit" flag
213 * @throws Writer\Exception\InvalidArgumentException
215 public function setItunesExplicit($value)
217 if (!in_array($value, array('yes', 'no', 'clean'))) {
218 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "explicit" may only'
219 . ' be one of "yes", "no" or "clean"');
221 $this->data
['explicit'] = $value;
228 * @param array $value
230 * @throws Writer\Exception\InvalidArgumentException
232 public function setItunesKeywords(array $value)
234 if (count($value) > 12) {
235 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "keywords" may only'
236 . ' contain a maximum of 12 terms');
238 $concat = implode(',', $value);
239 if ($this->stringWrapper
->strlen($concat) > 255) {
240 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "keywords" may only'
241 . ' have a concatenated length of 255 chars where terms are delimited'
244 $this->data
['keywords'] = $value;
251 * @param string $value
253 * @throws Writer\Exception\InvalidArgumentException
255 public function setItunesNewFeedUrl($value)
257 if (!Uri
::factory($value)->isValid()) {
258 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "newFeedUrl" may only'
259 . ' be a valid URI/IRI');
261 $this->data
['newFeedUrl'] = $value;
268 * @param array $values
271 public function addItunesOwners(array $values)
273 foreach ($values as $value) {
274 $this->addItunesOwner($value);
282 * @param array $value
284 * @throws Writer\Exception\InvalidArgumentException
286 public function addItunesOwner(array $value)
288 if (!isset($value['name']) ||
!isset($value['email'])) {
289 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: any "owner" must'
290 . ' be an array containing keys "name" and "email"');
292 if ($this->stringWrapper
->strlen($value['name']) > 255
293 ||
$this->stringWrapper
->strlen($value['email']) > 255
295 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: any "owner" may only'
296 . ' contain a maximum of 255 characters each for "name" and "email"');
298 if (!isset($this->data
['owners'])) {
299 $this->data
['owners'] = array();
301 $this->data
['owners'][] = $value;
308 * @param string $value
310 * @throws Writer\Exception\InvalidArgumentException
312 public function setItunesSubtitle($value)
314 if ($this->stringWrapper
->strlen($value) > 255) {
315 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "subtitle" may only'
316 . ' contain a maximum of 255 characters');
318 $this->data
['subtitle'] = $value;
325 * @param string $value
327 * @throws Writer\Exception\InvalidArgumentException
329 public function setItunesSummary($value)
331 if ($this->stringWrapper
->strlen($value) > 4000) {
332 throw new Writer\Exception\
InvalidArgumentException('invalid parameter: "summary" may only'
333 . ' contain a maximum of 4000 characters');
335 $this->data
['summary'] = $value;
340 * Overloading: proxy to internal setters
342 * @param string $method
343 * @param array $params
345 * @throws Writer\Exception\BadMethodCallException
347 public function __call($method, array $params)
349 $point = lcfirst(substr($method, 9));
350 if (!method_exists($this, 'setItunes' . ucfirst($point))
351 && !method_exists($this, 'addItunes' . ucfirst($point))
353 throw new Writer\Exception\
BadMethodCallException(
354 'invalid method: ' . $method
357 if (!array_key_exists($point, $this->data
) ||
empty($this->data
[$point])) {
360 return $this->data
[$point];