Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 * Copyright 2010-2012 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
 
18
/*%******************************************************************************************%*/
19
// CLASS
20
 
21
/**
22
 * Wraps the underlying `SimpleXMLIterator` class with enhancements for rapidly traversing the DOM tree,
23
 * converting types, and comparisons.
24
 *
25
 * @version 2012.01.17
26
 * @license See the included NOTICE.md file for more information.
27
 * @copyright See the included NOTICE.md file for more information.
28
 * @link http://aws.amazon.com/php/ PHP Developer Center
29
 * @link http://php.net/SimpleXML SimpleXML
30
 */
31
class CFSimpleXML extends SimpleXMLIterator
32
{
33
	/**
34
	 * Stores the namespace name to use in XPath queries.
35
	 */
36
	public $xml_ns;
37
 
38
	/**
39
	 * Stores the namespace URI to use in XPath queries.
40
	 */
41
	public $xml_ns_url;
42
 
43
	/**
44
	 * Catches requests made to methods that don't exist. Specifically, looks for child nodes via XPath.
45
	 *
46
	 * @param string $name (Required) The name of the method.
47
	 * @param array $arguments (Required) The arguments passed to the method.
48
	 * @return mixed Either an array of matches, or a single <CFSimpleXML> element.
49
	 */
50
	public function __call($name, $arguments)
51
	{
52
		// Remap $this
53
		$self = $this;
54
 
55
		// Re-base the XML
56
		$self = new CFSimpleXML($self->asXML());
57
 
58
		// Determine XPath query
59
		$self->xpath_expression = 'descendant-or-self::' . $name;
60
 
61
		// Get the results and augment with CFArray
62
		$results = $self->xpath($self->xpath_expression);
63
		if (!count($results)) return false;
64
		$results = new CFArray($results);
65
 
66
		// If an integer was passed, return only that result
67
		if (isset($arguments[0]) && is_int($arguments[0]))
68
		{
69
			if (isset($results[$arguments[0]]))
70
			{
71
				return $results[$arguments[0]];
72
			}
73
 
74
			return false;
75
		}
76
 
77
		return $results;
78
	}
79
 
80
	/**
81
	 * Alternate approach to constructing a new instance. Supports chaining.
82
	 *
83
	 * @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>.
84
	 * @param integer $options (Optional) Used to specify additional LibXML parameters. The default value is <code>0</code>.
85
	 * @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>.
86
	 * @param string $ns (Optional) The XML namespace to return values for.
87
	 * @param boolean $is_prefix (Optional) (No description provided by PHP.net.)
88
	 * @return CFSimpleXML Creates a new <CFSimpleXML> element.
89
	 */
90
	public static function init($data, $options = 0, $data_is_url, $ns, $is_prefix = false)
91
	{
92
		if (version_compare(PHP_VERSION, '5.3.0', '<'))
93
		{
94
			throw new Exception('PHP 5.3 or newer is required to instantiate a new class with CLASS::init().');
95
		}
96
 
97
		$self = get_called_class();
98
		return new $self($data, $options, $data_is_url, $ns, $is_prefix);
99
	}
100
 
101
 
102
	/*%******************************************************************************************%*/
103
	// TRAVERSAL
104
 
105
	/**
106
	 * Wraps the results of an XPath query in a <CFArray> object.
107
	 *
108
	 * @param string $expr (Required) The XPath expression to use to query the XML response.
109
	 * @return CFArray A <CFArray> object containing the results of the XPath query.
110
	 */
111
	public function query($expr)
112
	{
113
		return new CFArray($this->xpath($expr));
114
	}
115
 
116
	/**
117
	 * Gets the parent or a preferred ancestor of the current element.
118
	 *
119
	 * @param string $node (Optional) Name of the ancestor element to match and return.
120
	 * @return CFSimpleXML A <CFSimpleXML> object containing the requested node.
121
	 */
122
	public function parent($node = null)
123
	{
124
		if ($node)
125
		{
126
			$parents = $this->xpath('ancestor-or-self::' . $node);
127
		}
128
		else
129
		{
130
			$parents = $this->xpath('parent::*');
131
		}
132
 
133
		return $parents[0];
134
	}
135
 
136
 
137
	/*%******************************************************************************************%*/
138
	// ALTERNATE FORMATS
139
 
140
	/**
141
	 * Gets the current XML node as a true string.
142
	 *
143
	 * @return string The current XML node as a true string.
144
	 */
145
	public function to_string()
146
	{
147
		return (string) $this;
148
	}
149
 
150
	/**
151
	 * Gets the current XML node as <CFArray>, a child class of PHP's <php:ArrayObject> class.
152
	 *
153
	 * @return CFArray The current XML node as a <CFArray> object.
154
	 */
155
	public function to_array()
156
	{
157
		return new CFArray(json_decode(json_encode($this), true));
158
	}
159
 
160
	/**
161
	 * Gets the current XML node as a stdClass object.
162
	 *
163
	 * @return array The current XML node as a stdClass object.
164
	 */
165
	public function to_stdClass()
166
	{
167
		return json_decode(json_encode($this));
168
	}
169
 
170
	/**
171
	 * Gets the current XML node as a JSON string.
172
	 *
173
	 * @return string The current XML node as a JSON string.
174
	 */
175
	public function to_json()
176
	{
177
		return json_encode($this);
178
	}
179
 
180
	/**
181
	 * Gets the current XML node as a YAML string.
182
	 *
183
	 * @return string The current XML node as a YAML string.
184
	 */
185
	public function to_yaml()
186
	{
187
		return sfYaml::dump(json_decode(json_encode($this), true), 5);
188
	}
189
 
190
 
191
	/*%******************************************************************************************%*/
192
	// COMPARISONS
193
 
194
	/**
195
	 * Whether or not the current node exactly matches the compared value.
196
	 *
197
	 * @param string $value (Required) The value to compare the current node to.
198
	 * @return boolean Whether or not the current node exactly matches the compared value.
199
	 */
200
	public function is($value)
201
	{
202
		return ((string) $this === $value);
203
	}
204
 
205
	/**
206
	 * Whether or not the current node contains the compared value.
207
	 *
208
	 * @param string $value (Required) The value to use to determine whether it is contained within the node.
209
	 * @return boolean Whether or not the current node contains the compared value.
210
	 */
211
	public function contains($value)
212
	{
213
		return (stripos((string) $this, $value) !== false);
214
	}
215
}