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

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