CFSimpleXML

Wraps the underlying `SimpleXMLIterator` class with enhancements for rapidly traversing the DOM tree, converting types, and comparisons.

Defined (1)

The class is defined in the following location(s).

/lib/SNS/utilities/simplexml.class.php  
  1. class CFSimpleXML extends SimpleXMLIterator 
  2. /** 
  3. * Stores the namespace name to use in XPath queries. 
  4. */ 
  5. public $xml_ns; 
  6.  
  7. /** 
  8. * Stores the namespace URI to use in XPath queries. 
  9. */ 
  10. public $xml_ns_url; 
  11.  
  12. /** 
  13. * Catches requests made to methods that don't exist. Specifically, looks for child nodes via XPath. 
  14. * @param string $name (Required) The name of the method. 
  15. * @param array $arguments (Required) The arguments passed to the method. 
  16. * @return mixed Either an array of matches, or a single <CFSimpleXML> element. 
  17. */ 
  18. public function __call($name, $arguments) 
  19. // Remap $this 
  20. $self = $this; 
  21.  
  22. // Re-base the XML 
  23. $self = new CFSimpleXML($self->asXML()); 
  24.  
  25. // Determine XPath query 
  26. $self->xpath_expression = 'descendant-or-self::' . $name; 
  27.  
  28. // Get the results and augment with CFArray 
  29. $results = $self->xpath($self->xpath_expression); 
  30. if (!count($results)) return false; 
  31. $results = new CFArray($results); 
  32.  
  33. // If an integer was passed, return only that result 
  34. if (isset($arguments[0]) && is_int($arguments[0])) 
  35. if (isset($results[$arguments[0]])) 
  36. return $results[$arguments[0]]; 
  37.  
  38. return false; 
  39.  
  40. return $results; 
  41.  
  42. /** 
  43. * Alternate approach to constructing a new instance. Supports chaining. 
  44. * @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>. 
  45. * @param integer $options (Optional) Used to specify additional LibXML parameters. The default value is <code>0</code>. 
  46. * @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>. 
  47. * @param string $ns (Optional) The XML namespace to return values for. 
  48. * @param boolean $is_prefix (Optional) (No description provided by PHP.net.) 
  49. * @return CFSimpleXML Creates a new <CFSimpleXML> element. 
  50. */ 
  51. public static function init($data, $options = 0, $data_is_url, $ns, $is_prefix = false) 
  52. if (version_compare(PHP_VERSION, '5.3.0', '<')) 
  53. throw new Exception('PHP 5.3 or newer is required to instantiate a new class with CLASS::init().'); 
  54.  
  55. $self = get_called_class(); 
  56. return new $self($data, $options, $data_is_url, $ns, $is_prefix); 
  57.  
  58.  
  59. /**%******************************************************************************************%*/ 
  60. // TRAVERSAL 
  61.  
  62. /** 
  63. * Wraps the results of an XPath query in a <CFArray> object. 
  64. * @param string $expr (Required) The XPath expression to use to query the XML response. 
  65. * @return CFArray A <CFArray> object containing the results of the XPath query. 
  66. */ 
  67. public function query($expr) 
  68. return new CFArray($this->xpath($expr)); 
  69.  
  70. /** 
  71. * Gets the parent or a preferred ancestor of the current element. 
  72. * @param string $node (Optional) Name of the ancestor element to match and return. 
  73. * @return CFSimpleXML A <CFSimpleXML> object containing the requested node. 
  74. */ 
  75. public function parent($node = null) 
  76. if ($node) 
  77. $parents = $this->xpath('ancestor-or-self::' . $node); 
  78. else 
  79. $parents = $this->xpath('parent::*'); 
  80.  
  81. return $parents[0]; 
  82.  
  83.  
  84. /**%******************************************************************************************%*/ 
  85. // ALTERNATE FORMATS 
  86.  
  87. /** 
  88. * Gets the current XML node as a true string. 
  89. * @return string The current XML node as a true string. 
  90. */ 
  91. public function to_string() 
  92. return (string) $this; 
  93.  
  94. /** 
  95. * Gets the current XML node as <CFArray>, a child class of PHP's <php:ArrayObject> class. 
  96. * @return CFArray The current XML node as a <CFArray> object. 
  97. */ 
  98. public function to_array() 
  99. return new CFArray(json_decode(json_encode($this), true)); 
  100.  
  101. /** 
  102. * Gets the current XML node as a stdClass object. 
  103. * @return array The current XML node as a stdClass object. 
  104. */ 
  105. public function to_stdClass() 
  106. return json_decode(json_encode($this)); 
  107.  
  108. /** 
  109. * Gets the current XML node as a JSON string. 
  110. * @return string The current XML node as a JSON string. 
  111. */ 
  112. public function to_json() 
  113. return json_encode($this); 
  114.  
  115. /** 
  116. * Gets the current XML node as a YAML string. 
  117. * @return string The current XML node as a YAML string. 
  118. */ 
  119. public function to_yaml() 
  120. return sfYaml::dump(json_decode(json_encode($this), true), 5); 
  121.  
  122.  
  123. /**%******************************************************************************************%*/ 
  124. // COMPARISONS 
  125.  
  126. /** 
  127. * Whether or not the current node exactly matches the compared value. 
  128. * @param string $value (Required) The value to compare the current node to. 
  129. * @return boolean Whether or not the current node exactly matches the compared value. 
  130. */ 
  131. public function is($value) 
  132. return ((string) $this === $value); 
  133.  
  134. /** 
  135. * Whether or not the current node contains the compared value. 
  136. * @param string $value (Required) The value to use to determine whether it is contained within the node. 
  137. * @return boolean Whether or not the current node contains the compared value. 
  138. */ 
  139. public function contains($value) 
  140. return (stripos((string) $this, $value) !== false);