Schemas   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
c 3
b 0
f 0
dl 0
loc 127
ccs 32
cts 32
cp 1
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 3 1
A count() 0 3 1
A insert() 0 4 1
A getIterator() 0 3 1
A remove() 0 3 1
A import() 0 4 2
A create() 0 3 1
A all() 0 3 1
A item() 0 6 2
A getImporterXsd() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Eclipxe\XmlSchemaValidator;
6
7
use ArrayIterator;
8
use Countable;
9
use DOMDocument;
10
use DOMElement;
11
use Eclipxe\XmlSchemaValidator\Exceptions\NamespaceNotFoundInSchemas;
12
use IteratorAggregate;
13
use Traversable;
14
15
/**
16
 * Collection of Schema objects, used by SchemaValidator
17
 *
18
 * @implements IteratorAggregate<string, Schema>
19
 */
20
class Schemas implements IteratorAggregate, Countable
21
{
22
    /** @var array<string, Schema> internal collection of schemas */
23
    private $schemas = [];
24
25
    /**
26
     * Return the XML of a Xsd that includes all the namespaces
27
     * with the local location
28
     *
29
     * @return string
30
     * @noinspection PhpDocMissingThrowsInspection
31
     */
32 3
    public function getImporterXsd(): string
33
    {
34 3
        $xsd = new DOMDocument('1.0', 'UTF-8');
35 3
        $xsd->loadXML('<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>');
36
        /** @var DOMElement $document */
37 3
        $document = $xsd->documentElement;
38 3
        foreach ($this->schemas as $schema) {
39
            /** @noinspection PhpUnhandledExceptionInspection */
40 2
            $node = $xsd->createElementNS('http://www.w3.org/2001/XMLSchema', 'import');
41 2
            $node->setAttribute('namespace', $schema->getNamespace());
42 2
            $node->setAttribute('schemaLocation', str_replace('\\', '/', $schema->getLocation()));
43 2
            $document->appendChild($node);
44
        }
45 3
        return strval($xsd->saveXML());
46
    }
47
48
    /**
49
     * Create a new schema and inserts it to the collection
50
     * The returned object is the created schema
51
     *
52
     * @param string $namespace
53
     * @param string $location
54
     * @return Schema
55
     */
56 6
    public function create(string $namespace, string $location): Schema
57
    {
58 6
        return $this->insert(new Schema($namespace, $location));
59
    }
60
61
    /**
62
     * Insert (add or replace) a schema to the collection
63
     * The returned object is the same schema
64
     *
65
     * @param Schema $schema
66
     * @return Schema
67
     */
68 8
    public function insert(Schema $schema): Schema
69
    {
70 8
        $this->schemas[$schema->getNamespace()] = $schema;
71 8
        return $schema;
72
    }
73
74
    /**
75
     * Import the schemas from other schema collection to this collection
76
     *
77
     * @param Schemas $schemas
78
     */
79 1
    public function import(self $schemas): void
80
    {
81 1
        foreach ($schemas->getIterator() as $schema) {
82 1
            $this->insert($schema);
83
        }
84
    }
85
86
    /**
87
     * Remove a schema based on its namespace
88
     *
89
     * @param string $namespace
90
     * @return void
91
     */
92 1
    public function remove(string $namespace): void
93
    {
94 1
        unset($this->schemas[$namespace]);
95
    }
96
97
    /**
98
     * Return the complete collection of schemas as an associative array
99
     *
100
     * @return array<string, Schema>
101
     */
102 1
    public function all(): array
103
    {
104 1
        return $this->schemas;
105
    }
106
107
    /**
108
     * Check if a schema exists by its namespace
109
     *
110
     * @param string $namespace
111
     * @return bool
112
     */
113 2
    public function exists(string $namespace): bool
114
    {
115 2
        return array_key_exists($namespace, $this->schemas);
116
    }
117
118
    /**
119
     * Get a schema object by its namespace
120
     *
121
     * @param string $namespace
122
     * @throws NamespaceNotFoundInSchemas when namespace does not exist on schema
123
     * @return Schema
124
     */
125 4
    public function item(string $namespace): Schema
126
    {
127 4
        if (! array_key_exists($namespace, $this->schemas)) {
128 1
            throw NamespaceNotFoundInSchemas::create($namespace);
129
        }
130 3
        return $this->schemas[$namespace];
131
    }
132
133
    /**
134
     * Count elements on the collection
135
     *
136
     * @return int
137
     */
138 6
    public function count(): int
139
    {
140 6
        return count($this->schemas);
141
    }
142
143
    /** @return Traversable<string, Schema> */
144 2
    public function getIterator(): Traversable
145
    {
146 2
        return new ArrayIterator($this->schemas);
147
    }
148
}
149