Hoa central
Basic.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Xml\Element;
38 
39 use Hoa\Stream;
40 use Hoa\Xml;
41 
50 class Basic
51  extends \SimpleXMLElement
52  implements Element,
54 {
60  protected static $_cssToXPath = null;
61 
67  protected static $_buffer = null;
68 
69 
70 
76  public function selectRoot()
77  {
78  self::$_buffer = null;
79 
80  return simplexml_import_dom(
81  $this->readDOM()->ownerDocument->documentElement,
82  get_class($this)
83  );
84  }
85 
91  public function selectAnyElements()
92  {
93  self::$_buffer = null;
94 
95  return $this->xpath('//__current_ns:*');
96  }
97 
104  public function selectElements($E = null)
105  {
106  if (null === $E) {
107  return $this->selectAnyElements();
108  }
109 
110  self::$_buffer = null;
111 
112  return $this->xpath('//__current_ns:' . $E);
113  }
114 
121  public function selectDescendantElements($F = null)
122  {
123  return $this->selectElements($F);
124  }
125 
132  public function selectChildElements($F = null)
133  {
134  self::$_buffer = null;
135 
136  if (null === $F || '*' == $F) {
137  return $this->xpath('child::__current_ns:*');
138  }
139 
140  return $this->xpath('child::__current_ns:' . $F);
141  }
142 
149  public function selectAdjacentSiblingElement($F)
150  {
151  self::$_buffer = null;
152  $handle = $this->xpath(
153  'following-sibling::__current_ns:*[1]/self::__current_ns:' . $F
154  );
155 
156  if (empty($handle)) {
157  return false;
158  }
159 
160  return $handle[0];
161  }
162 
169  public function selectSiblingElements($F = null)
170  {
171  if (null === $F) {
172  $F = '*';
173  }
174 
175  self::$_buffer = null;
176 
177  return $this->xpath('following-sibling::__current_ns:' . $F);
178  }
179 
187  public function querySelector($query)
188  {
189  $handle = $this->querySelectorAll($query);
190 
191  if (empty($handle)) {
192  return false;
193  }
194 
195  return $handle[0];
196  }
197 
205  public function querySelectorAll($query)
206  {
207  self::$_buffer = null;
208  $_cssToXPath = self::getCssToXPathInstance();
209  $_cssToXPath->compile($query);
210 
211  return $this->xpath($_cssToXPath->getXPath());
212  }
213 
219  public function readAttributes()
220  {
221  $handle = (array) $this->attributes();
222 
223  if (!isset($handle['@attributes'])) {
224  $handle = (array) $this;
225 
226  if (!isset($handle['@attributes'])) {
227  return [];
228  }
229  }
230 
231  return $handle['@attributes'];
232  }
233 
240  public function readAttribute($name)
241  {
242  $attributes = $this->readAttributes();
243 
244  if (false === array_key_exists($name, $attributes)) {
245  return null;
246  }
247 
248  return $attributes[$name];
249  }
250 
257  public function attributeExists($name)
258  {
259  return true === array_key_exists($name, $this->readAttributes());
260  }
261 
267  public function readAttributesAsList()
268  {
269  $attributes = $this->readAttributes();
270 
271  foreach ($attributes as $name => &$value) {
272  $value = explode(' ', $value);
273  }
274 
275  return $attributes;
276  }
277 
284  public function readAttributeAsList($name)
285  {
286  return explode(' ', $this->readAttribute($name));
287  }
288 
298  public function readCustomAttributes($set)
299  {
300  $out = [];
301  $set .= '-';
302  $strlen = strlen($set);
303 
304  foreach ($this->readAttributes() as $name => $value) {
305  if ($set === substr($name, 0, $strlen)) {
306  $out[substr($name, $strlen)] = $value;
307  }
308  }
309 
310  return $out;
311  }
312 
319  public function readCustomAttributesAsList($set)
320  {
321  $out = [];
322 
323  foreach ($this->readCustomAttributes($set) as $name => $value) {
324  $out[$name] = explode(' ', $value);
325  }
326 
327  return $out;
328  }
329 
335  public function readAttributesAsString()
336  {
337  $out = null;
338 
339  foreach ($this->readAttributes() as $name => $value) {
340  $out .= ' ' . $name . '="' . str_replace('"', '\"', $value) . '"';
341  }
342 
343  return $out;
344  }
345 
351  public function readXML()
352  {
353  return $this->asXML();
354  }
355 
361  public function readDOM()
362  {
363  return dom_import_simplexml($this);
364  }
365 
378  public function readAsPhrasingModel($namespace = '', $element = '__text')
379  {
380  $out = [];
381  $list = $this->readDOM()->childNodes;
382  $class = get_class($this);
383 
384  for ($i = 0, $max = $list->length; $i < $max; ++$i) {
385  $node = $list->item($i);
386 
387  switch ($node->nodeType) {
388  case XML_ELEMENT_NODE:
389  $out[] = simplexml_import_dom($node, $class);
390 
391  break;
392 
393  case XML_TEXT_NODE:
394  $out[] = new $class(
395  '<' . $element . '>' . $node->nodeValue .
396  '</' . $element . '>',
397  LIBXML_NOXMLDECL,
398  false,
399  $namespace,
400  false
401  );
402 
403  break;
404  }
405  }
406 
407  return $out;
408  }
409 
418  public function useNamespace($namespace)
419  {
420  $this->registerXPathNamespace('__current_ns', $namespace);
421 
422  return $this;
423  }
424 
430  public static function getCssToXPathInstance()
431  {
432  if (null === self::$_cssToXPath) {
433  self::$_cssToXPath = new \Hoa\Xml\CssToXPath();
434  self::$_cssToXPath->setDefaultNamespacePrefix('__current_ns');
435  }
436 
437  return self::$_cssToXPath;
438  }
439 }
static getCssToXPathInstance()
Definition: Basic.php:430
static $_cssToXPath
Definition: Basic.php:60
readAsPhrasingModel($namespace= '', $element= '__text')
Definition: Basic.php:378
readCustomAttributes($set)
Definition: Basic.php:298
readCustomAttributesAsList($set)
Definition: Basic.php:319
selectAdjacentSiblingElement($F)
Definition: Basic.php:149
readAttribute($name)
Definition: Basic.php:240
querySelectorAll($query)
Definition: Basic.php:205
selectElements($E=null)
Definition: Basic.php:104
selectDescendantElements($F=null)
Definition: Basic.php:121
selectChildElements($F=null)
Definition: Basic.php:132
querySelector($query)
Definition: Basic.php:187
selectSiblingElements($F=null)
Definition: Basic.php:169
readAttributeAsList($name)
Definition: Basic.php:284
attributeExists($name)
Definition: Basic.php:257
useNamespace($namespace)
Definition: Basic.php:418