Passed
Push — master ( cb02e9...c4035d )
by Gabriel
01:46
created

Xml::toObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/** @noinspection PhpComposerExtensionStubsInspection */
3
4
namespace Nip\Utility;
5
6
use DOMDocument;
7
use DOMText;
8
use Nip\Utility\Xml\FromArrayBuilder;
9
use SebastianBergmann\CodeCoverage\XmlException;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCoverage\XmlException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SimpleXMLElement;
11
12
/**
13
 * Class Xml
14
 * @package Nip\Utility
15
 *
16
 * @inspiration https://github.com/cakephp/utility/blob/master/Xml.php
17
 */
18
class Xml
19
{
20
    public static function toObject($xml): \SimpleXMLElement
21
    {
22
        return simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);;
0 ignored issues
show
Bug Best Practice introduced by
The expression return simplexml_load_st...Utility\LIBXML_NOCDATA) could return the type false which is incompatible with the type-hinted return SimpleXMLElement. Consider adding an additional type-check to rule them out.
Loading history...
23
    }
24
25
    /**
26
     * @param          $input
27
     * @param   array  $options
28
     *
29
     * @return DOMDocument|SimpleXMLElement
30
     */
31
    public static function fromArray($input, array $options = [])
32
    {
33
        return FromArrayBuilder::build($input, $options);
34
    }
35
36
37
    /**
38
     * @param $xml
39
     * @param $schema
40
     *
41
     * @throws \Exception
42
     */
43
    public static function validate($xml, $schema)
44
    {
45
        libxml_use_internal_errors(true);
46
        $xmlDocument = new \DOMDocument();
47
        $xmlDocument->loadXML($xml);
48
49
        $schema = static::prepareSchema($schema);
50
51
        if (!$xmlDocument->schemaValidateSource($schema)) {
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type false; however, parameter $source of DOMDocument::schemaValidateSource() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        if (!$xmlDocument->schemaValidateSource(/** @scrutinizer ignore-type */ $schema)) {
Loading history...
52
            $errors = libxml_get_errors();
53
54
            foreach ($errors as $error) {
55
                throw new \Exception(static::displayError($error));
56
            }
57
            libxml_clear_errors();
58
            throw new \Exception('INVALID XML');
59
        }
60
    }
61
62
    /**
63
     * @param $error
64
     *
65
     * @return string
66
     */
67
    public static function displayError($error): string
68
    {
69
        $return = "\n";
70
        switch ($error->level) {
71
            case LIBXML_ERR_WARNING:
72
                $return .= "Warning $error->code: ";
73
                break;
74
            case LIBXML_ERR_ERROR:
75
                $return .= "Error $error->code: ";
76
                break;
77
            case LIBXML_ERR_FATAL:
78
                $return .= "Fatal Error $error->code: ";
79
                break;
80
        }
81
        $return .= trim($error->message);
82
        $return .= "\n";
83
84
        return $return;
85
    }
86
87
    /**
88
     * @param $schema
89
     *
90
     * @return false|string
91
     */
92
    protected static function prepareSchema($schema)
93
    {
94
        if (!Url::isValid($schema)) {
95
            return $schema;
96
        }
97
        $response = file_get_contents($schema);
98
99
        return $response;
100
    }
101
102
103
}