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