Element.php 3.6 KB

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