Element.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_InfoCard
  17. * @subpackage Zend_InfoCard_Xml
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_InfoCard_Xml_Element_Interface
  24. */
  25. require_once 'Zend/InfoCard/Xml/Element/Interface.php';
  26. /**
  27. * An abstract class representing a an XML data block
  28. *
  29. * @category Zend
  30. * @package Zend_InfoCard
  31. * @subpackage Zend_InfoCard_Xml
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. abstract class Zend_InfoCard_Xml_Element
  36. extends SimpleXMLElement
  37. implements Zend_InfoCard_Xml_Element_Interface
  38. {
  39. /**
  40. * Convert the object to a string by displaying its XML content
  41. *
  42. * @return string an XML representation of the object
  43. */
  44. public function __toString()
  45. {
  46. return $this->asXML();
  47. }
  48. /**
  49. * Converts an XML Element object into a DOM object
  50. *
  51. * @throws Zend_InfoCard_Xml_Exception
  52. * @param Zend_InfoCard_Xml_Element $e The object to convert
  53. * @return DOMElement A DOMElement representation of the same object
  54. */
  55. static public function convertToDOM(Zend_InfoCard_Xml_Element $e)
  56. {
  57. $dom = dom_import_simplexml($e);
  58. if(!($dom instanceof DOMElement)) {
  59. // Zend_InfoCard_Xml_Element exntes SimpleXMLElement, so this should *never* fail
  60. // @codeCoverageIgnoreStart
  61. require_once 'Zend/InfoCard/Xml/Exception.php';
  62. throw new Zend_InfoCard_Xml_Exception("Failed to convert between SimpleXML and DOM");
  63. // @codeCoverageIgnoreEnd
  64. }
  65. return $dom;
  66. }
  67. /**
  68. * Converts a DOMElement object into the specific class
  69. *
  70. * @throws Zend_InfoCard_Xml_Exception
  71. * @param DOMElement $e The DOMElement object to convert
  72. * @param string $classname The name of the class to convert it to (must inhert from Zend_InfoCard_Xml_Element)
  73. * @return Zend_InfoCard_Xml_Element a Xml Element object from the DOM element
  74. */
  75. static public function convertToObject(DOMElement $e, $classname)
  76. {
  77. if (!class_exists($classname)) {
  78. require_once 'Zend/Loader.php';
  79. Zend_Loader::loadClass($classname);
  80. }
  81. $reflection = new ReflectionClass($classname);
  82. if(!$reflection->isSubclassOf('Zend_InfoCard_Xml_Element')) {
  83. require_once 'Zend/InfoCard/Xml/Exception.php';
  84. throw new Zend_InfoCard_Xml_Exception("DOM element must be converted to an instance of Zend_InfoCard_Xml_Element");
  85. }
  86. $sxe = simplexml_import_dom($e, $classname);
  87. if(!($sxe instanceof Zend_InfoCard_Xml_Element)) {
  88. // Since we just checked to see if this was a subclass of Zend_infoCard_Xml_Element this shoudl never fail
  89. // @codeCoverageIgnoreStart
  90. require_once 'Zend/InfoCard/Xml/Exception.php';
  91. throw new Zend_InfoCard_Xml_Exception("Failed to convert between DOM and SimpleXML");
  92. // @codeCoverageIgnoreEnd
  93. }
  94. return $sxe;
  95. }
  96. }