Passed
Push — master ( 3e732d...8f4dd6 )
by Gerben
01:27
created

Validator::getEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Hyperized\Xml;
4
5
use DOMDocument;
6
use Hyperized\Xml\Constants\ErrorMessages;
7
use Hyperized\Xml\Constants\Strings;
8
use Hyperized\Xml\Exceptions\InvalidXml;
9
10
/**
11
 * Class Validator
12
 *
13
 * @package Hyperized\Xml
14
 * Based on: http://stackoverflow.com/a/30058598/1757763
15
 */
16
final class Validator
17
{
18
    /**
19
     * @var string
20
     */
21
    private $version = Strings::VERSION;
22
    /**
23
     * @var string
24
     */
25 8
    private $encoding = Strings::UTF_8;
26
27 8
    /**
28
     * @param  string $xmlFilename
29
     * @param  string $xsdFile
30
     * @return bool
31
     * @throws InvalidXml
32
     */
33
    public function isXMLFileValid(
34
        string $xmlFilename,
35
        string $xsdFile = null
36
    ): bool {
37
        return $this->isXMLStringValid(file_get_contents($xmlFilename), $xsdFile);
38
    }
39 14
40
    /**
41 14
     * @param  string $xml
42 4
     * @param  string $xsdFile
43
     * @return bool
44 10
     * @throws InvalidXml
45
     */
46
    public function isXMLStringValid(
47
        string $xml,
48
        string $xsdFile = null
49
    ): bool {
50
        if (\is_string($xsdFile)) {
51
            return $this->isXMLValid($xml, $xsdFile);
52
        }
53
        return $this->isXMLValid($xml);
54
    }
55
56 14
    /**
57
     * @param string      $xmlContent
58 14
     * @param string|null $xsdFile
59 2
     *
60
     * @return bool
61
     * @throws InvalidXml
62 12
     */
63
    private function isXMLValid(
64 12
        string $xmlContent,
65 12
        string $xsdFile = null
66 12
    ): bool {
67 4
        if (trim($xmlContent) === '') {
68
            throw new InvalidXml(ErrorMessages::XML_EMPTY_TRIMMED);
69
        }
70 12
71 12
        libxml_use_internal_errors(true);
72 12
73
        $document = new DOMDocument($this->version, $this->encoding);
74 6
        $document->loadXML($xmlContent);
75 6
        if (\is_string($xsdFile)) {
76 6
            $document->schemaValidate($xsdFile);
77
        }
78 6
79
        $errors = libxml_get_errors();
80 6
        libxml_clear_errors();
81
        if (!empty($errors)) {
82
            $return = [];
83
            foreach ($errors as $error) {
84
                $return[] = trim($error->message);
85
            }
86
            throw new InvalidXml(implode(Strings::NEW_LINE, $return));
87
        }
88
        return true;
89
    }
90
91
    /**
92
     * @param string $version
93
     */
94
    public function setVersion(string $version): void
95
    {
96
        $this->version = $version;
97
    }
98
99
    /**
100
     * @param string $encoding
101
     */
102
    public function setEncoding(string $encoding): void
103
    {
104
        $this->encoding = $encoding;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getVersion(): string
111
    {
112
        return $this->version;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getEncoding(): string
119
    {
120
        return $this->encoding;
121
    }
122
}
123