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

Validator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 14
eloc 30
dl 0
loc 113
c 0
b 0
f 0
ccs 35
cts 36
cp 0.9722
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isXMLValid() 0 24 5
A getVersion() 0 3 1
A getFileContent() 0 7 2
A isXMLFileValid() 0 3 1
A setVersion() 0 3 1
A getEncoding() 0 3 1
A setEncoding() 0 3 1
A isXMLStringValid() 0 6 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
     * @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
        if (trim($xmlContent) === '') {
63 2
            throw new InvalidXml(ErrorMessages::XML_EMPTY_TRIMMED);
64
        }
65
66 12
        libxml_use_internal_errors(true);
67
68 12
        $document = new DOMDocument($this->version, $this->encoding);
69 12
        $document->loadXML($xmlContent);
70 12
        if (isset($xsdPath)) {
71 4
            $document->schemaValidate($xsdPath);
72
        }
73
74 12
        $errors = libxml_get_errors();
75 12
        libxml_clear_errors();
76 12
        if (!empty($errors)) {
77 6
            $return = [];
78 6
            foreach ($errors as $error) {
79 6
                $return[] = trim($error->message);
80
            }
81 6
            throw new InvalidXml(implode(Strings::NEW_LINE, $return));
82
        }
83 6
        return true;
84
    }
85
86
    /**
87
     * @param  string $fileName
88
     * @return string
89
     * @throws FileCouldNotBeOpenedException
90
     */
91 8
    private static function getFileContent(string $fileName): string
92
    {
93 8
        $contents = file_get_contents($fileName);
94 8
        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
        }
97 8
        return $contents;
98
    }
99
100
    /**
101
     * @return string
102
     */
103 2
    public function getVersion(): string
104
    {
105 2
        return $this->version;
106
    }
107
108
    /**
109
     * @param string $version
110
     */
111 2
    public function setVersion(string $version): void
112
    {
113 2
        $this->version = $version;
114 2
    }
115
116
    /**
117
     * @return string
118
     */
119 2
    public function getEncoding(): string
120
    {
121 2
        return $this->encoding;
122
    }
123
124
    /**
125
     * @param string $encoding
126
     */
127 2
    public function setEncoding(string $encoding): void
128
    {
129 2
        $this->encoding = $encoding;
130 2
    }
131
}
132