f05803f0f5f78577cd72567bc89e5c6a93ffbac9
[yaffs-website] / vendor / zendframework / zend-feed / src / Reader / Entry / AbstractEntry.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\Entry;
11
12 use DOMDocument;
13 use DOMElement;
14 use DOMXPath;
15 use Zend\Feed\Reader;
16 use Zend\Feed\Reader\Exception;
17
18 abstract class AbstractEntry
19 {
20     /**
21      * Feed entry data
22      *
23      * @var array
24      */
25     protected $data = [];
26
27     /**
28      * DOM document object
29      *
30      * @var DOMDocument
31      */
32     protected $domDocument = null;
33
34     /**
35      * Entry instance
36      *
37      * @var DOMElement
38      */
39     protected $entry = null;
40
41     /**
42      * Pointer to the current entry
43      *
44      * @var int
45      */
46     protected $entryKey = 0;
47
48     /**
49      * XPath object
50      *
51      * @var DOMXPath
52      */
53     protected $xpath = null;
54
55     /**
56      * Registered extensions
57      *
58      * @var array
59      */
60     protected $extensions = [];
61
62     /**
63      * Constructor
64      *
65      * @param  DOMElement $entry
66      * @param  int $entryKey
67      * @param  string $type
68      */
69     public function __construct(DOMElement $entry, $entryKey, $type = null)
70     {
71         $this->entry       = $entry;
72         $this->entryKey    = $entryKey;
73         $this->domDocument = $entry->ownerDocument;
74         if ($type !== null) {
75             $this->data['type'] = $type;
76         } elseif ($this->domDocument !== null) {
77             $this->data['type'] = Reader\Reader::detectType($this->domDocument);
78         } else {
79             $this->data['type'] = Reader\Reader::TYPE_ANY;
80         }
81         $this->loadExtensions();
82     }
83
84     /**
85      * Get the DOM
86      *
87      * @return DOMDocument
88      */
89     public function getDomDocument()
90     {
91         return $this->domDocument;
92     }
93
94     /**
95      * Get the entry element
96      *
97      * @return DOMElement
98      */
99     public function getElement()
100     {
101         return $this->entry;
102     }
103
104     /**
105      * Get the Entry's encoding
106      *
107      * @return string
108      */
109     public function getEncoding()
110     {
111         $assumed = $this->getDomDocument()->encoding;
112         if (empty($assumed)) {
113             $assumed = 'UTF-8';
114         }
115         return $assumed;
116     }
117
118     /**
119      * Get entry as xml
120      *
121      * @return string
122      */
123     public function saveXml()
124     {
125         $dom   = new DOMDocument('1.0', $this->getEncoding());
126         $deep  = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true;
127         $entry = $dom->importNode($this->getElement(), $deep);
128         $dom->appendChild($entry);
129         return $dom->saveXml();
130     }
131
132     /**
133      * Get the entry type
134      *
135      * @return string
136      */
137     public function getType()
138     {
139         return $this->data['type'];
140     }
141
142     /**
143      * Get the XPath query object
144      *
145      * @return DOMXPath
146      */
147     public function getXpath()
148     {
149         if (! $this->xpath) {
150             $this->setXpath(new DOMXPath($this->getDomDocument()));
151         }
152         return $this->xpath;
153     }
154
155     /**
156      * Set the XPath query
157      *
158      * @param  DOMXPath $xpath
159      * @return AbstractEntry
160      */
161     public function setXpath(DOMXPath $xpath)
162     {
163         $this->xpath = $xpath;
164         return $this;
165     }
166
167     /**
168      * Get registered extensions
169      *
170      * @return array
171      */
172     public function getExtensions()
173     {
174         return $this->extensions;
175     }
176
177     /**
178      * Return an Extension object with the matching name (postfixed with _Entry)
179      *
180      * @param string $name
181      * @return Reader\Extension\AbstractEntry
182      */
183     public function getExtension($name)
184     {
185         if (array_key_exists($name . '\\Entry', $this->extensions)) {
186             return $this->extensions[$name . '\\Entry'];
187         }
188         return;
189     }
190
191     /**
192      * Method overloading: call given method on first extension implementing it
193      *
194      * @param  string $method
195      * @param  array $args
196      * @return mixed
197      * @throws Exception\RuntimeException if no extensions implements the method
198      */
199     public function __call($method, $args)
200     {
201         foreach ($this->extensions as $extension) {
202             if (method_exists($extension, $method)) {
203                 return call_user_func_array([$extension, $method], $args);
204             }
205         }
206         throw new Exception\RuntimeException(sprintf(
207             'Method: %s does not exist and could not be located on a registered Extension',
208             $method
209         ));
210     }
211
212     /**
213      * Load extensions from Zend\Feed\Reader\Reader
214      *
215      * @return void
216      */
217     protected function loadExtensions()
218     {
219         $all     = Reader\Reader::getExtensions();
220         $manager = Reader\Reader::getExtensionManager();
221         $feed    = $all['entry'];
222         foreach ($feed as $extension) {
223             if (in_array($extension, $all['core'])) {
224                 continue;
225             }
226             $plugin = $manager->get($extension);
227             $plugin->setEntryElement($this->getElement());
228             $plugin->setEntryKey($this->entryKey);
229             $plugin->setType($this->data['type']);
230             $this->extensions[$extension] = $plugin;
231         }
232     }
233 }