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