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