Passed
Pull Request — master (#11)
by Gerben
03:41
created

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