Completed
Pull Request — master (#13)
by Gerben
01:58
created

Validator::isXMLFileValid()   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 2
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\FileCouldNotBeOpenedException;
9
use Hyperized\Xml\Exceptions\InvalidXml;
10
11
/**
12
 * Class Validator
13
 *
14
 * @package Hyperized\Xml
15
 * Based on: http://stackoverflow.com/a/30058598/1757763
16
 */
17
final class Validator implements ValidatorInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $version = Strings::VERSION;
23
    /**
24
     * @var string
25
     */
26
    private $encoding = Strings::UTF_8;
27
28
    /**
29
     * @param  string      $xmlPath
30
     * @param  string|null $xsdPath
31
     * @return bool
32
     * @throws FileCouldNotBeOpenedException
33
     * @throws InvalidXml
34
     */
35 8
    public function isXMLFileValid(string $xmlPath, string $xsdPath = null): bool
36
    {
37 8
        return $this->isXMLStringValid(self::getFileContent($xmlPath), $xsdPath);
38
    }
39
40
    /**
41
     * @param  string      $xml
42
     * @param  string|null $xsdPath
43
     * @return bool
44
     * @throws InvalidXml
45
     */
46 14
    public function isXMLStringValid(string $xml, string $xsdPath = null): bool
47
    {
48 14
        if (\is_string($xsdPath)) {
49 4
            return $this->isXMLValid($xml, $xsdPath);
50
        }
51 10
        return $this->isXMLValid($xml);
52
    }
53
54
    /**
55
     * @param  string      $xmlContent
56
     * @param  string|null $xsdPath
57
     * @return bool
58
     * @throws InvalidXml
59
     */
60 14
    private function isXMLValid(string $xmlContent, string $xsdPath = null): bool
61
    {
62 14
        self::checkEmptyWhenTrimmed($xmlContent);
63
64 12
        libxml_use_internal_errors(true);
65
66 12
        $document = new DOMDocument($this->version, $this->encoding);
67 12
        $document->loadXML($xmlContent);
68 12
        if (isset($xsdPath)) {
69 4
            $document->schemaValidate($xsdPath);
70
        }
71
72 12
        $errors = libxml_get_errors();
73 12
        libxml_clear_errors();
74 12
        self::parseErrors($errors);
75 6
        return true;
76
    }
77
78
    /**
79
     * @param  string $xmlContent
80
     * @throws InvalidXml
81
     */
82 14
    private static function checkEmptyWhenTrimmed(string $xmlContent): void
83
    {
84 14
        if (trim($xmlContent) === '') {
85 2
            throw new InvalidXml(ErrorMessages::XML_EMPTY_TRIMMED);
86
        }
87 12
    }
88
89
    /**
90
     * @param  array $errors
91
     * @throws InvalidXml
92
     */
93 12
    private static function parseErrors(array $errors): void
94
    {
95 12
        if (!empty($errors)) {
96 6
            $return = [];
97 6
            foreach ($errors as $error) {
98 6
                $return[] = trim($error->message);
99
            }
100 6
            throw new InvalidXml(implode(Strings::NEW_LINE, $return));
101
        }
102 6
    }
103
104
    /**
105
     * @param  string $fileName
106
     * @return string
107
     * @throws FileCouldNotBeOpenedException
108
     */
109 8
    private static function getFileContent(string $fileName): string
110
    {
111 8
        $contents = file_get_contents($fileName);
112 8
        if (!\is_string($contents)) {
0 ignored issues
show
introduced by
The condition is_string($contents) is always true.
Loading history...
113
            throw new FileCouldNotBeOpenedException(ErrorMessages::NO_FILE_CONTENTS);
114
        }
115 8
        return $contents;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 2
    public function getVersion(): string
122
    {
123 2
        return $this->version;
124
    }
125
126
    /**
127
     * @param string $version
128
     */
129 2
    public function setVersion(string $version): void
130
    {
131 2
        $this->version = $version;
132 2
    }
133
134
    /**
135
     * @return string
136
     */
137 2
    public function getEncoding(): string
138
    {
139 2
        return $this->encoding;
140
    }
141
142
    /**
143
     * @param string $encoding
144
     */
145 2
    public function setEncoding(string $encoding): void
146
    {
147 2
        $this->encoding = $encoding;
148 2
    }
149
}
150