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

Validator::checkEmptyWhenTrimmed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
        self::checkEmptyWhenTrimmed($xmlContent);
63 14
64
        libxml_use_internal_errors(true);
65
66
        $document = new DOMDocument($this->version, $this->encoding);
67 14
        $document->loadXML($xmlContent);
68 2
        if (isset($xsdPath)) {
69
            $document->schemaValidate($xsdPath);
70
        }
71 12
72
        $errors = libxml_get_errors();
73 12
        libxml_clear_errors();
74 12
        self::parseErrors($errors);
75 12
        return true;
76 4
    }
77
78
    /**
79 12
     * @param  string $xmlContent
80 12
     * @throws InvalidXml
81 12
     */
82 6
    private static function checkEmptyWhenTrimmed(string $xmlContent): void
83 6
    {
84 6
        if (trim($xmlContent) === '') {
85
            throw new InvalidXml(ErrorMessages::XML_EMPTY_TRIMMED);
86 6
        }
87
    }
88 6
89
    /**
90
     * @param  array $errors
91
     * @throws InvalidXml
92
     */
93
    private static function parseErrors(array $errors): void
94 2
    {
95
        if (!empty($errors)) {
96 2
            $return = [];
97 2
            foreach ($errors as $error) {
98
                $return[] = trim($error->message);
99
            }
100
            throw new InvalidXml(implode(Strings::NEW_LINE, $return));
101
        }
102 2
    }
103
104 2
    /**
105 2
     * @param  string $fileName
106
     * @return string
107
     * @throws FileCouldNotBeOpenedException
108
     */
109
    private static function getFileContent(string $fileName): string
110 2
    {
111
        try {
112 2
            $contents = file_get_contents($fileName);
113
        } catch (\Exception $exception) {
114
            throw new FileCouldNotBeOpenedException(ErrorMessages::NO_FILE_CONTENTS);
115
        }
116
        return $contents;
117
    }
118 2
119
    /**
120 2
     * @return string
121
     */
122
    public function getVersion(): string
123
    {
124
        return $this->version;
125
    }
126
127
    /**
128
     * @param string $version
129
     */
130
    public function setVersion(string $version): void
131
    {
132
        $this->version = $version;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getEncoding(): string
139
    {
140
        return $this->encoding;
141
    }
142
143
    /**
144
     * @param string $encoding
145
     */
146
    public function setEncoding(string $encoding): void
147
    {
148
        $this->encoding = $encoding;
149
    }
150
}
151