|
1
|
|
|
<?php |
|
2
|
|
|
namespace XmlSchemaValidator; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Collection of Schema objects, used by SchemaValidator |
|
6
|
|
|
*/ |
|
7
|
|
|
class Schemas implements \IteratorAggregate, \Countable |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var Schema[] */ |
|
10
|
|
|
private $schemas = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Return the XML of a Xsd that includes all the namespaces |
|
14
|
|
|
* with the local location |
|
15
|
|
|
* |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
7 |
|
public function getImporterXsd(): string |
|
19
|
|
|
{ |
|
20
|
7 |
|
$xsd = new \DOMDocument('1.0', 'utf-8'); |
|
21
|
7 |
|
$xsd->loadXML('<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>'); |
|
22
|
7 |
|
foreach ($this->schemas as $schema) { |
|
23
|
6 |
|
$node = $xsd->createElementNS('http://www.w3.org/2001/XMLSchema', 'import'); |
|
24
|
6 |
|
$node->setAttribute('namespace', $schema->getNamespace()); |
|
25
|
6 |
|
$node->setAttribute('schemaLocation', $schema->getLocation()); |
|
26
|
6 |
|
$xsd->documentElement->appendChild($node); |
|
27
|
|
|
} |
|
28
|
7 |
|
return $xsd->saveXML(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create a new schema and inserts it to the collection |
|
33
|
|
|
* The returned object is the schema |
|
34
|
|
|
* @param string $namespace |
|
35
|
|
|
* @param string $location |
|
36
|
|
|
* @return Schema |
|
37
|
|
|
*/ |
|
38
|
9 |
|
public function create(string $namespace, string $location): Schema |
|
39
|
|
|
{ |
|
40
|
9 |
|
return $this->insert(new Schema($namespace, $location)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Insert a schema to the collection |
|
45
|
|
|
* The returned object is the same schema |
|
46
|
|
|
* @param Schema $schema |
|
47
|
|
|
* @return Schema |
|
48
|
|
|
*/ |
|
49
|
11 |
|
public function insert(Schema $schema): Schema |
|
50
|
|
|
{ |
|
51
|
11 |
|
$this->schemas[$schema->getNamespace()] = $schema; |
|
52
|
11 |
|
return $schema; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Remove a schema |
|
57
|
|
|
* @param string $namespace |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function remove(string $namespace) |
|
60
|
|
|
{ |
|
61
|
1 |
|
unset($this->schemas[$namespace]); |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Return the complete collection of schemas as an associative array |
|
66
|
|
|
* @return Schema[] |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function all(): array |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->schemas; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $namespace |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
4 |
|
public function exists(string $namespace): bool |
|
78
|
|
|
{ |
|
79
|
4 |
|
return array_key_exists($namespace, $this->schemas); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get an schema object by its namespace |
|
84
|
|
|
* @param string $namespace |
|
85
|
|
|
* @return Schema |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public function item(string $namespace): Schema |
|
88
|
|
|
{ |
|
89
|
3 |
|
if (! $this->exists($namespace)) { |
|
90
|
1 |
|
throw new \InvalidArgumentException("Namespace $namespace does not exists in the schemas"); |
|
91
|
|
|
} |
|
92
|
2 |
|
return $this->schemas[$namespace]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
12 |
|
public function count() |
|
96
|
|
|
{ |
|
97
|
12 |
|
return count($this->schemas); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
public function getIterator() |
|
101
|
|
|
{ |
|
102
|
1 |
|
return new \ArrayIterator($this->schemas); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|