annotate vendor/zendframework/zend-feed/src/Reader/AbstractEntry.php @ 19:fa3358dc1485 tip

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