| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\SimplexmlException; |
| 6 |
|
| 7 |
/** |
| 8 |
* This function takes a node of a DOM |
| 9 |
* document and makes it into a SimpleXML node. This new object can |
| 10 |
* then be used as a native SimpleXML element. |
| 11 |
* |
| 12 |
* @param \DOMNode $node A DOM Element node |
| 13 |
* @param string $class_name You may use this optional parameter so that |
| 14 |
* simplexml_import_dom will return an object of |
| 15 |
* the specified class. That class should extend the |
| 16 |
* SimpleXMLElement class. |
| 17 |
* @return \SimpleXMLElement Returns a SimpleXMLElement. |
| 18 |
* @throws SimplexmlException |
| 19 |
* |
| 20 |
*/ |
| 21 |
function simplexml_import_dom(\DOMNode $node, string $class_name = "SimpleXMLElement"): \SimpleXMLElement |
| 22 |
{ |
| 23 |
error_clear_last(); |
| 24 |
$result = \simplexml_import_dom($node, $class_name); |
| 25 |
if ($result === false) { |
| 26 |
throw SimplexmlException::createFromPhpError(); |
| 27 |
} |
| 28 |
return $result; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Convert the well-formed XML document in the given file to an object. |
| 34 |
* |
| 35 |
* @param string $filename Path to the XML file |
| 36 |
* |
| 37 |
* Libxml 2 unescapes the URI, so if you want to pass e.g. |
| 38 |
* b&c as the URI parameter a, |
| 39 |
* you have to call |
| 40 |
* simplexml_load_file(rawurlencode('http://example.com/?a=' . |
| 41 |
* urlencode('b&c'))). Since PHP 5.1.0 you don't need to do |
| 42 |
* this because PHP will do it for you. |
| 43 |
* @param string $class_name You may use this optional parameter so that |
| 44 |
* simplexml_load_file will return an object of |
| 45 |
* the specified class. That class should extend the |
| 46 |
* SimpleXMLElement class. |
| 47 |
* @param int $options Since PHP 5.1.0 and Libxml 2.6.0, you may also use the |
| 48 |
* options parameter to specify additional Libxml parameters. |
| 49 |
* @param string $ns Namespace prefix or URI. |
| 50 |
* @param bool $is_prefix TRUE if ns is a prefix, FALSE if it's a URI; |
| 51 |
* defaults to FALSE. |
| 52 |
* @return \SimpleXMLElement Returns an object of class SimpleXMLElement with |
| 53 |
* properties containing the data held within the XML document. |
| 54 |
* @throws SimplexmlException |
| 55 |
* |
| 56 |
*/ |
| 57 |
function simplexml_load_file(string $filename, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $is_prefix = false): \SimpleXMLElement |
| 58 |
{ |
| 59 |
error_clear_last(); |
| 60 |
$result = \simplexml_load_file($filename, $class_name, $options, $ns, $is_prefix); |
| 61 |
if ($result === false) { |
| 62 |
throw SimplexmlException::createFromPhpError(); |
| 63 |
} |
| 64 |
return $result; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* Takes a well-formed XML string and returns it as an object. |
| 70 |
* |
| 71 |
* @param string $data A well-formed XML string |
| 72 |
* @param string $class_name You may use this optional parameter so that |
| 73 |
* simplexml_load_string will return an object of |
| 74 |
* the specified class. That class should extend the |
| 75 |
* SimpleXMLElement class. |
| 76 |
* @param int $options Since PHP 5.1.0 and Libxml 2.6.0, you may also use the |
| 77 |
* options parameter to specify additional Libxml parameters. |
| 78 |
* @param string $ns Namespace prefix or URI. |
| 79 |
* @param bool $is_prefix TRUE if ns is a prefix, FALSE if it's a URI; |
| 80 |
* defaults to FALSE. |
| 81 |
* @return \SimpleXMLElement Returns an object of class SimpleXMLElement with |
| 82 |
* properties containing the data held within the xml document. |
| 83 |
* @throws SimplexmlException |
| 84 |
* |
| 85 |
*/ |
| 86 |
function simplexml_load_string(string $data, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $is_prefix = false): \SimpleXMLElement |
| 87 |
{ |
| 88 |
error_clear_last(); |
| 89 |
$result = \simplexml_load_string($data, $class_name, $options, $ns, $is_prefix); |
| 90 |
if ($result === false) { |
| 91 |
throw SimplexmlException::createFromPhpError(); |
| 92 |
} |
| 93 |
return $result; |
| 94 |
} |
| 95 |
|