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