|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Bmatovu\LaravelXml\Support\ArrayToXml; |
|
4
|
|
|
use Bmatovu\LaravelXml\Support\JsonSimpleXMLElementDecorator; |
|
5
|
|
|
use Bmatovu\LaravelXml\Support\XmlValidator; |
|
6
|
|
|
|
|
7
|
|
|
if (! function_exists('xml_encode')) { |
|
8
|
|
|
/** |
|
9
|
|
|
* Convert the an array to an xml string. |
|
10
|
|
|
* |
|
11
|
|
|
* @param string[] $array |
|
12
|
|
|
* @param string $rootElementName |
|
13
|
|
|
* @param string $elementCase |
|
14
|
|
|
* @param string $xmlVersion |
|
15
|
|
|
* @param string $xmlEncoding |
|
16
|
|
|
* @param bool $xmlStandalone |
|
17
|
|
|
* |
|
18
|
|
|
* @return string |
|
19
|
|
|
*/ |
|
20
|
|
|
function xml_encode(array $array, $rootElementName = 'document', $elementCase = 'slug', $xmlVersion = '1.0', $xmlEncoding = 'UTF-8', $xmlStandalone = false) |
|
21
|
|
|
{ |
|
22
|
|
|
return ArrayToXml::convert($array, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding, $xmlStandalone); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (! function_exists('xml_decode')) { |
|
27
|
|
|
/** |
|
28
|
|
|
* Convert a string of XML into an array. |
|
29
|
|
|
* |
|
30
|
|
|
* @see http://php.net/manual/en/function.simplexml-load-string.php |
|
31
|
|
|
* @see https://stackoverflow.com/a/20431742/2732184 |
|
32
|
|
|
* @see https://stackoverflow.com/a/2970701/2732184 |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $data A well-formed XML string |
|
35
|
|
|
* @param string $class_name [optional] Default: SimpleXMLElement |
|
36
|
|
|
* @param int $options |
|
37
|
|
|
* @param string $namespace_or_prefix [optional] Namespace prefix or URI |
|
38
|
|
|
* @param bool $is_prefix [optional] TRUE if ns is a prefix, FALSE if it's a URI, defaults to FALSE |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed Array or FALSE on failure |
|
41
|
|
|
*/ |
|
42
|
|
|
function xml_decode($data, $class_name = SimpleXMLElement::class, $options = 0, $namespace_or_prefix = '', $is_prefix = false) |
|
43
|
|
|
{ |
|
44
|
|
|
$simple_xml = simplexml_load_string($data, $class_name, $options, $namespace_or_prefix, $is_prefix); |
|
45
|
|
|
|
|
46
|
|
|
$json_simple_xml = new JsonSimpleXMLElementDecorator($simple_xml); |
|
47
|
|
|
|
|
48
|
|
|
return json_decode(json_encode($json_simple_xml), true); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (! function_exists('is_valid_xml')) { |
|
53
|
|
|
/** |
|
54
|
|
|
* Check if a string is valid XML. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $xml |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
function is_valid_xml($xml) |
|
61
|
|
|
{ |
|
62
|
|
|
$validator = new XmlValidator(); |
|
63
|
|
|
|
|
64
|
|
|
return $validator->is_valid($xml); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (! function_exists('validate_xml')) { |
|
69
|
|
|
/** |
|
70
|
|
|
* Validate XML string. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $xml |
|
73
|
|
|
* @param string $xsd file |
|
74
|
|
|
* |
|
75
|
|
|
* @return array errors |
|
76
|
|
|
*/ |
|
77
|
|
|
function validate_xml($xml, $xsd) |
|
78
|
|
|
{ |
|
79
|
|
|
$validator = new XmlValidator(); |
|
80
|
|
|
|
|
81
|
|
|
return $validator->validate($xml, $xsd); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|