| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This file contains the class XML_Query2XML_Data_Source_XPath.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category XML
|
|
|
8 |
* @package XML_Query2XML
|
|
|
9 |
* @author Lukas Feiler <lukas.feiler@lukasfeiler.com>
|
|
|
10 |
* @copyright 2009 Lukas Feiler
|
|
|
11 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL Version 2.1
|
|
|
12 |
* @version CVS: $Id: XPath.php 276639 2009-03-01 13:17:08Z lukasfeiler $
|
|
|
13 |
* @link http://pear.php.net/package/XML_Query2XML
|
|
|
14 |
* @access private
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* XML_Query2XML_Data_Source_ColumnValue extends the class
|
|
|
19 |
* XML_Query2XML_Data_Source.
|
|
|
20 |
*/
|
|
|
21 |
require_once 'XML/Query2XML/Data/Source.php';
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Data Source Class that allows the results of an XPath query to be used
|
|
|
25 |
* as a data source.
|
|
|
26 |
*
|
|
|
27 |
* usage:
|
|
|
28 |
* <code>
|
|
|
29 |
* $commandObject = new XML_Query2XML_Data_Source_XPath(
|
|
|
30 |
* new DOMXPath(DOMDocument::load('albums.xml')),
|
|
|
31 |
* '/music_store/album[artist_id="?"]',
|
|
|
32 |
* array('artistid')
|
|
|
33 |
* );
|
|
|
34 |
* </code>
|
|
|
35 |
*
|
|
|
36 |
* @category XML
|
|
|
37 |
* @package XML_Query2XML
|
|
|
38 |
* @author Lukas Feiler <lukas.feiler@lukasfeiler.com>
|
|
|
39 |
* @copyright 2009 Lukas Feiler
|
|
|
40 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL Version 2.1
|
|
|
41 |
* @version Release: @package_version@
|
|
|
42 |
* @link http://pear.php.net/package/XML_Query2XML
|
|
|
43 |
* @access private
|
|
|
44 |
* @since Release 1.8.0RC1
|
|
|
45 |
*/
|
|
|
46 |
class XML_Query2XML_Data_Source_XPath extends XML_Query2XML_Data_Source
|
|
|
47 |
{
|
|
|
48 |
/**
|
|
|
49 |
* THe DOMXPath instance used.
|
|
|
50 |
* @var DOMXPath An instance of DOMXPath
|
|
|
51 |
*/
|
|
|
52 |
private $_xpath = null;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* An array of column names.
|
|
|
56 |
* @var array
|
|
|
57 |
*/
|
|
|
58 |
private $_data = array();
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* The placeholder string to be used.
|
|
|
62 |
* @var string
|
|
|
63 |
*/
|
|
|
64 |
private $_placeholder;
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Constructor function.
|
|
|
68 |
*
|
|
|
69 |
* @param DOMXPath $xpath An instance of DOMXPath.
|
|
|
70 |
* @param string $query The XPath query.
|
|
|
71 |
* @param mixed $data A string or an array of strings. Each string
|
|
|
72 |
* will replace an occurance of $placeholder
|
|
|
73 |
* within $query. This argument is optional.
|
|
|
74 |
* @param string $placeholder The string to use as a placeholder. The default
|
|
|
75 |
* is '?'. This argument is optional.
|
|
|
76 |
*/
|
|
|
77 |
public function __construct(DOMXPath $xpath,
|
|
|
78 |
$query,
|
|
|
79 |
$data = null,
|
|
|
80 |
$placeholder = '?')
|
|
|
81 |
{
|
|
|
82 |
$this->_xpath = $xpath;
|
|
|
83 |
$this->_query = $query;
|
|
|
84 |
if (is_string($data)) {
|
|
|
85 |
$this->_data = array($data);
|
|
|
86 |
} elseif (is_array($data)) {
|
|
|
87 |
$this->_data = $data;
|
|
|
88 |
} else {
|
|
|
89 |
// unit test: MISSING
|
|
|
90 |
throw new XML_Query2XML_ConfigException(
|
|
|
91 |
'XML_Query2XML_Data_Source_XPath::__construct(): '
|
|
|
92 |
. 'array or string expected as third argument.'
|
|
|
93 |
);
|
|
|
94 |
}
|
|
|
95 |
$this->_placeholder = $placeholder;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Creates a new instance of this class.
|
|
|
100 |
* This method is called by XML_Query2XML.
|
|
|
101 |
*
|
|
|
102 |
* @param string $stringDef String definition.
|
|
|
103 |
* @param string $configPath The configuration path within the $options array.
|
|
|
104 |
* This argument is optional.
|
|
|
105 |
*
|
|
|
106 |
* @return void
|
|
|
107 |
*/
|
|
|
108 |
public function create($stringDef, $configPath)
|
|
|
109 |
{
|
|
|
110 |
throw new XML_Query2XML_ConfigException(
|
|
|
111 |
'XML_Query2XML_Data_Source_XPath::create() is not yet fully implemented'
|
|
|
112 |
);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Called by XML_Query2XML for every record in the result set.
|
|
|
117 |
*
|
|
|
118 |
* @param array $record An associative array.
|
|
|
119 |
*
|
|
|
120 |
* @return array An array of DOMNode instances.
|
|
|
121 |
* @throws XML_Query2XML_ConfigException If any of the columns specified
|
|
|
122 |
* using the third constructor argument
|
|
|
123 |
* does not exist.
|
|
|
124 |
*/
|
|
|
125 |
public function execute(array $record)
|
|
|
126 |
{
|
|
|
127 |
$data = array();
|
|
|
128 |
foreach ($this->_data as $columnName) {
|
|
|
129 |
if (array_key_exists($columnName, $record)) {
|
|
|
130 |
$data[] = $record[$columnName];
|
|
|
131 |
} else {
|
|
|
132 |
// UNIT TEST: MISSING
|
|
|
133 |
throw new XML_Query2XML_ConfigException(
|
|
|
134 |
'XML_Query2XML_Data_Source_XPath::execute(): The column "'
|
|
|
135 |
. $columnName . '" was not found in the result set.'
|
|
|
136 |
);
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
$query = self::_replacePlaceholders(
|
|
|
140 |
$this->_query,
|
|
|
141 |
$data,
|
|
|
142 |
$this->_placeholder
|
|
|
143 |
);
|
|
|
144 |
|
|
|
145 |
$elements = array();
|
|
|
146 |
$result = @$this->_xpath->query($query);
|
|
|
147 |
if ($result === false) {
|
|
|
148 |
// UNIT TEST: MISSING
|
|
|
149 |
throw new XML_Query2XML_XMLException(
|
|
|
150 |
'XML_Query2XML_Data_Source_XPath::execute(): could not execute '
|
|
|
151 |
. 'XPath query "' . $query . '"'
|
|
|
152 |
);
|
|
|
153 |
}
|
|
|
154 |
foreach ($result as $element) {
|
|
|
155 |
$elements[] = $element;
|
|
|
156 |
}
|
|
|
157 |
return $elements;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Replaces all placeholder strings (e.g. '?') with replacement strings.
|
|
|
162 |
*
|
|
|
163 |
* @param string $string The string in which to replace the placeholder
|
|
|
164 |
* strings.
|
|
|
165 |
* @param array &$replacements An array of replacement strings.
|
|
|
166 |
* @param string $placeholder The placeholder string.
|
|
|
167 |
*
|
|
|
168 |
* @return string The modified version of $string.
|
|
|
169 |
*/
|
|
|
170 |
private static function _replacePlaceholders($string,
|
|
|
171 |
&$replacements,
|
|
|
172 |
$placeholder)
|
|
|
173 |
{
|
|
|
174 |
while (($pos = strpos($string, $placeholder)) !== false) {
|
|
|
175 |
if (count($replacements) > 0) {
|
|
|
176 |
$string = substr($string, 0, $pos) .
|
|
|
177 |
array_shift($replacements) .
|
|
|
178 |
substr($string, $pos+strlen($placeholder));
|
|
|
179 |
} else {
|
|
|
180 |
break;
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
return $string;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* This method is called by XML_Query2XML in case the asterisk shortcut was used.
|
|
|
188 |
*
|
|
|
189 |
* The interface XML_Query2XML_Data_Source requires an implementation of
|
|
|
190 |
* this method.
|
|
|
191 |
*
|
|
|
192 |
* @param string $columnName The column name that is to replace every occurance
|
|
|
193 |
* of the asterisk character ('*') in the second and
|
|
|
194 |
* third argument passed to the constructor ($query
|
|
|
195 |
* and $data).
|
|
|
196 |
*
|
|
|
197 |
* @return void
|
|
|
198 |
*/
|
|
|
199 |
public function replaceAsterisks($columnName)
|
|
|
200 |
{
|
|
|
201 |
$this->_query = str_replace('*', $columnName, $this->_query);
|
|
|
202 |
foreach ($this->_data as $key => $value) {
|
|
|
203 |
$this->_data[$key] = str_replace('*', $columnName, $value);
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Returns a textual representation of this instance.
|
|
|
209 |
* This might be useful for debugging.
|
|
|
210 |
*
|
|
|
211 |
* @return string
|
|
|
212 |
*/
|
|
|
213 |
public function toString()
|
|
|
214 |
{
|
|
|
215 |
return get_class($this) . '(' . $this->_query . ')';
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
?>
|