comparison vendor/zendframework/zend-feed/src/Writer/Entry.php @ 0:c75dbcec494b

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