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;
|
Chris@0
|
11
|
Chris@0
|
12 use DateTime;
|
Chris@0
|
13 use Zend\Feed\Uri;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 */
|
Chris@0
|
17 class Entry
|
Chris@0
|
18 {
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Internal array containing all data associated with this entry or item.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var array
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $data = [];
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Registered extensions
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var array
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $extensions = [];
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Holds the value "atom" or "rss" depending on the feed type set when
|
Chris@0
|
35 * when last exported.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $type = null;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Constructor: Primarily triggers the registration of core extensions and
|
Chris@0
|
43 * loads those appropriate to this data container.
|
Chris@0
|
44 *
|
Chris@0
|
45 */
|
Chris@0
|
46 public function __construct()
|
Chris@0
|
47 {
|
Chris@0
|
48 Writer::registerCoreExtensions();
|
Chris@0
|
49 $this->_loadExtensions();
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Set a single author
|
Chris@0
|
54 *
|
Chris@0
|
55 * The following option keys are supported:
|
Chris@0
|
56 * 'name' => (string) The name
|
Chris@0
|
57 * 'email' => (string) An optional email
|
Chris@0
|
58 * 'uri' => (string) An optional and valid URI
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param array $author
|
Chris@0
|
61 * @throws Exception\InvalidArgumentException If any value of $author not follow the format.
|
Chris@0
|
62 * @return Entry
|
Chris@0
|
63 */
|
Chris@0
|
64 public function addAuthor(array $author)
|
Chris@0
|
65 {
|
Chris@0
|
66 // Check array values
|
Chris@0
|
67 if (!array_key_exists('name', $author)
|
Chris@0
|
68 || empty($author['name'])
|
Chris@0
|
69 || !is_string($author['name'])
|
Chris@0
|
70 ) {
|
Chris@0
|
71 throw new Exception\InvalidArgumentException(
|
Chris@0
|
72 'Invalid parameter: author array must include a "name" key with a non-empty string value');
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 if (isset($author['email'])) {
|
Chris@0
|
76 if (empty($author['email']) || !is_string($author['email'])) {
|
Chris@0
|
77 throw new Exception\InvalidArgumentException(
|
Chris@0
|
78 'Invalid parameter: "email" array value must be a non-empty string');
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81 if (isset($author['uri'])) {
|
Chris@0
|
82 if (empty($author['uri']) || !is_string($author['uri']) ||
|
Chris@0
|
83 !Uri::factory($author['uri'])->isValid()
|
Chris@0
|
84 ) {
|
Chris@0
|
85 throw new Exception\InvalidArgumentException(
|
Chris@0
|
86 'Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI');
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 $this->data['authors'][] = $author;
|
Chris@0
|
91
|
Chris@0
|
92 return $this;
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Set an array with feed authors
|
Chris@0
|
97 *
|
Chris@0
|
98 * @see addAuthor
|
Chris@0
|
99 * @param array $authors
|
Chris@0
|
100 * @return Entry
|
Chris@0
|
101 */
|
Chris@0
|
102 public function addAuthors(array $authors)
|
Chris@0
|
103 {
|
Chris@0
|
104 foreach ($authors as $author) {
|
Chris@0
|
105 $this->addAuthor($author);
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 return $this;
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Set the feed character encoding
|
Chris@0
|
113 *
|
Chris@0
|
114 * @param string $encoding
|
Chris@0
|
115 * @throws Exception\InvalidArgumentException
|
Chris@0
|
116 * @return Entry
|
Chris@0
|
117 */
|
Chris@0
|
118 public function setEncoding($encoding)
|
Chris@0
|
119 {
|
Chris@0
|
120 if (empty($encoding) || !is_string($encoding)) {
|
Chris@0
|
121 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
|
Chris@0
|
122 }
|
Chris@0
|
123 $this->data['encoding'] = $encoding;
|
Chris@0
|
124
|
Chris@0
|
125 return $this;
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Get the feed character encoding
|
Chris@0
|
130 *
|
Chris@0
|
131 * @return string|null
|
Chris@0
|
132 */
|
Chris@0
|
133 public function getEncoding()
|
Chris@0
|
134 {
|
Chris@0
|
135 if (!array_key_exists('encoding', $this->data)) {
|
Chris@0
|
136 return 'UTF-8';
|
Chris@0
|
137 }
|
Chris@0
|
138 return $this->data['encoding'];
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * Set the copyright entry
|
Chris@0
|
143 *
|
Chris@0
|
144 * @param string $copyright
|
Chris@0
|
145 * @throws Exception\InvalidArgumentException
|
Chris@0
|
146 * @return Entry
|
Chris@0
|
147 */
|
Chris@0
|
148 public function setCopyright($copyright)
|
Chris@0
|
149 {
|
Chris@0
|
150 if (empty($copyright) || !is_string($copyright)) {
|
Chris@0
|
151 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
|
Chris@0
|
152 }
|
Chris@0
|
153 $this->data['copyright'] = $copyright;
|
Chris@0
|
154
|
Chris@0
|
155 return $this;
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 * Set the entry's content
|
Chris@0
|
160 *
|
Chris@0
|
161 * @param string $content
|
Chris@0
|
162 * @throws Exception\InvalidArgumentException
|
Chris@0
|
163 * @return Entry
|
Chris@0
|
164 */
|
Chris@0
|
165 public function setContent($content)
|
Chris@0
|
166 {
|
Chris@0
|
167 if (empty($content) || !is_string($content)) {
|
Chris@0
|
168 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
|
Chris@0
|
169 }
|
Chris@0
|
170 $this->data['content'] = $content;
|
Chris@0
|
171
|
Chris@0
|
172 return $this;
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@0
|
176 * Set the feed creation date
|
Chris@0
|
177 *
|
Chris@0
|
178 * @param null|int|DateTime $date
|
Chris@0
|
179 * @throws Exception\InvalidArgumentException
|
Chris@0
|
180 * @return Entry
|
Chris@0
|
181 */
|
Chris@0
|
182 public function setDateCreated($date = null)
|
Chris@0
|
183 {
|
Chris@0
|
184 if ($date === null) {
|
Chris@0
|
185 $date = new DateTime();
|
Chris@0
|
186 } elseif (is_int($date)) {
|
Chris@0
|
187 $date = new DateTime('@' . $date);
|
Chris@0
|
188 } elseif (!$date instanceof DateTime) {
|
Chris@0
|
189 throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp passed as parameter');
|
Chris@0
|
190 }
|
Chris@0
|
191 $this->data['dateCreated'] = $date;
|
Chris@0
|
192
|
Chris@0
|
193 return $this;
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Set the feed modification date
|
Chris@0
|
198 *
|
Chris@0
|
199 * @param null|int|DateTime $date
|
Chris@0
|
200 * @throws Exception\InvalidArgumentException
|
Chris@0
|
201 * @return Entry
|
Chris@0
|
202 */
|
Chris@0
|
203 public function setDateModified($date = null)
|
Chris@0
|
204 {
|
Chris@0
|
205 if ($date === null) {
|
Chris@0
|
206 $date = new DateTime();
|
Chris@0
|
207 } elseif (is_int($date)) {
|
Chris@0
|
208 $date = new DateTime('@' . $date);
|
Chris@0
|
209 } elseif (!$date instanceof DateTime) {
|
Chris@0
|
210 throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp passed as parameter');
|
Chris@0
|
211 }
|
Chris@0
|
212 $this->data['dateModified'] = $date;
|
Chris@0
|
213
|
Chris@0
|
214 return $this;
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 /**
|
Chris@0
|
218 * Set the feed description
|
Chris@0
|
219 *
|
Chris@0
|
220 * @param string $description
|
Chris@0
|
221 * @throws Exception\InvalidArgumentException
|
Chris@0
|
222 * @return Entry
|
Chris@0
|
223 */
|
Chris@0
|
224 public function setDescription($description)
|
Chris@0
|
225 {
|
Chris@0
|
226 if (empty($description) || !is_string($description)) {
|
Chris@0
|
227 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
|
Chris@0
|
228 }
|
Chris@0
|
229 $this->data['description'] = $description;
|
Chris@0
|
230
|
Chris@0
|
231 return $this;
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 /**
|
Chris@0
|
235 * Set the feed ID
|
Chris@0
|
236 *
|
Chris@0
|
237 * @param string $id
|
Chris@0
|
238 * @throws Exception\InvalidArgumentException
|
Chris@0
|
239 * @return Entry
|
Chris@0
|
240 */
|
Chris@0
|
241 public function setId($id)
|
Chris@0
|
242 {
|
Chris@0
|
243 if (empty($id) || !is_string($id)) {
|
Chris@0
|
244 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
|
Chris@0
|
245 }
|
Chris@0
|
246 $this->data['id'] = $id;
|
Chris@0
|
247
|
Chris@0
|
248 return $this;
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 /**
|
Chris@0
|
252 * Set a link to the HTML source of this entry
|
Chris@0
|
253 *
|
Chris@0
|
254 * @param string $link
|
Chris@0
|
255 * @throws Exception\InvalidArgumentException
|
Chris@0
|
256 * @return Entry
|
Chris@0
|
257 */
|
Chris@0
|
258 public function setLink($link)
|
Chris@0
|
259 {
|
Chris@0
|
260 if (empty($link) || !is_string($link) || !Uri::factory($link)->isValid()) {
|
Chris@0
|
261 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string and valid URI/IRI');
|
Chris@0
|
262 }
|
Chris@0
|
263 $this->data['link'] = $link;
|
Chris@0
|
264
|
Chris@0
|
265 return $this;
|
Chris@0
|
266 }
|
Chris@0
|
267
|
Chris@0
|
268 /**
|
Chris@0
|
269 * Set the number of comments associated with this entry
|
Chris@0
|
270 *
|
Chris@0
|
271 * @param int $count
|
Chris@0
|
272 * @throws Exception\InvalidArgumentException
|
Chris@0
|
273 * @return Entry
|
Chris@0
|
274 */
|
Chris@0
|
275 public function setCommentCount($count)
|
Chris@0
|
276 {
|
Chris@0
|
277 if (!is_numeric($count) || (int) $count != $count || (int) $count < 0) {
|
Chris@0
|
278 throw new Exception\InvalidArgumentException('Invalid parameter: "count" must be a positive integer number or zero');
|
Chris@0
|
279 }
|
Chris@0
|
280 $this->data['commentCount'] = (int) $count;
|
Chris@0
|
281
|
Chris@0
|
282 return $this;
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 /**
|
Chris@0
|
286 * Set a link to a HTML page containing comments associated with this entry
|
Chris@0
|
287 *
|
Chris@0
|
288 * @param string $link
|
Chris@0
|
289 * @throws Exception\InvalidArgumentException
|
Chris@0
|
290 * @return Entry
|
Chris@0
|
291 */
|
Chris@0
|
292 public function setCommentLink($link)
|
Chris@0
|
293 {
|
Chris@0
|
294 if (empty($link) || !is_string($link) || !Uri::factory($link)->isValid()) {
|
Chris@0
|
295 throw new Exception\InvalidArgumentException('Invalid parameter: "link" must be a non-empty string and valid URI/IRI');
|
Chris@0
|
296 }
|
Chris@0
|
297 $this->data['commentLink'] = $link;
|
Chris@0
|
298
|
Chris@0
|
299 return $this;
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 /**
|
Chris@0
|
303 * Set a link to an XML feed for any comments associated with this entry
|
Chris@0
|
304 *
|
Chris@0
|
305 * @param array $link
|
Chris@0
|
306 * @throws Exception\InvalidArgumentException
|
Chris@0
|
307 * @return Entry
|
Chris@0
|
308 */
|
Chris@0
|
309 public function setCommentFeedLink(array $link)
|
Chris@0
|
310 {
|
Chris@0
|
311 if (!isset($link['uri']) || !is_string($link['uri']) || !Uri::factory($link['uri'])->isValid()) {
|
Chris@0
|
312 throw new Exception\InvalidArgumentException('Invalid parameter: "link" must be a non-empty string and valid URI/IRI');
|
Chris@0
|
313 }
|
Chris@0
|
314 if (!isset($link['type']) || !in_array($link['type'], ['atom', 'rss', 'rdf'])) {
|
Chris@0
|
315 throw new Exception\InvalidArgumentException('Invalid parameter: "type" must be one'
|
Chris@0
|
316 . ' of "atom", "rss" or "rdf"');
|
Chris@0
|
317 }
|
Chris@0
|
318 if (!isset($this->data['commentFeedLinks'])) {
|
Chris@0
|
319 $this->data['commentFeedLinks'] = [];
|
Chris@0
|
320 }
|
Chris@0
|
321 $this->data['commentFeedLinks'][] = $link;
|
Chris@0
|
322
|
Chris@0
|
323 return $this;
|
Chris@0
|
324 }
|
Chris@0
|
325
|
Chris@0
|
326 /**
|
Chris@0
|
327 * Set a links to an XML feed for any comments associated with this entry.
|
Chris@0
|
328 * Each link is an array with keys "uri" and "type", where type is one of:
|
Chris@0
|
329 * "atom", "rss" or "rdf".
|
Chris@0
|
330 *
|
Chris@0
|
331 * @param array $links
|
Chris@0
|
332 * @return Entry
|
Chris@0
|
333 */
|
Chris@0
|
334 public function setCommentFeedLinks(array $links)
|
Chris@0
|
335 {
|
Chris@0
|
336 foreach ($links as $link) {
|
Chris@0
|
337 $this->setCommentFeedLink($link);
|
Chris@0
|
338 }
|
Chris@0
|
339
|
Chris@0
|
340 return $this;
|
Chris@0
|
341 }
|
Chris@0
|
342
|
Chris@0
|
343 /**
|
Chris@0
|
344 * Set the feed title
|
Chris@0
|
345 *
|
Chris@0
|
346 * @param string $title
|
Chris@0
|
347 * @throws Exception\InvalidArgumentException
|
Chris@0
|
348 * @return Entry
|
Chris@0
|
349 */
|
Chris@0
|
350 public function setTitle($title)
|
Chris@0
|
351 {
|
Chris@0
|
352 if (empty($title) || !is_string($title)) {
|
Chris@0
|
353 throw new Exception\InvalidArgumentException('Invalid parameter: parameter must be a non-empty string');
|
Chris@0
|
354 }
|
Chris@0
|
355 $this->data['title'] = $title;
|
Chris@0
|
356
|
Chris@0
|
357 return $this;
|
Chris@0
|
358 }
|
Chris@0
|
359
|
Chris@0
|
360 /**
|
Chris@0
|
361 * Get an array with feed authors
|
Chris@0
|
362 *
|
Chris@0
|
363 * @return array
|
Chris@0
|
364 */
|
Chris@0
|
365 public function getAuthors()
|
Chris@0
|
366 {
|
Chris@0
|
367 if (!array_key_exists('authors', $this->data)) {
|
Chris@0
|
368 return;
|
Chris@0
|
369 }
|
Chris@0
|
370 return $this->data['authors'];
|
Chris@0
|
371 }
|
Chris@0
|
372
|
Chris@0
|
373 /**
|
Chris@0
|
374 * Get the entry content
|
Chris@0
|
375 *
|
Chris@0
|
376 * @return string
|
Chris@0
|
377 */
|
Chris@0
|
378 public function getContent()
|
Chris@0
|
379 {
|
Chris@0
|
380 if (!array_key_exists('content', $this->data)) {
|
Chris@0
|
381 return;
|
Chris@0
|
382 }
|
Chris@0
|
383 return $this->data['content'];
|
Chris@0
|
384 }
|
Chris@0
|
385
|
Chris@0
|
386 /**
|
Chris@0
|
387 * Get the entry copyright information
|
Chris@0
|
388 *
|
Chris@0
|
389 * @return string
|
Chris@0
|
390 */
|
Chris@0
|
391 public function getCopyright()
|
Chris@0
|
392 {
|
Chris@0
|
393 if (!array_key_exists('copyright', $this->data)) {
|
Chris@0
|
394 return;
|
Chris@0
|
395 }
|
Chris@0
|
396 return $this->data['copyright'];
|
Chris@0
|
397 }
|
Chris@0
|
398
|
Chris@0
|
399 /**
|
Chris@0
|
400 * Get the entry creation date
|
Chris@0
|
401 *
|
Chris@0
|
402 * @return string
|
Chris@0
|
403 */
|
Chris@0
|
404 public function getDateCreated()
|
Chris@0
|
405 {
|
Chris@0
|
406 if (!array_key_exists('dateCreated', $this->data)) {
|
Chris@0
|
407 return;
|
Chris@0
|
408 }
|
Chris@0
|
409 return $this->data['dateCreated'];
|
Chris@0
|
410 }
|
Chris@0
|
411
|
Chris@0
|
412 /**
|
Chris@0
|
413 * Get the entry modification date
|
Chris@0
|
414 *
|
Chris@0
|
415 * @return string
|
Chris@0
|
416 */
|
Chris@0
|
417 public function getDateModified()
|
Chris@0
|
418 {
|
Chris@0
|
419 if (!array_key_exists('dateModified', $this->data)) {
|
Chris@0
|
420 return;
|
Chris@0
|
421 }
|
Chris@0
|
422 return $this->data['dateModified'];
|
Chris@0
|
423 }
|
Chris@0
|
424
|
Chris@0
|
425 /**
|
Chris@0
|
426 * Get the entry description
|
Chris@0
|
427 *
|
Chris@0
|
428 * @return string
|
Chris@0
|
429 */
|
Chris@0
|
430 public function getDescription()
|
Chris@0
|
431 {
|
Chris@0
|
432 if (!array_key_exists('description', $this->data)) {
|
Chris@0
|
433 return;
|
Chris@0
|
434 }
|
Chris@0
|
435 return $this->data['description'];
|
Chris@0
|
436 }
|
Chris@0
|
437
|
Chris@0
|
438 /**
|
Chris@0
|
439 * Get the entry ID
|
Chris@0
|
440 *
|
Chris@0
|
441 * @return string
|
Chris@0
|
442 */
|
Chris@0
|
443 public function getId()
|
Chris@0
|
444 {
|
Chris@0
|
445 if (!array_key_exists('id', $this->data)) {
|
Chris@0
|
446 return;
|
Chris@0
|
447 }
|
Chris@0
|
448 return $this->data['id'];
|
Chris@0
|
449 }
|
Chris@0
|
450
|
Chris@0
|
451 /**
|
Chris@0
|
452 * Get a link to the HTML source
|
Chris@0
|
453 *
|
Chris@0
|
454 * @return string|null
|
Chris@0
|
455 */
|
Chris@0
|
456 public function getLink()
|
Chris@0
|
457 {
|
Chris@0
|
458 if (!array_key_exists('link', $this->data)) {
|
Chris@0
|
459 return;
|
Chris@0
|
460 }
|
Chris@0
|
461 return $this->data['link'];
|
Chris@0
|
462 }
|
Chris@0
|
463
|
Chris@0
|
464
|
Chris@0
|
465 /**
|
Chris@0
|
466 * Get all links
|
Chris@0
|
467 *
|
Chris@0
|
468 * @return array
|
Chris@0
|
469 */
|
Chris@0
|
470 public function getLinks()
|
Chris@0
|
471 {
|
Chris@0
|
472 if (!array_key_exists('links', $this->data)) {
|
Chris@0
|
473 return;
|
Chris@0
|
474 }
|
Chris@0
|
475 return $this->data['links'];
|
Chris@0
|
476 }
|
Chris@0
|
477
|
Chris@0
|
478 /**
|
Chris@0
|
479 * Get the entry title
|
Chris@0
|
480 *
|
Chris@0
|
481 * @return string
|
Chris@0
|
482 */
|
Chris@0
|
483 public function getTitle()
|
Chris@0
|
484 {
|
Chris@0
|
485 if (!array_key_exists('title', $this->data)) {
|
Chris@0
|
486 return;
|
Chris@0
|
487 }
|
Chris@0
|
488 return $this->data['title'];
|
Chris@0
|
489 }
|
Chris@0
|
490
|
Chris@0
|
491 /**
|
Chris@0
|
492 * Get the number of comments/replies for current entry
|
Chris@0
|
493 *
|
Chris@0
|
494 * @return int
|
Chris@0
|
495 */
|
Chris@0
|
496 public function getCommentCount()
|
Chris@0
|
497 {
|
Chris@0
|
498 if (!array_key_exists('commentCount', $this->data)) {
|
Chris@0
|
499 return;
|
Chris@0
|
500 }
|
Chris@0
|
501 return $this->data['commentCount'];
|
Chris@0
|
502 }
|
Chris@0
|
503
|
Chris@0
|
504 /**
|
Chris@0
|
505 * Returns a URI pointing to the HTML page where comments can be made on this entry
|
Chris@0
|
506 *
|
Chris@0
|
507 * @return string
|
Chris@0
|
508 */
|
Chris@0
|
509 public function getCommentLink()
|
Chris@0
|
510 {
|
Chris@0
|
511 if (!array_key_exists('commentLink', $this->data)) {
|
Chris@0
|
512 return;
|
Chris@0
|
513 }
|
Chris@0
|
514 return $this->data['commentLink'];
|
Chris@0
|
515 }
|
Chris@0
|
516
|
Chris@0
|
517 /**
|
Chris@0
|
518 * Returns an array of URIs pointing to a feed of all comments for this entry
|
Chris@0
|
519 * where the array keys indicate the feed type (atom, rss or rdf).
|
Chris@0
|
520 *
|
Chris@0
|
521 * @return string
|
Chris@0
|
522 */
|
Chris@0
|
523 public function getCommentFeedLinks()
|
Chris@0
|
524 {
|
Chris@0
|
525 if (!array_key_exists('commentFeedLinks', $this->data)) {
|
Chris@0
|
526 return;
|
Chris@0
|
527 }
|
Chris@0
|
528 return $this->data['commentFeedLinks'];
|
Chris@0
|
529 }
|
Chris@0
|
530
|
Chris@0
|
531 /**
|
Chris@0
|
532 * Add an entry category
|
Chris@0
|
533 *
|
Chris@0
|
534 * @param array $category
|
Chris@0
|
535 * @throws Exception\InvalidArgumentException
|
Chris@0
|
536 * @return Entry
|
Chris@0
|
537 */
|
Chris@0
|
538 public function addCategory(array $category)
|
Chris@0
|
539 {
|
Chris@0
|
540 if (!isset($category['term'])) {
|
Chris@0
|
541 throw new Exception\InvalidArgumentException('Each category must be an array and '
|
Chris@0
|
542 . 'contain at least a "term" element containing the machine '
|
Chris@0
|
543 . ' readable category name');
|
Chris@0
|
544 }
|
Chris@0
|
545 if (isset($category['scheme'])) {
|
Chris@0
|
546 if (empty($category['scheme'])
|
Chris@0
|
547 || !is_string($category['scheme'])
|
Chris@0
|
548 || !Uri::factory($category['scheme'])->isValid()
|
Chris@0
|
549 ) {
|
Chris@0
|
550 throw new Exception\InvalidArgumentException('The Atom scheme or RSS domain of'
|
Chris@0
|
551 . ' a category must be a valid URI');
|
Chris@0
|
552 }
|
Chris@0
|
553 }
|
Chris@0
|
554 if (!isset($this->data['categories'])) {
|
Chris@0
|
555 $this->data['categories'] = [];
|
Chris@0
|
556 }
|
Chris@0
|
557 $this->data['categories'][] = $category;
|
Chris@0
|
558
|
Chris@0
|
559 return $this;
|
Chris@0
|
560 }
|
Chris@0
|
561
|
Chris@0
|
562 /**
|
Chris@0
|
563 * Set an array of entry categories
|
Chris@0
|
564 *
|
Chris@0
|
565 * @param array $categories
|
Chris@0
|
566 * @return Entry
|
Chris@0
|
567 */
|
Chris@0
|
568 public function addCategories(array $categories)
|
Chris@0
|
569 {
|
Chris@0
|
570 foreach ($categories as $category) {
|
Chris@0
|
571 $this->addCategory($category);
|
Chris@0
|
572 }
|
Chris@0
|
573
|
Chris@0
|
574 return $this;
|
Chris@0
|
575 }
|
Chris@0
|
576
|
Chris@0
|
577 /**
|
Chris@0
|
578 * Get the entry categories
|
Chris@0
|
579 *
|
Chris@0
|
580 * @return string|null
|
Chris@0
|
581 */
|
Chris@0
|
582 public function getCategories()
|
Chris@0
|
583 {
|
Chris@0
|
584 if (!array_key_exists('categories', $this->data)) {
|
Chris@0
|
585 return;
|
Chris@0
|
586 }
|
Chris@0
|
587 return $this->data['categories'];
|
Chris@0
|
588 }
|
Chris@0
|
589
|
Chris@0
|
590 /**
|
Chris@0
|
591 * Adds an enclosure to the entry. The array parameter may contain the
|
Chris@0
|
592 * keys 'uri', 'type' and 'length'. Only 'uri' is required for Atom, though the
|
Chris@0
|
593 * others must also be provided or RSS rendering (where they are required)
|
Chris@0
|
594 * will throw an Exception.
|
Chris@0
|
595 *
|
Chris@0
|
596 * @param array $enclosure
|
Chris@0
|
597 * @throws Exception\InvalidArgumentException
|
Chris@0
|
598 * @return Entry
|
Chris@0
|
599 */
|
Chris@0
|
600 public function setEnclosure(array $enclosure)
|
Chris@0
|
601 {
|
Chris@0
|
602 if (!isset($enclosure['uri'])) {
|
Chris@0
|
603 throw new Exception\InvalidArgumentException('Enclosure "uri" is not set');
|
Chris@0
|
604 }
|
Chris@0
|
605 if (!Uri::factory($enclosure['uri'])->isValid()) {
|
Chris@0
|
606 throw new Exception\InvalidArgumentException('Enclosure "uri" is not a valid URI/IRI');
|
Chris@0
|
607 }
|
Chris@0
|
608 $this->data['enclosure'] = $enclosure;
|
Chris@0
|
609
|
Chris@0
|
610 return $this;
|
Chris@0
|
611 }
|
Chris@0
|
612
|
Chris@0
|
613 /**
|
Chris@0
|
614 * Retrieve an array of all enclosures to be added to entry.
|
Chris@0
|
615 *
|
Chris@0
|
616 * @return array
|
Chris@0
|
617 */
|
Chris@0
|
618 public function getEnclosure()
|
Chris@0
|
619 {
|
Chris@0
|
620 if (!array_key_exists('enclosure', $this->data)) {
|
Chris@0
|
621 return;
|
Chris@0
|
622 }
|
Chris@0
|
623 return $this->data['enclosure'];
|
Chris@0
|
624 }
|
Chris@0
|
625
|
Chris@0
|
626 /**
|
Chris@0
|
627 * Unset a specific data point
|
Chris@0
|
628 *
|
Chris@0
|
629 * @param string $name
|
Chris@0
|
630 * @return Entry
|
Chris@0
|
631 */
|
Chris@0
|
632 public function remove($name)
|
Chris@0
|
633 {
|
Chris@0
|
634 if (isset($this->data[$name])) {
|
Chris@0
|
635 unset($this->data[$name]);
|
Chris@0
|
636 }
|
Chris@0
|
637
|
Chris@0
|
638 return $this;
|
Chris@0
|
639 }
|
Chris@0
|
640
|
Chris@0
|
641 /**
|
Chris@0
|
642 * Get registered extensions
|
Chris@0
|
643 *
|
Chris@0
|
644 * @return array
|
Chris@0
|
645 */
|
Chris@0
|
646 public function getExtensions()
|
Chris@0
|
647 {
|
Chris@0
|
648 return $this->extensions;
|
Chris@0
|
649 }
|
Chris@0
|
650
|
Chris@0
|
651 /**
|
Chris@0
|
652 * Return an Extension object with the matching name (postfixed with _Entry)
|
Chris@0
|
653 *
|
Chris@0
|
654 * @param string $name
|
Chris@0
|
655 * @return object
|
Chris@0
|
656 */
|
Chris@0
|
657 public function getExtension($name)
|
Chris@0
|
658 {
|
Chris@0
|
659 if (array_key_exists($name . '\\Entry', $this->extensions)) {
|
Chris@0
|
660 return $this->extensions[$name . '\\Entry'];
|
Chris@0
|
661 }
|
Chris@0
|
662 return;
|
Chris@0
|
663 }
|
Chris@0
|
664
|
Chris@0
|
665 /**
|
Chris@0
|
666 * Set the current feed type being exported to "rss" or "atom". This allows
|
Chris@0
|
667 * other objects to gracefully choose whether to execute or not, depending
|
Chris@0
|
668 * on their appropriateness for the current type, e.g. renderers.
|
Chris@0
|
669 *
|
Chris@0
|
670 * @param string $type
|
Chris@0
|
671 * @return Entry
|
Chris@0
|
672 */
|
Chris@0
|
673 public function setType($type)
|
Chris@0
|
674 {
|
Chris@0
|
675 $this->type = $type;
|
Chris@0
|
676 return $this;
|
Chris@0
|
677 }
|
Chris@0
|
678
|
Chris@0
|
679 /**
|
Chris@0
|
680 * Retrieve the current or last feed type exported.
|
Chris@0
|
681 *
|
Chris@0
|
682 * @return string Value will be "rss" or "atom"
|
Chris@0
|
683 */
|
Chris@0
|
684 public function getType()
|
Chris@0
|
685 {
|
Chris@0
|
686 return $this->type;
|
Chris@0
|
687 }
|
Chris@0
|
688
|
Chris@0
|
689 /**
|
Chris@0
|
690 * Method overloading: call given method on first extension implementing it
|
Chris@0
|
691 *
|
Chris@0
|
692 * @param string $method
|
Chris@0
|
693 * @param array $args
|
Chris@0
|
694 * @return mixed
|
Chris@0
|
695 * @throws Exception\BadMethodCallException if no extensions implements the method
|
Chris@0
|
696 */
|
Chris@0
|
697 public function __call($method, $args)
|
Chris@0
|
698 {
|
Chris@0
|
699 foreach ($this->extensions as $extension) {
|
Chris@0
|
700 try {
|
Chris@0
|
701 return call_user_func_array([$extension, $method], $args);
|
Chris@0
|
702 } catch (\BadMethodCallException $e) {
|
Chris@0
|
703 }
|
Chris@0
|
704 }
|
Chris@0
|
705 throw new Exception\BadMethodCallException('Method: ' . $method
|
Chris@0
|
706 . ' does not exist and could not be located on a registered Extension');
|
Chris@0
|
707 }
|
Chris@0
|
708
|
Chris@0
|
709 /**
|
Chris@0
|
710 * Creates a new Zend\Feed\Writer\Source data container for use. This is NOT
|
Chris@0
|
711 * added to the current feed automatically, but is necessary to create a
|
Chris@0
|
712 * container with some initial values preset based on the current feed data.
|
Chris@0
|
713 *
|
Chris@0
|
714 * @return Source
|
Chris@0
|
715 */
|
Chris@0
|
716 public function createSource()
|
Chris@0
|
717 {
|
Chris@0
|
718 $source = new Source;
|
Chris@0
|
719 if ($this->getEncoding()) {
|
Chris@0
|
720 $source->setEncoding($this->getEncoding());
|
Chris@0
|
721 }
|
Chris@0
|
722 $source->setType($this->getType());
|
Chris@0
|
723 return $source;
|
Chris@0
|
724 }
|
Chris@0
|
725
|
Chris@0
|
726 /**
|
Chris@0
|
727 * Appends a Zend\Feed\Writer\Entry object representing a new entry/item
|
Chris@0
|
728 * the feed data container's internal group of entries.
|
Chris@0
|
729 *
|
Chris@0
|
730 * @param Source $source
|
Chris@0
|
731 * @return Entry
|
Chris@0
|
732 */
|
Chris@0
|
733 public function setSource(Source $source)
|
Chris@0
|
734 {
|
Chris@0
|
735 $this->data['source'] = $source;
|
Chris@0
|
736 return $this;
|
Chris@0
|
737 }
|
Chris@0
|
738
|
Chris@0
|
739 /**
|
Chris@0
|
740 * @return Source
|
Chris@0
|
741 */
|
Chris@0
|
742 public function getSource()
|
Chris@0
|
743 {
|
Chris@0
|
744 if (isset($this->data['source'])) {
|
Chris@0
|
745 return $this->data['source'];
|
Chris@0
|
746 }
|
Chris@0
|
747 return;
|
Chris@0
|
748 }
|
Chris@0
|
749
|
Chris@0
|
750 /**
|
Chris@0
|
751 * Load extensions from Zend\Feed\Writer\Writer
|
Chris@0
|
752 *
|
Chris@0
|
753 * @return void
|
Chris@0
|
754 */
|
Chris@0
|
755 protected function _loadExtensions()
|
Chris@0
|
756 {
|
Chris@0
|
757 $all = Writer::getExtensions();
|
Chris@0
|
758 $manager = Writer::getExtensionManager();
|
Chris@0
|
759 $exts = $all['entry'];
|
Chris@0
|
760 foreach ($exts as $ext) {
|
Chris@0
|
761 $this->extensions[$ext] = $manager->get($ext);
|
Chris@0
|
762 $this->extensions[$ext]->setEncoding($this->getEncoding());
|
Chris@0
|
763 }
|
Chris@0
|
764 }
|
Chris@0
|
765 }
|