2
0

Element.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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-2008 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-2008 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), $var, $val);
  183. $this->_element->appendChild($node);
  184. } else {
  185. $node = $this->_element->ownerDocument->createElement($var, $val);
  186. $this->_element->appendChild($node);
  187. }
  188. } elseif (count($nodes) > 1) {
  189. /**
  190. * @see Zend_Feed_Exception
  191. */
  192. require_once 'Zend/Feed/Exception.php';
  193. throw new Zend_Feed_Exception('Cannot set the value of multiple tags simultaneously.');
  194. } else {
  195. $nodes[0]->nodeValue = $val;
  196. }
  197. }
  198. /**
  199. * Map isset calls onto the underlying entry representation.
  200. *
  201. * @param string $var
  202. * @return boolean
  203. */
  204. public function __isset($var)
  205. {
  206. // Look for access of the form {ns:var}. We don't use
  207. // _children() here because we can break out of the loop
  208. // immediately once we find something.
  209. if (strpos($var, ':') !== false) {
  210. list($ns, $elt) = explode(':', $var, 2);
  211. foreach ($this->_element->childNodes as $child) {
  212. if ($child->localName == $elt && $child->prefix == $ns) {
  213. return true;
  214. }
  215. }
  216. } else {
  217. foreach ($this->_element->childNodes as $child) {
  218. if ($child->localName == $var) {
  219. return true;
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * Get the value of an element with method syntax.
  226. *
  227. * Map method calls to get the string value of the requested
  228. * element. If there are multiple elements that match, this will
  229. * return an array of those objects.
  230. *
  231. * @param string $var The element to get the string value of.
  232. * @param mixed $unused This parameter is not used.
  233. * @return mixed The node's value, null, or an array of nodes.
  234. */
  235. public function __call($var, $unused)
  236. {
  237. $nodes = $this->_children($var);
  238. if (!$nodes) {
  239. return null;
  240. } elseif (count($nodes) > 1) {
  241. return $nodes;
  242. } else {
  243. return $nodes[0]->nodeValue;
  244. }
  245. }
  246. /**
  247. * Remove all children matching $var.
  248. *
  249. * @param string $var
  250. * @return void
  251. */
  252. public function __unset($var)
  253. {
  254. $nodes = $this->_children($var);
  255. foreach ($nodes as $node) {
  256. $parent = $node->parentNode;
  257. $parent->removeChild($node);
  258. }
  259. }
  260. /**
  261. * Returns the nodeValue of this element when this object is used
  262. * in a string context.
  263. *
  264. * @return string
  265. */
  266. public function __toString()
  267. {
  268. return $this->_element->nodeValue;
  269. }
  270. /**
  271. * Finds children with tagnames matching $var
  272. *
  273. * Similar to SimpleXML's children() method.
  274. *
  275. * @param string $var Tagname to match, can be either namespace:tagName or just tagName.
  276. * @return array
  277. */
  278. protected function _children($var)
  279. {
  280. $found = array();
  281. // Look for access of the form {ns:var}.
  282. if (strpos($var, ':') !== false) {
  283. list($ns, $elt) = explode(':', $var, 2);
  284. foreach ($this->_element->childNodes as $child) {
  285. if ($child->localName == $elt && $child->prefix == $ns) {
  286. $found[] = $child;
  287. }
  288. }
  289. } else {
  290. foreach ($this->_element->childNodes as $child) {
  291. if ($child->localName == $var) {
  292. $found[] = $child;
  293. }
  294. }
  295. }
  296. return $found;
  297. }
  298. /**
  299. * Required by the ArrayAccess interface.
  300. *
  301. * @param string $offset
  302. * @return boolean
  303. */
  304. public function offsetExists($offset)
  305. {
  306. if (strpos($offset, ':') !== false) {
  307. list($ns, $attr) = explode(':', $offset, 2);
  308. return $this->_element->hasAttributeNS(Zend_Feed::lookupNamespace($ns), $attr);
  309. } else {
  310. return $this->_element->hasAttribute($offset);
  311. }
  312. }
  313. /**
  314. * Required by the ArrayAccess interface.
  315. *
  316. * @param string $offset
  317. * @return string
  318. */
  319. public function offsetGet($offset)
  320. {
  321. if (strpos($offset, ':') !== false) {
  322. list($ns, $attr) = explode(':', $offset, 2);
  323. return $this->_element->getAttributeNS(Zend_Feed::lookupNamespace($ns), $attr);
  324. } else {
  325. return $this->_element->getAttribute($offset);
  326. }
  327. }
  328. /**
  329. * Required by the ArrayAccess interface.
  330. *
  331. * @param string $offset
  332. * @param string $value
  333. * @return string
  334. */
  335. public function offsetSet($offset, $value)
  336. {
  337. $this->ensureAppended();
  338. if (strpos($offset, ':') !== false) {
  339. list($ns, $attr) = explode(':', $offset, 2);
  340. return $this->_element->setAttributeNS(Zend_Feed::lookupNamespace($ns), $attr, $value);
  341. } else {
  342. return $this->_element->setAttribute($offset, $value);
  343. }
  344. }
  345. /**
  346. * Required by the ArrayAccess interface.
  347. *
  348. * @param string $offset
  349. * @return boolean
  350. */
  351. public function offsetUnset($offset)
  352. {
  353. if (strpos($offset, ':') !== false) {
  354. list($ns, $attr) = explode(':', $offset, 2);
  355. return $this->_element->removeAttributeNS(Zend_Feed::lookupNamespace($ns), $attr);
  356. } else {
  357. return $this->_element->removeAttribute($offset);
  358. }
  359. }
  360. }