Hoa central
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Hoa\Xml\Xml Class Reference
Inheritance diagram for Hoa\Xml\Xml:

Public Member Functions

 __construct ($stream, Stream $innerStream, $initializeNamespace=true, $entityResolver=null)
 
 initializeNamespaces ()
 
 namespaceExists ($namespace)
 
 useNamespace ($namespace)
 
 getPrefix ($namespace)
 
 getNamespaces ()
 
 selectRoot ()
 
 selectAnyElements ()
 
 selectElements ($E=null)
 
 selectDescendantElements ($F=null)
 
 selectChildElements ($F=null)
 
 selectAdjacentSiblingElement ($F)
 
 selectSiblingElements ($F=null)
 
 querySelector ($query)
 
 querySelectorAll ($query)
 
 xpath ($path)
 
 __toString ()
 
 readAttributes ()
 
 readAttribute ($name)
 
 attributeExists ($name)
 
 readAttributesAsList ()
 
 readAttributeAsList ($name)
 
 readAttributesAsString ()
 
 readXML ()
 
 readDOM ()
 
 getName ()
 
 count ()
 
 getIterator ()
 
 __set ($name, $value)
 
 __get ($name)
 
 offsetExists ($offset)
 
 offsetGet ($offset)
 
 offsetSet ($offset, $value)
 
 offsetUnset ($offset)
 
 hasError ()
 
 getErrors ()
 
- Public Member Functions inherited from Hoa\Stream\Composite
 getInnerStream ()
 

Protected Member Functions

 clearErrors ()
 
- Protected Member Functions inherited from Hoa\Stream\Composite
 setStream ($stream)
 
 getStream ()
 
 setInnerStream (Stream $innerStream)
 

Protected Attributes

 $_namespaces = null
 
 $_errors = null
 
- Protected Attributes inherited from Hoa\Stream\Composite
 $_stream = null
 
 $_innerStream = null
 

Detailed Description

Class .

Definition at line 50 of file Xml.php.

Constructor & Destructor Documentation

Hoa\Xml\Xml::__construct (   $stream,
Stream  $innerStream,
  $initializeNamespace = true,
  $entityResolver = null 
)

Constructor. Load the inner stream as a XML tree. If we cannot load the inner stream and if it is a stream and if its content is empty, we use the follow default XML value: <?xml version="1.0" encoding="utf-8"?>

<handler xmlns="http://hoa-project.net/ns/xml/default"> </handler>

Parameters
string$streamStream name to use.
\Hoa\Stream$innerStreamInner stream.
bool$initializeNamespaceWhether we initialize namespaces.
mixed$entityResolverEntity resolver.
Returns
void
Exceptions

Definition at line 92 of file Xml.php.

97  {
98  if (!function_exists('simplexml_load_file')) {
99  throw new Exception(
100  'SimpleXML must be enable for using %s.',
101  0,
102  get_class($this)
103  );
104  }
105 
106  if (null !== $entityResolver) {
107  $entityResolver = xcallable($entityResolver);
108  }
109 
110  libxml_use_internal_errors(true);
111 
112  if (PHP_VERSION_ID >= 50400) {
113  libxml_set_external_entity_loader(
114  function ($public, $system, $context) use (&$entityResolver) {
115  if (null === $entityResolver) {
116  return null;
117  }
118 
119  return $entityResolver($public, $system, $context);
120  }
121  );
122  } else {
123  libxml_disable_entity_loader(true);
124  }
125 
126  if ($innerStream instanceof Stream\IStream\In) {
127  if ($innerStream instanceof Stream\IStream\Pointable) {
128  $innerStream->rewind();
129  }
130 
131  $handle = $innerStream->readAll();
132  $root = @simplexml_load_string($handle, $stream);
133  } else {
134  $root = @simplexml_load_file($innerStream->getStreamName(), $stream);
135  }
136 
137  $this->_errors = libxml_get_errors();
138  $this->clearErrors();
139 
140  if (false === $root || true === $this->hasError()) {
141  if (false === $this->hasError()) {
142  throw new Exception(
143  'Failed to open the XML document %s.',
144  1,
145  $innerStream->getStreamName()
146  );
147  }
148 
149  $errors = $this->getErrors();
150  $first = array_shift($errors);
151  $message =
152  ' • ' . trim(ucfirst($first->message)) .
153  ' (at line ' . $first->line .
154  ', column ' . $first->column . ')';
155 
156  foreach ($errors as $error) {
157  $message .=
158  ';' . "\n" .
159  ' • ' . trim(ucfirst($error->message)) .
160  ' (at line ' . $error->line .
161  ', column ' . $error->column . ')';
162  }
163 
164  $message .= '.' . "\n";
165 
166  if ($innerStream instanceof Stream\IStream\In) {
167  $xml = explode("\n", $innerStream->readAll());
168 
169  if (!empty($xml[0])) {
170  $message .=
171  "\n" . 'You should take a look at ' .
172  'this piece of code: ' . "\n";
173  $lines = count($xml) - 1;
174  $line = $first->line;
175  $foo = strlen((string) ($line + 3));
176 
177  for (
178  $i = max(1, $line - 3), $m = min($lines, $line + 3);
179  $i <= $m;
180  ++$i
181  ) {
182  $message .= sprintf('%' . $foo . 'd', $i) . '. ';
183 
184  if ($i == $line) {
185  $message .= '➜ ';
186  } else {
187  $message .= ' ';
188  }
189 
190  $message .= $xml[$i - 1] . "\n";
191  }
192  }
193  }
194 
195  throw new Exception(
196  'Errors occured while parsing the XML document %s:' .
197  "\n" . '%s',
198  2,
199  [$innerStream->getStreamName(), $message]
200  );
201  }
202 
203  $this->setStream($root);
204  $this->setInnerStream($innerStream);
205 
206  if (true === $initializeNamespace) {
207  $this->initializeNamespaces();
208  }
209 
210  return;
211  }
setInnerStream(Stream $innerStream)
Definition: Composite.php:95
hasError()
Definition: Xml.php:637
initializeNamespaces()
Definition: Xml.php:221
count()
Definition: Xml.php:541
clearErrors()
Definition: Xml.php:657
getErrors()
Definition: Xml.php:647

Here is the call graph for this function:

Member Function Documentation

Hoa\Xml\Xml::__get (   $name)

Get a child.

Parameters
string$nameChild value.
Returns
mixed

Definition at line 576 of file Xml.php.

577  {
578  return $this->getStream()->$name;
579  }

Here is the call graph for this function:

Hoa\Xml\Xml::__set (   $name,
  $value 
)

Set a child.

Parameters
string$nameChild name.
mixed$valueChild value.
Returns
void

Definition at line 563 of file Xml.php.

564  {
565  $this->getStream()->$name = $value;
566 
567  return;
568  }

Here is the call graph for this function:

Hoa\Xml\Xml::__toString ( )

Transform this object to a string.

Returns
string

Definition at line 438 of file Xml.php.

439  {
440  return $this->getStream()->__toString();
441  }

Here is the call graph for this function:

Hoa\Xml\Xml::attributeExists (   $name)

Whether an attribute exists.

Parameters
string$nameAttribute's name.
Returns
bool

Definition at line 470 of file Xml.php.

471  {
472  return $this->getStream()->attributeExists($name);
473  }

Here is the call graph for this function:

Hoa\Xml\Xml::clearErrors ( )
protected

Clear libXMLError.

Returns
void

Definition at line 657 of file Xml.php.

658  {
659  return libxml_clear_errors();
660  }

Here is the caller graph for this function:

Hoa\Xml\Xml::count ( )

Count children number.

Returns
int

Definition at line 541 of file Xml.php.

542  {
543  return $this->getStream()->count();
544  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Xml\Xml::getErrors ( )

Get all errors (as an array of libXMLError structures).

Returns
array

Definition at line 647 of file Xml.php.

648  {
649  return $this->_errors;
650  }
$_errors
Definition: Xml.php:70

Here is the caller graph for this function:

Hoa\Xml\Xml::getIterator ( )

Get the iterator.

Returns

Definition at line 551 of file Xml.php.

552  {
553  return $this->getStream();
554  }

Here is the call graph for this function:

Hoa\Xml\Xml::getName ( )

Get the name of the XML element.

Returns
string

Definition at line 531 of file Xml.php.

532  {
533  return $this->getStream()->getName();
534  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Xml\Xml::getNamespaces ( )

Get declared namespaces.

Returns
array

Definition at line 314 of file Xml.php.

315  {
316  if (null === $this->_namespaces) {
317  $this->initializeNamespaces();
318  }
319 
320  return $this->_namespaces;
321  }
$_namespaces
Definition: Xml.php:63
initializeNamespaces()
Definition: Xml.php:221

Here is the call graph for this function:

Hoa\Xml\Xml::getPrefix (   $namespace)

Get namespace prefix.

Parameters
string$namespaceNamespace.
Returns
string
Exceptions

Definition at line 296 of file Xml.php.

297  {
298  if (false === $prefix = array_search($namespace, $this->_namespaces)) {
299  throw new Exception(
300  'The namespace %s does not exist in the document %s.',
301  6,
302  [$namespace, $this->getInnerStream()->getStreamName()]
303  );
304  }
305 
306  return $prefix;
307  }

Here is the call graph for this function:

Hoa\Xml\Xml::hasError ( )

Whether an error occured or not.

Returns
boolean

Definition at line 637 of file Xml.php.

638  {
639  return !empty($this->_errors);
640  }

Here is the caller graph for this function:

Hoa\Xml\Xml::initializeNamespaces ( )

Initialize namespaces. If your document has no namespace, some of the Element::select*() methods could not work properly.

Returns
void
Exceptions

Definition at line 221 of file Xml.php.

222  {
223  $stream = $this->getStream();
224  $this->_namespaces = $stream->getDocNamespaces();
225 
226  if (empty($this->_namespaces)) {
227  throw new Exception\NamespaceMissing(
228  'The XML document %s must have a default namespace at least.',
229  4,
230  $this->getInnerStream()->getStreamName()
231  );
232  }
233 
234  if (1 == count($this->_namespaces)) {
235  $stream->registerXPathNamespace(
236  '__current_ns',
237  current($this->_namespaces)
238  );
239  } else {
240  foreach ($this->_namespaces as $prefix => $namespace) {
241  if ('' == $prefix) {
242  $prefix = '__current_ns';
243  }
244 
245  $stream->registerXPathNamespace($prefix, $namespace);
246  }
247  }
248 
249  return;
250  }
count()
Definition: Xml.php:541

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Xml\Xml::namespaceExists (   $namespace)

Whether a namespace exists.

Parameters
string$namespaceNamespace.
Returns
bool

Definition at line 258 of file Xml.php.

259  {
260  return false !== array_search($namespace, $this->_namespaces);
261  }

Here is the caller graph for this function:

Hoa\Xml\Xml::offsetExists (   $offset)

Check if an attribute exists.

Parameters
string$offsetAttribute name.
Returns
bool

Definition at line 587 of file Xml.php.

588  {
589  return null !== $this->readAttribute($offset);
590  }
readAttribute($name)
Definition: Xml.php:459

Here is the call graph for this function:

Hoa\Xml\Xml::offsetGet (   $offset)

Get an attribute.

Parameters
string$offsetAttribute name.
Returns
array

Definition at line 598 of file Xml.php.

599  {
600  return $this->readAttribute($offset);
601  }
readAttribute($name)
Definition: Xml.php:459

Here is the call graph for this function:

Hoa\Xml\Xml::offsetSet (   $offset,
  $value 
)

Set a value to the attribute.

Parameters
string$offsetAttribute name.
string$valueAttribute value.
Returns
void

Definition at line 610 of file Xml.php.

611  {
612  $handle = $this->getStream();
613  $handle[$offset] = $value;
614 
615  return;
616  }

Here is the call graph for this function:

Hoa\Xml\Xml::offsetUnset (   $offset)

Remove an attribute.

Parameters
string$offsetAttribute name.
Returns
void

Definition at line 624 of file Xml.php.

625  {
626  $handle = $this->getStream();
627  unset($handle[$offset]);
628 
629  return;
630  }

Here is the call graph for this function:

Hoa\Xml\Xml::querySelector (   $query)

Execute a query selector and return the first result.

Parameters
string$queryQuery.
Returns
Exceptions

Implements Hoa\Stream\IStream\Structural.

Definition at line 405 of file Xml.php.

406  {
407  return $this->getStream()->querySelector($query);
408  }

Here is the call graph for this function:

Hoa\Xml\Xml::querySelectorAll (   $query)

Execute a query selector and return one or many results.

Parameters
string$queryQuery.
Returns
Exceptions
array

Implements Hoa\Stream\IStream\Structural.

Definition at line 417 of file Xml.php.

418  {
419  return $this->getStream()->querySelectorAll($query);
420  }

Here is the call graph for this function:

Hoa\Xml\Xml::readAttribute (   $name)

Read a specific attribute.

Parameters
string$nameAttribute's name.
Returns
string

Definition at line 459 of file Xml.php.

460  {
461  return $this->getStream()->readAttribute($name);
462  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Xml\Xml::readAttributeAsList (   $name)

Read a attribute value as a list.

Parameters
string$nameAttribute's name.
Returns
array

Definition at line 491 of file Xml.php.

492  {
493  return $this->getStream()->readAttributeAsList($name);
494  }

Here is the call graph for this function:

Hoa\Xml\Xml::readAttributes ( )

Read all attributes.

Returns
array

Definition at line 448 of file Xml.php.

449  {
450  return $this->getStream()->readAttributes();
451  }

Here is the call graph for this function:

Hoa\Xml\Xml::readAttributesAsList ( )

Read attributes value as a list.

Returns
array

Definition at line 480 of file Xml.php.

481  {
482  return $this->getStream()->readAttributesAsList();
483  }

Here is the call graph for this function:

Hoa\Xml\Xml::readAttributesAsString ( )

Read attributes as a string.

Returns
string

Definition at line 501 of file Xml.php.

502  {
503  return $this->getStream()->readAttributesAsString();
504  }

Here is the call graph for this function:

Hoa\Xml\Xml::readDOM ( )

Read content as a DOM tree.

Returns

Definition at line 521 of file Xml.php.

522  {
523  return $this->getStream()->readDOM();
524  }

Here is the call graph for this function:

Hoa\Xml\Xml::readXML ( )

Read all with XML node.

Returns
string

Definition at line 511 of file Xml.php.

512  {
513  return $this->getStream()->asXML();
514  }

Here is the call graph for this function:

Hoa\Xml\Xml::selectAdjacentSiblingElement (   $F)

Select an F element immediately preceded by an E element: E + F.

Parameters
string$FElement F.
Returns

Implements Hoa\Stream\IStream\Structural.

Definition at line 382 of file Xml.php.

383  {
384  return $this->getStream()->selectAdjacentSiblingElement($F);
385  }

Here is the call graph for this function:

Hoa\Xml\Xml::selectAnyElements ( )

Select any elements: *.

Returns
array

Implements Hoa\Stream\IStream\Structural.

Definition at line 338 of file Xml.php.

339  {
340  return $this->getStream()->selectAnyElements();
341  }

Here is the call graph for this function:

Hoa\Xml\Xml::selectChildElements (   $F = null)

Select F elements children of an E element: E > F.

Parameters
string$FElement F.
Returns
array

Implements Hoa\Stream\IStream\Structural.

Definition at line 371 of file Xml.php.

372  {
373  return $this->getStream()->selectChildElements($F);
374  }

Here is the call graph for this function:

Hoa\Xml\Xml::selectDescendantElements (   $F = null)

Select F elements descendant of an E element: E F.

Parameters
string$FElement F.
Returns
array

Implements Hoa\Stream\IStream\Structural.

Definition at line 360 of file Xml.php.

361  {
362  return $this->getStream()->selectDescendantElements($F);
363  }

Here is the call graph for this function:

Hoa\Xml\Xml::selectElements (   $E = null)

Select elements of type E: E.

Parameters
string$EElement E.
Returns
array

Implements Hoa\Stream\IStream\Structural.

Definition at line 349 of file Xml.php.

350  {
351  return $this->getStream()->selectElements($E);
352  }

Here is the call graph for this function:

Hoa\Xml\Xml::selectRoot ( )

Select root of the document: :root.

Returns

Implements Hoa\Stream\IStream\Structural.

Definition at line 328 of file Xml.php.

329  {
330  return $this->getStream()->selectRoot();
331  }

Here is the call graph for this function:

Hoa\Xml\Xml::selectSiblingElements (   $F = null)

Select F elements preceded by an E element: E ~ F.

Parameters
string$FElement F.
Returns
array

Implements Hoa\Stream\IStream\Structural.

Definition at line 393 of file Xml.php.

394  {
395  return $this->getStream()->selectSiblingElements($F);
396  }

Here is the call graph for this function:

Hoa\Xml\Xml::useNamespace (   $namespace)

Use a specific namespace.

Parameters
string$namespaceNamespace.
Returns
Exceptions

Definition at line 270 of file Xml.php.

271  {
272  if (null === $this->_namespaces) {
273  $this->initializeNamespaces();
274  }
275 
276  if (false === $prefix = array_search($namespace, $this->_namespaces)) {
277  throw new Exception(
278  'The namespace %s does not exist in the document %s.',
279  5,
280  [$namespace, $this->getInnerStream()->getStreamName()]
281  );
282  }
283 
284  $this->getStream()->registerXPathNamespace('__current_ns', $namespace);
285 
286  return $this;
287  }
initializeNamespaces()
Definition: Xml.php:221

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Xml\Xml::xpath (   $path)

Run a XPath query on this tree.

Parameters
string$pathXPath query.
Returns
array

Definition at line 428 of file Xml.php.

429  {
430  return $this->getStream()->xpath($path);
431  }

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Hoa\Xml\Xml::$_errors = null
protected

Definition at line 70 of file Xml.php.

Hoa\Xml\Xml::$_namespaces = null
protected

Definition at line 63 of file Xml.php.


The documentation for this class was generated from the following file: