|
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> intenal 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
|
|
|
*/ |
|
31
|
2 |
|
public function getImporterXsd(): string |
|
32
|
|
|
{ |
|
33
|
2 |
|
$xsd = new DOMDocument('1.0', 'UTF-8'); |
|
34
|
2 |
|
$xsd->loadXML('<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>'); |
|
35
|
|
|
/** @var DOMElement $document */ |
|
36
|
2 |
|
$document = $xsd->documentElement; |
|
37
|
2 |
|
foreach ($this->schemas as $schema) { |
|
38
|
1 |
|
$node = $xsd->createElementNS('http://www.w3.org/2001/XMLSchema', 'import'); |
|
39
|
1 |
|
$node->setAttribute('namespace', $schema->getNamespace()); |
|
40
|
1 |
|
$node->setAttribute('schemaLocation', str_replace('\\', '/', $schema->getLocation())); |
|
41
|
1 |
|
$document->appendChild($node); |
|
42
|
|
|
} |
|
43
|
2 |
|
return $xsd->saveXML() ?: ''; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create a new schema and inserts it to the collection |
|
48
|
|
|
* The returned object is the created schema |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $namespace |
|
51
|
|
|
* @param string $location |
|
52
|
|
|
* @return Schema |
|
53
|
|
|
*/ |
|
54
|
5 |
|
public function create(string $namespace, string $location): Schema |
|
55
|
|
|
{ |
|
56
|
5 |
|
return $this->insert(new Schema($namespace, $location)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Insert (add or replace) a schema to the collection |
|
61
|
|
|
* The returned object is the same schema |
|
62
|
|
|
* |
|
63
|
|
|
* @param Schema $schema |
|
64
|
|
|
* @return Schema |
|
65
|
|
|
*/ |
|
66
|
7 |
|
public function insert(Schema $schema): Schema |
|
67
|
|
|
{ |
|
68
|
7 |
|
$this->schemas[$schema->getNamespace()] = $schema; |
|
69
|
7 |
|
return $schema; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Import the schemas from other schema collection to this collection |
|
74
|
|
|
* |
|
75
|
|
|
* @param Schemas $schemas |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function import(self $schemas): void |
|
78
|
|
|
{ |
|
79
|
1 |
|
foreach ($schemas->getIterator() as $schema) { |
|
80
|
1 |
|
$this->insert($schema); |
|
81
|
|
|
} |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Remove a schema based on its namespace |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $namespace |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function remove(string $namespace): void |
|
91
|
|
|
{ |
|
92
|
1 |
|
unset($this->schemas[$namespace]); |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return the complete collection of schemas as an associative array |
|
97
|
|
|
* |
|
98
|
|
|
* @return array<string, Schema> |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function all(): array |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->schemas; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Check if a schema exists by its namespace |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $namespace |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function exists(string $namespace): bool |
|
112
|
|
|
{ |
|
113
|
2 |
|
return array_key_exists($namespace, $this->schemas); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get an schema object by its namespace |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $namespace |
|
120
|
|
|
* @throws NamespaceNotFoundInSchemas when namespace does not exists on schema |
|
121
|
|
|
* @return Schema |
|
122
|
|
|
*/ |
|
123
|
4 |
|
public function item(string $namespace): Schema |
|
124
|
|
|
{ |
|
125
|
4 |
|
if (! array_key_exists($namespace, $this->schemas)) { |
|
126
|
1 |
|
throw NamespaceNotFoundInSchemas::create($namespace); |
|
127
|
|
|
} |
|
128
|
3 |
|
return $this->schemas[$namespace]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Count elements on the collection |
|
133
|
|
|
* |
|
134
|
|
|
* @return int |
|
135
|
|
|
*/ |
|
136
|
6 |
|
public function count(): int |
|
137
|
|
|
{ |
|
138
|
6 |
|
return count($this->schemas); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** @return Traversable<string, Schema> */ |
|
142
|
2 |
|
public function getIterator() |
|
143
|
|
|
{ |
|
144
|
2 |
|
return new ArrayIterator($this->schemas); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|