4da435b355ccfd75047a370152782b1721e406f0
[yaffs-website] / vendor / zendframework / zend-feed / src / Reader / Extension / DublinCore / Entry.php
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\Reader\Extension\DublinCore;
11
12 use DateTime;
13 use Zend\Feed\Reader;
14 use Zend\Feed\Reader\Collection;
15 use Zend\Feed\Reader\Extension;
16
17 class Entry extends Extension\AbstractEntry
18 {
19     /**
20      * Get an author entry
21      *
22      * @param int $index
23      * @return string
24      */
25     public function getAuthor($index = 0)
26     {
27         $authors = $this->getAuthors();
28
29         if (isset($authors[$index])) {
30             return $authors[$index];
31         }
32
33         return;
34     }
35
36     /**
37      * Get an array with feed authors
38      *
39      * @return array
40      */
41     public function getAuthors()
42     {
43         if (array_key_exists('authors', $this->data)) {
44             return $this->data['authors'];
45         }
46
47         $authors = [];
48         $list = $this->getXpath()->evaluate($this->getXpathPrefix() . '//dc11:creator');
49
50         if (! $list->length) {
51             $list = $this->getXpath()->evaluate($this->getXpathPrefix() . '//dc10:creator');
52         }
53         if (! $list->length) {
54             $list = $this->getXpath()->evaluate($this->getXpathPrefix() . '//dc11:publisher');
55
56             if (! $list->length) {
57                 $list = $this->getXpath()->evaluate($this->getXpathPrefix() . '//dc10:publisher');
58             }
59         }
60
61         if ($list->length) {
62             foreach ($list as $author) {
63                 $authors[] = [
64                     'name' => $author->nodeValue
65                 ];
66             }
67             $authors = new Collection\Author(
68                 Reader\Reader::arrayUnique($authors)
69             );
70         } else {
71             $authors = null;
72         }
73
74         $this->data['authors'] = $authors;
75
76         return $this->data['authors'];
77     }
78
79     /**
80      * Get categories (subjects under DC)
81      *
82      * @return Collection\Category
83      */
84     public function getCategories()
85     {
86         if (array_key_exists('categories', $this->data)) {
87             return $this->data['categories'];
88         }
89
90         $list = $this->getXpath()->evaluate($this->getXpathPrefix() . '//dc11:subject');
91
92         if (! $list->length) {
93             $list = $this->getXpath()->evaluate($this->getXpathPrefix() . '//dc10:subject');
94         }
95
96         if ($list->length) {
97             $categoryCollection = new Collection\Category;
98             foreach ($list as $category) {
99                 $categoryCollection[] = [
100                     'term' => $category->nodeValue,
101                     'scheme' => null,
102                     'label' => $category->nodeValue,
103                 ];
104             }
105         } else {
106             $categoryCollection = new Collection\Category;
107         }
108
109         $this->data['categories'] = $categoryCollection;
110         return $this->data['categories'];
111     }
112
113     /**
114      * Get the entry content
115      *
116      * @return string
117      */
118     public function getContent()
119     {
120         return $this->getDescription();
121     }
122
123     /**
124      * Get the entry description
125      *
126      * @return string
127      */
128     public function getDescription()
129     {
130         if (array_key_exists('description', $this->data)) {
131             return $this->data['description'];
132         }
133
134         $description = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/dc11:description)');
135
136         if (! $description) {
137             $description = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/dc10:description)');
138         }
139
140         if (! $description) {
141             $description = null;
142         }
143
144         $this->data['description'] = $description;
145
146         return $this->data['description'];
147     }
148
149     /**
150      * Get the entry ID
151      *
152      * @return string
153      */
154     public function getId()
155     {
156         if (array_key_exists('id', $this->data)) {
157             return $this->data['id'];
158         }
159
160         $id = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/dc11:identifier)');
161
162         if (! $id) {
163             $id = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/dc10:identifier)');
164         }
165
166         $this->data['id'] = $id;
167
168         return $this->data['id'];
169     }
170
171     /**
172      * Get the entry title
173      *
174      * @return string
175      */
176     public function getTitle()
177     {
178         if (array_key_exists('title', $this->data)) {
179             return $this->data['title'];
180         }
181
182         $title = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/dc11:title)');
183
184         if (! $title) {
185             $title = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/dc10:title)');
186         }
187
188         if (! $title) {
189             $title = null;
190         }
191
192         $this->data['title'] = $title;
193
194         return $this->data['title'];
195     }
196
197     /**
198      *
199      *
200      * @return DateTime|null
201      */
202     public function getDate()
203     {
204         if (array_key_exists('date', $this->data)) {
205             return $this->data['date'];
206         }
207
208         $d    = null;
209         $date = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/dc11:date)');
210
211         if (! $date) {
212             $date = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/dc10:date)');
213         }
214
215         if ($date) {
216             $d = new DateTime($date);
217         }
218
219         $this->data['date'] = $d;
220
221         return $this->data['date'];
222     }
223
224     /**
225      * Register DC namespaces
226      *
227      * @return void
228      */
229     protected function registerNamespaces()
230     {
231         $this->getXpath()->registerNamespace('dc10', 'http://purl.org/dc/elements/1.0/');
232         $this->getXpath()->registerNamespace('dc11', 'http://purl.org/dc/elements/1.1/');
233     }
234 }