Passed
Pull Request — master (#13)
by Gerben
01:56
created

Validator::isXMLStringValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\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 8
     * @throws InvalidXml
34
     */
35
    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
        if (\is_string($xsdPath)) {
49
            return $this->isXMLValid($xml, $xsdPath);
50 14
        }
51 4
        return $this->isXMLValid($xml);
52
    }
53 10
54
    /**
55
     * @param  string      $xmlContent
56
     * @param  string|null $xsdPath
57
     * @return bool
58
     * @throws InvalidXml
59
     */
60
    private function isXMLValid(string $xmlContent, string $xsdPath = null): bool
61
    {
62
        if (trim($xmlContent) === '') {
63 14
            throw new InvalidXml(ErrorMessages::XML_EMPTY_TRIMMED);
64
        }
65
66
        libxml_use_internal_errors(true);
67 14
68 2
        $document = new DOMDocument($this->version, $this->encoding);
69
        $document->loadXML($xmlContent);
70
        if (isset($xsdPath)) {
71 12
            $document->schemaValidate($xsdPath);
72
        }
73 12
74 12
        $errors = libxml_get_errors();
75 12
        libxml_clear_errors();
76 4
        if (!empty($errors)) {
77
            $return = [];
78
            foreach ($errors as $error) {
79 12
                $return[] = trim($error->message);
80 12
            }
81 12
            throw new InvalidXml(implode(Strings::NEW_LINE, $return));
82 6
        }
83 6
        return true;
84 6
    }
85
86 6
    /**
87
     * @param  string $fileName
88 6
     * @return string
89
     * @throws FileCouldNotBeOpenedException
90
     */
91
    private static function getFileContent(string $fileName): string
92
    {
93
        $contents = file_get_contents($fileName);
94 2
        if (!\is_string($contents)) {
0 ignored issues
show
introduced by
The condition is_string($contents) is always true.
Loading history...
95
            throw new FileCouldNotBeOpenedException(ErrorMessages::NO_FILE_CONTENTS);
96 2
        }
97 2
        return $contents;
98
    }
99
100
    /**
101
     * @return string
102 2
     */
103
    public function getVersion(): string
104 2
    {
105 2
        return $this->version;
106
    }
107
108
    /**
109
     * @param string $version
110 2
     */
111
    public function setVersion(string $version): void
112 2
    {
113
        $this->version = $version;
114
    }
115
116
    /**
117
     * @return string
118 2
     */
119
    public function getEncoding(): string
120 2
    {
121
        return $this->encoding;
122
    }
123
124
    /**
125
     * @param string $encoding
126
     */
127
    public function setEncoding(string $encoding): void
128
    {
129
        $this->encoding = $encoding;
130
    }
131
}
132