Passed
Push — master ( 40f6c7...45ad77 )
by Brian
02:37
created

LaravelXml::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
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 7
    public function __construct()
12
    {
13
        // Silence is golden...
14 7
    }
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
     *
25
     * @return string
26
     */
27 1
    public function encode(
28
        $arr,
29
        $rootElementName = 'document',
30
        $elementCase = 'snake',
31
        $xmlVersion = '1.0',
32
        $xmlEncoding = 'UTF-8'
33
    ) {
34 1
        return ArrayToXml::convert($arr, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding);
35
    }
36
37
    /**
38
     * Convert a string of XML into an array.
39
     *
40
     * @see http://php.net/manual/en/function.simplexml-load-string.php
41
     * @see https://stackoverflow.com/a/20431742/2732184
42
     * @see https://stackoverflow.com/a/2970701/2732184
43
     *
44
     * @param string $data A well-formed XML string
45
     * @param string $class_name Default: SimpleXMLElement
46
     * @param int $options
47
     * @param string $ns Namespace prefix or URI
48
     * @param bool $is_prefix TRUE if ns is a prefix, FALSE if it's a URI, defaults to FALSE
49
     *
50
     * @return mixed Array or FALSE on failure
51
     */
52 2
    public function decode($data, $class_name = 'SimpleXMLElement', $options = 0, $ns = '', $is_prefix = false)
53
    {
54 2
        $simple_xml = simplexml_load_string($data, $class_name, $options, $ns, $is_prefix);
55
56 2
        $json_simple_xml = new JsonSimpleXMLElementDecorator($simple_xml);
57
58 2
        return json_decode(json_encode($json_simple_xml), true);
59
    }
60
61
    /**
62
     * Check if a string is valid XML.
63
     *
64
     * @param string $xmlStr
65
     * @param bool $ignoreHtml
66
     *
67
     * @return bool
68
     */
69 1
    public function is_valid($xmlStr, $ignoreHtml = true)
70
    {
71 1
        return (new XmlValidator())->is_valid($xmlStr, $ignoreHtml);
72
    }
73
74
    /**
75
     * Validate XML string.
76
     *
77
     * @param string $xmlStr
78
     * @param string $xsdFilePath
79
     * @param int $flags
80
     * @param bool $checkXml
81
     *
82
     * @return array Rrrors
83
     */
84 3
    public function validate($xmlStr, $xsdFilePath, $flags = 0, $checkXml = false)
85
    {
86 3
        return (new XmlValidator())->validate($xmlStr, $xsdFilePath, $flags, $checkXml);
87
    }
88
}
89