Element.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Wraps a DOMElement allowing for SimpleXML-like access to attributes.
  23. *
  24. * @category Zend
  25. * @package Zend_Feed
  26. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Feed_Element implements ArrayAccess
  30. {
  31. /**
  32. * @var DOMElement
  33. */
  34. protected $_element;
  35. /**
  36. * @var Zend_Feed_Element
  37. */
  38. protected $_parentElement;
  39. /**
  40. * @var boolean
  41. */
  42. protected $_appended = true;
  43. /**
  44. * Zend_Feed_Element constructor.
  45. *
  46. * @param DOMElement $element The DOM element we're encapsulating.
  47. * @return void
  48. */
  49. public function __construct($element = null)
  50. {
  51. $this->_element = $element;
  52. }
  53. /**
  54. * Get a DOM representation of the element
  55. *
  56. * Returns the underlying DOM object, which can then be
  57. * manipulated with full DOM methods.
  58. *
  59. * @return DOMDocument
  60. */
  61. public function getDOM()
  62. {
  63. return $this->_element;
  64. }
  65. /**
  66. * Update the object from a DOM element
  67. *
  68. * Take a DOMElement object, which may be originally from a call
  69. * to getDOM() or may be custom created, and use it as the
  70. * DOM tree for this Zend_Feed_Element.
  71. *
  72. * @param DOMElement $element
  73. * @return void
  74. */
  75. public function setDOM(DOMElement $element)
  76. {
  77. $this->_element = $this->_element->ownerDocument->importNode($element, true);
  78. }
  79. /**
  80. * Set the parent element of this object to another
  81. * Zend_Feed_Element.
  82. *
  83. * @param Zend_Feed_Element $element
  84. * @return void
  85. */
  86. public function setParent(Zend_Feed_Element $element)
  87. {
  88. $this->_parentElement = $element;
  89. $this->_appended = false;
  90. }
  91. /**
  92. * Appends this element to its parent if necessary.
  93. *
  94. * @return void
  95. */
  96. protected function ensureAppended()
  97. {
  98. if (!$this->_appended) {
  99. $this->_parentElement->getDOM()->appendChild($this->_element);
  100. $this->_appended = true;
  101. $this->_parentElement->ensureAppended();
  102. }
  103. }
  104. /**
  105. * Get an XML string representation of this element
  106. *
  107. * Returns a string of this element's XML, including the XML
  108. * prologue.
  109. *
  110. * @return string
  111. */
  112. public function saveXml()
  113. {
  114. // Return a complete document including XML prologue.
  115. $doc = new DOMDocument($this->_element->ownerDocument->version,
  116. $this->_element->ownerDocument->actualEncoding);
  117. $doc->appendChild($doc->importNode($this->_element, true));
  118. return $doc->saveXML();
  119. }
  120. /**
  121. * Get the XML for only this element
  122. *
  123. * Returns a string of this element's XML without prologue.
  124. *
  125. * @return string
  126. */
  127. public function saveXmlFragment()
  128. {
  129. return $this->_element->ownerDocument->saveXML($this->_element);
  130. }
  131. /**
  132. * Map variable access onto the underlying entry representation.
  133. *
  134. * Get-style access returns a Zend_Feed_Element representing the
  135. * child element accessed. To get string values, use method syntax
  136. * with the __call() overriding.
  137. *
  138. * @param string $var The property to access.
  139. * @return mixed
  140. */
  141. public function __get($var)
  142. {
  143. $nodes = $this->_children($var);
  144. $length = count($nodes);
  145. if ($length == 1) {
  146. return new Zend_Feed_Element($nodes[0]);
  147. } elseif ($length > 1) {
  148. return array_map(create_function('$e', 'return new Zend_Feed_Element($e);'), $nodes);
  149. } else {
  150. // When creating anonymous nodes for __set chaining, don't
  151. // call appendChild() on them. Instead we pass the current
  152. // element to them as an extra reference; the child is
  153. // then responsible for appending itself when it is
  154. // actually set. This way "if ($foo->bar)" doesn't create
  155. // a phantom "bar" element in our tree.
  156. if (strpos($var, ':') !== false) {
  157. list($ns, $elt) = explode(':', $var, 2);
  158. $node = $this->_element->ownerDocument->createElementNS(Zend_Feed::lookupNamespace($ns), $elt);
  159. } else {
  160. $node = $this->_element->ownerDocument->createElement($var);
  161. }
  162. $node = new self($node);
  163. $node->setParent($this);
  164. return $node;
  165. }
  166. }
  167. /**
  168. * Map variable sets onto the underlying entry representation.
  169. *
  170. * @param string $var The property to change.
  171. * @param string $val The property's new value.
  172. * @return void
  173. * @throws Zend_Feed_Exception
  174. */
  175. public function __set($var, $val)
  176. {
  177. $this->ensureAppended();
  178. $nodes = $this->_children($var);
  179. if (!$nodes) {
  180. if (strpos($var, ':') !== false) {
  181. list($ns, $elt) = explode(':', $var, 2);
  182. $node = $this->_element->ownerDocument->createElementNS(Zend_Feed::lookupNamespace($ns),
  183. $var, htmlspecialchars($val, ENT_NOQUOTES, 'UTF-8'));
  184. $this->_element->appendChild($node);
  185. } else {
  186. $node = $this->_element->ownerDocument->createElement($var,
  187. htmlspecialchars($val, ENT_NOQUOTES, 'UTF-8'));
  188. $this->_element->appendChild($node);
  189. }
  190. } elseif (count($nodes) > 1) {
  191. /**
  192. * @see Zend_Feed_Exception
  193. */
  194. require_once 'Zend/Feed/Exception.php';
  195. throw new Zend_Feed_Exception('Cannot set the value of multiple tags simultaneously.');
  196. } else {
  197. $nodes[0]->nodeValue = $val;
  198. }
  199. }
  200. /**
  201. * Map isset calls onto the underlying entry representation.
  202. *
  203. * @param string $var
  204. * @return boolean
  205. */
  206. public function __isset($var)
  207. {
  208. // Look for access of the form {ns:var}. We don't use
  209. // _children() here because we can break out of the loop
  210. // immediately once we find something.
  211. if (strpos($var, ':') !== false) {
  212. list($ns, $elt) = explode(':', $var, 2);
  213. foreach ($this->_element->childNodes as $child) {
  214. if ($child->localName == $elt && $child->prefix == $ns) {
  215. return true;
  216. }
  217. }
  218. } else {
  219. foreach ($this->_element->childNodes as $child) {
  220. if ($child->localName == $var) {
  221. return true;
  222. }
  223. }
  224. }
  225. }
  226. /**
  227. * Get the value of an element with method syntax.
  228. *
  229. * Map method calls to get the string value of the requested
  230. * element. If there are multiple elements that match, this will
  231. * return an array of those objects.
  232. *
  233. * @param string $var The element to get the string value of.
  234. * @param mixed $unused This parameter is not used.
  235. * @return mixed The node's value, null, or an array of nodes.
  236. */
  237. public function __call($var, $unused)
  238. {
  239. $nodes = $this->_children($var);
  240. if (!$nodes) {
  241. return null;
  242. } elseif (count($nodes) > 1) {
  243. return $nodes;
  244. } else {
  245. return $nodes[0]->nodeValue;
  246. }
  247. }
  248. /**
  249. * Remove all children matching $var.
  250. *
  251. * @param string $var
  252. * @return void
  253. */
  254. public function __unset($var)
  255. {
  256. $nodes = $this->_children($var);
  257. foreach ($nodes as $node) {
  258. $parent = $node->parentNode;
  259. $parent->removeChild($node);
  260. }
  261. }
  262. /**
  263. * Returns the nodeValue of this element when this object is used
  264. * in a string context.
  265. *
  266. * @return string
  267. */
  268. public function __toString()
  269. {
  270. return $this->_element->nodeValue;
  271. }
  272. /**
  273. * Finds children with tagnames matching $var
  274. *
  275. * Similar to SimpleXML's children() method.
  276. *
  277. * @param string $var Tagname to match, can be either namespace:tagName or just tagName.
  278. * @return array
  279. */
  280. protected function _children($var)
  281. {
  282. $found = array();
  283. // Look for access of the form {ns:var}.
  284. if (strpos($var, ':') !== false) {
  285. list($ns, $elt) = explode(':', $var, 2);
  286. foreach ($this->_element->childNodes as $child) {
  287. if ($child->localName == $elt && $child->prefix == $ns) {
  288. $found[] = $child;
  289. }
  290. }
  291. } else {
  292. foreach ($this->_element->childNodes as $child) {
  293. if ($child->localName == $var) {
  294. $found[] = $child;
  295. }
  296. }
  297. }
  298. return $found;
  299. }
  300. /**
  301. * Required by the ArrayAccess interface.
  302. *
  303. * @param string $offset
  304. * @return boolean
  305. */
  306. public function offsetExists($offset)
  307. {
  308. if (strpos($offset, ':') !== false) {
  309. list($ns, $attr) = explode(':', $offset, 2);
  310. return $this->_element->hasAttributeNS(Zend_Feed::lookupNamespace($ns), $attr);
  311. } else {
  312. return $this->_element->hasAttribute($offset);
  313. }
  314. }
  315. /**
  316. * Required by the ArrayAccess interface.
  317. *
  318. * @param string $offset
  319. * @return string
  320. */
  321. public function offsetGet($offset)
  322. {
  323. if (strpos($offset, ':') !== false) {
  324. list($ns, $attr) = explode(':', $offset, 2);
  325. return $this->_element->getAttributeNS(Zend_Feed::lookupNamespace($ns), $attr);
  326. } else {
  327. return $this->_element->getAttribute($offset);
  328. }
  329. }
  330. /**
  331. * Required by the ArrayAccess interface.
  332. *
  333. * @param string $offset
  334. * @param string $value
  335. * @return string
  336. */
  337. public function offsetSet($offset, $value)
  338. {
  339. $this->ensureAppended();
  340. if (strpos($offset, ':') !== false) {
  341. list($ns, $attr) = explode(':', $offset, 2);
  342. // DOMElement::setAttributeNS() requires $qualifiedName to have a prefix
  343. return $this->_element->setAttributeNS(Zend_Feed::lookupNamespace($ns), $offset, $value);
  344. } else {
  345. return $this->_element->setAttribute($offset, $value);
  346. }
  347. }
  348. /**
  349. * Required by the ArrayAccess interface.
  350. *
  351. * @param string $offset
  352. * @return boolean
  353. */
  354. public function offsetUnset($offset)
  355. {
  356. if (strpos($offset, ':') !== false) {
  357. list($ns, $attr) = explode(':', $offset, 2);
  358. return $this->_element->removeAttributeNS(Zend_Feed::lookupNamespace($ns), $attr);
  359. } else {
  360. return $this->_element->removeAttribute($offset);
  361. }
  362. }
  363. }