simplexml.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /*
  3. * Copyright 2010-2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. /*%******************************************************************************************%*/
  17. // CLASS
  18. /**
  19. * Wraps the underlying `SimpleXMLIterator` class with enhancements for rapidly traversing the DOM tree,
  20. * converting types, and comparisons.
  21. *
  22. * @version 2011.04.25
  23. * @license See the included NOTICE.md file for more information.
  24. * @copyright See the included NOTICE.md file for more information.
  25. * @link http://aws.amazon.com/php/ PHP Developer Center
  26. * @link http://php.net/SimpleXML SimpleXML
  27. */
  28. class CFSimpleXML extends SimpleXMLIterator
  29. {
  30. /**
  31. * Stores the namespace name to use in XPath queries.
  32. */
  33. public $xml_ns;
  34. /**
  35. * Stores the namespace URI to use in XPath queries.
  36. */
  37. public $xml_ns_url;
  38. /**
  39. * Catches requests made to methods that don't exist. Specifically, looks for child nodes via XPath.
  40. *
  41. * @param string $name (Required) The name of the method.
  42. * @param array $arguments (Required) The arguments passed to the method.
  43. * @return mixed Either an array of matches, or a single <CFSimpleXML> element.
  44. */
  45. public function __call($name, $arguments)
  46. {
  47. // Remap $this
  48. $self = $this;
  49. // Re-base the XML
  50. $self = new CFSimpleXML($self->asXML());
  51. // Determine XPath query
  52. $self->xpath_expression = 'descendant-or-self::' . $name;
  53. // Get the results and augment with CFArray
  54. $results = $self->xpath($self->xpath_expression);
  55. if (!count($results)) return false;
  56. $results = new CFArray($results);
  57. // If an integer was passed, return only that result
  58. if (isset($arguments[0]) && is_int($arguments[0]))
  59. {
  60. if (isset($results[$arguments[0]]))
  61. {
  62. return $results[$arguments[0]];
  63. }
  64. return false;
  65. }
  66. return $results;
  67. }
  68. /**
  69. * Alternate approach to constructing a new instance. Supports chaining.
  70. *
  71. * @param string $data (Required) A well-formed XML string or the path or URL to an XML document if $data_is_url is <code>true</code>.
  72. * @param integer $options (Optional) Used to specify additional LibXML parameters. The default value is <code>0</code>.
  73. * @param boolean $data_is_url (Optional) Specify a value of <code>true</code> to specify that data is a path or URL to an XML document instead of string data. The default value is <code>false</code>.
  74. * @param string $ns (Optional) The XML namespace to return values for.
  75. * @param boolean $is_prefix (Optional) (No description provided by PHP.net.)
  76. * @return CFSimpleXML Creates a new <CFSimpleXML> element.
  77. */
  78. public static function init($data, $options = 0, $data_is_url, $ns, $is_prefix = false)
  79. {
  80. if (version_compare(PHP_VERSION, '5.3.0', '<'))
  81. {
  82. throw new Exception('PHP 5.3 or newer is required to instantiate a new class with CLASS::init().');
  83. }
  84. $self = get_called_class();
  85. return new $self($data, $options, $data_is_url, $ns, $is_prefix);
  86. }
  87. /*%******************************************************************************************%*/
  88. // TRAVERSAL
  89. /**
  90. * Wraps the results of an XPath query in a <CFArray> object.
  91. *
  92. * @param string $expr (Required) The XPath expression to use to query the XML response.
  93. * @return CFArray A <CFArray> object containing the results of the XPath query.
  94. */
  95. public function query($expr)
  96. {
  97. return new CFArray($this->xpath($expr));
  98. }
  99. /**
  100. * Gets the parent or a preferred ancestor of the current element.
  101. *
  102. * @param string $node (Optional) Name of the ancestor element to match and return.
  103. * @return CFSimpleXML A <CFSimpleXML> object containing the requested node.
  104. */
  105. public function parent($node = null)
  106. {
  107. if ($node)
  108. {
  109. $parents = $this->xpath('ancestor-or-self::' . $node);
  110. }
  111. else
  112. {
  113. $parents = $this->xpath('parent::*');
  114. }
  115. return $parents[0];
  116. }
  117. /*%******************************************************************************************%*/
  118. // ALTERNATE FORMATS
  119. /**
  120. * Gets the current XML node as a true string.
  121. *
  122. * @return string The current XML node as a true string.
  123. */
  124. public function to_string()
  125. {
  126. return (string) $this;
  127. }
  128. /**
  129. * Gets the current XML node as <CFArray>, a child class of PHP's <php:ArrayObject> class.
  130. *
  131. * @return CFArray The current XML node as a <CFArray> object.
  132. */
  133. public function to_array()
  134. {
  135. return new CFArray(json_decode(json_encode($this), true));
  136. }
  137. /**
  138. * Gets the current XML node as a stdClass object.
  139. *
  140. * @return array The current XML node as a stdClass object.
  141. */
  142. public function to_stdClass()
  143. {
  144. return json_decode(json_encode($this));
  145. }
  146. /**
  147. * Gets the current XML node as a JSON string.
  148. *
  149. * @return string The current XML node as a JSON string.
  150. */
  151. public function to_json()
  152. {
  153. return json_encode($this);
  154. }
  155. /**
  156. * Gets the current XML node as a YAML string.
  157. *
  158. * @return string The current XML node as a YAML string.
  159. */
  160. public function to_yaml()
  161. {
  162. return sfYaml::dump(json_decode(json_encode($this), true), 5);
  163. }
  164. /*%******************************************************************************************%*/
  165. // COMPARISONS
  166. /**
  167. * Whether or not the current node exactly matches the compared value.
  168. *
  169. * @param string $value (Required) The value to compare the current node to.
  170. * @return boolean Whether or not the current node exactly matches the compared value.
  171. */
  172. public function is($value)
  173. {
  174. return ((string) $this === $value);
  175. }
  176. /**
  177. * Whether or not the current node contains the compared value.
  178. *
  179. * @param string $value (Required) The value to use to determine whether it is contained within the node.
  180. * @return boolean Whether or not the current node contains the compared value.
  181. */
  182. public function contains($value)
  183. {
  184. return (stripos((string) $this, $value) !== false);
  185. }
  186. }