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