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