annotate vendor/zendframework/zend-feed/src/Reader/AbstractEntry.php @ 3:e11175134f4e

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