1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\LaravelXml\Support\Facades; |
4
|
|
|
|
5
|
|
|
use Bmatovu\LaravelXml\Support\ArrayToXml; |
6
|
|
|
use Bmatovu\LaravelXml\Support\JsonSimpleXMLElementDecorator; |
7
|
|
|
use Bmatovu\LaravelXml\Support\XmlValidator; |
8
|
|
|
|
9
|
|
|
class LaravelXml |
10
|
|
|
{ |
11
|
8 |
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
// Silence is golden... |
14
|
8 |
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Convert the an array to an xml string. |
18
|
|
|
* |
19
|
|
|
* @param string[] $arr |
20
|
|
|
* @param string $rootElementName |
21
|
|
|
* @param string $elementCase |
22
|
|
|
* @param string $xmlVersion |
23
|
|
|
* @param string $xmlEncoding |
24
|
|
|
* @param bool $xmlStandalone |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
2 |
|
public function encode( |
29
|
|
|
$arr, |
30
|
|
|
$rootElementName = 'document', |
31
|
|
|
$elementCase = 'snake', |
32
|
|
|
$xmlVersion = '1.0', |
33
|
|
|
$xmlEncoding = 'UTF-8', |
34
|
|
|
$xmlStandalone = false |
35
|
|
|
) { |
36
|
2 |
|
return ArrayToXml::convert($arr, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding, $xmlStandalone); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Convert a string of XML into an array. |
41
|
|
|
* |
42
|
|
|
* @see http://php.net/manual/en/function.simplexml-load-string.php |
43
|
|
|
* @see https://stackoverflow.com/a/20431742/2732184 |
44
|
|
|
* @see https://stackoverflow.com/a/2970701/2732184 |
45
|
|
|
* |
46
|
|
|
* @param string $data A well-formed XML string |
47
|
|
|
* @param string $class_name Default: SimpleXMLElement |
48
|
|
|
* @param int $options |
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, defaults to FALSE |
51
|
|
|
* |
52
|
|
|
* @return mixed Array or FALSE on failure |
53
|
|
|
*/ |
54
|
2 |
|
public function decode($data, $class_name = 'SimpleXMLElement', $options = 0, $ns = '', $is_prefix = false) |
55
|
|
|
{ |
56
|
2 |
|
$simple_xml = simplexml_load_string($data, $class_name, $options, $ns, $is_prefix); |
57
|
|
|
|
58
|
2 |
|
$json_simple_xml = new JsonSimpleXMLElementDecorator($simple_xml); |
59
|
|
|
|
60
|
2 |
|
return json_decode(json_encode($json_simple_xml), true); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if a string is valid XML. |
65
|
|
|
* |
66
|
|
|
* @param string $xmlStr |
67
|
|
|
* @param bool $ignoreHtml |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
1 |
|
public function is_valid($xmlStr, $ignoreHtml = true) |
72
|
|
|
{ |
73
|
1 |
|
return (new XmlValidator())->is_valid($xmlStr, $ignoreHtml); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Validate XML string. |
78
|
|
|
* |
79
|
|
|
* @param string $xmlStr |
80
|
|
|
* @param string $xsdFilePath |
81
|
|
|
* @param int $flags |
82
|
|
|
* @param bool $checkXml |
83
|
|
|
* |
84
|
|
|
* @return array Rrrors |
85
|
|
|
*/ |
86
|
3 |
|
public function validate($xmlStr, $xsdFilePath, $flags = 0, $checkXml = false) |
87
|
|
|
{ |
88
|
3 |
|
return (new XmlValidator())->validate($xmlStr, $xsdFilePath, $flags, $checkXml); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|