1 | <?php |
||||||
2 | |||||||
3 | namespace Xml\Impl; |
||||||
4 | |||||||
5 | use Xml\ModelInterface; |
||||||
6 | use Xml\ModelBuilder; |
||||||
7 | use Xml\ModelInstanceInterface; |
||||||
8 | use Xml\Exception\ModelException; |
||||||
9 | use Xml\Impl\Instance\ModelElementInstanceImpl; |
||||||
10 | use Xml\Impl\Util\ModelUtil; |
||||||
11 | use Xml\Impl\Validation\ModelInstanceValidator; |
||||||
12 | use Xml\Instance\{ |
||||||
13 | DomDocumentInterface, |
||||||
14 | ModelElementInstanceInterface |
||||||
15 | }; |
||||||
16 | use Xml\Type\ModelElementTypeInterface; |
||||||
17 | use Xml\Validation\ValidationResultsInterface; |
||||||
18 | |||||||
19 | class ModelInstanceImpl implements ModelInstanceInterface |
||||||
20 | { |
||||||
21 | protected $document; |
||||||
22 | protected $model; |
||||||
23 | protected $modelBuilder; |
||||||
24 | |||||||
25 | public function __construct(ModelImpl $model, ModelBuilder $modelBuilder, DomDocumentInterface $document) |
||||||
26 | { |
||||||
27 | $this->document = $document; |
||||||
28 | $this->model = $model; |
||||||
29 | $this->modelBuilder = $modelBuilder; |
||||||
30 | } |
||||||
31 | |||||||
32 | public function getDocument(): DomDocumentInterface |
||||||
33 | { |
||||||
34 | return $this->document; |
||||||
35 | } |
||||||
36 | |||||||
37 | public function getDocumentElement(): ?ModelElementInstanceInterface |
||||||
38 | { |
||||||
39 | $rootElement = $this->document->getRootElement(); |
||||||
40 | if ($rootElement !== null) { |
||||||
41 | return ModelUtil::getModelElement($rootElement, $this); |
||||||
42 | } else { |
||||||
43 | return null; |
||||||
44 | } |
||||||
45 | } |
||||||
46 | |||||||
47 | public function setDocumentElement(ModelElementInstanceInterface $modelElement): void |
||||||
48 | { |
||||||
49 | ModelUtil::ensureInstanceOf($modelElement, ModelElementInstanceImpl::class); |
||||||
50 | $domElement = $modelElement->getDomElement(); |
||||||
51 | $this->document->setRootElement($domElement); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
52 | } |
||||||
53 | |||||||
54 | /** |
||||||
55 | * @param mixed $type |
||||||
56 | */ |
||||||
57 | public function newInstance($type, ?string $id = null): ModelElementInstanceInterface |
||||||
58 | { |
||||||
59 | if (is_string($type)) { |
||||||
60 | $modelElementType = $this->model->getType($type); |
||||||
61 | if ($modelElementType !== null) { |
||||||
62 | $type = $modelElementType; |
||||||
63 | } else { |
||||||
64 | throw new ModelException( |
||||||
65 | sprintf("Cannot create instance of ModelType %s: no such type registered.", $type) |
||||||
66 | ); |
||||||
67 | } |
||||||
68 | } |
||||||
69 | $modelElementInstance = $type->newInstance($this); |
||||||
70 | if (!empty($id)) { |
||||||
71 | ModelUtil::setNewIdentifier($type, $modelElementInstance, $id, false); |
||||||
72 | } else { |
||||||
73 | ModelUtil::setGeneratedUniqueIdentifier($type, $modelElementInstance, false); |
||||||
74 | } |
||||||
75 | return $modelElementInstance; |
||||||
76 | } |
||||||
77 | |||||||
78 | public function getModel(): ModelInterface |
||||||
79 | { |
||||||
80 | return $this->model; |
||||||
81 | } |
||||||
82 | |||||||
83 | public function registerGenericType(string $namespaceUri, string $localName): ModelElementTypeInterface |
||||||
84 | { |
||||||
85 | $elementType = $this->model->getTypeForName($namespaceUri, $localName); |
||||||
86 | if ($elementType === null) { |
||||||
87 | $elementType = $this->modelBuilder->defineGenericType($localName, $namespaceUri); |
||||||
88 | $model = $this->modelBuilder->build(); |
||||||
0 ignored issues
–
show
|
|||||||
89 | } |
||||||
90 | return $elementType; |
||||||
91 | } |
||||||
92 | |||||||
93 | public function getModelElementById(?string $id): ?ModelElementInstanceInterface |
||||||
94 | { |
||||||
95 | if ($id === null) { |
||||||
96 | return null; |
||||||
97 | } |
||||||
98 | $element = $this->document->getElementById($id); |
||||||
99 | if ($element !== null) { |
||||||
100 | return ModelUtil::getModelElement($element, $this); |
||||||
101 | } else { |
||||||
102 | return null; |
||||||
103 | } |
||||||
104 | } |
||||||
105 | |||||||
106 | /** |
||||||
107 | * @param mixed $reference |
||||||
108 | */ |
||||||
109 | public function getModelElementsByType($reference): array |
||||||
110 | { |
||||||
111 | if ($reference instanceof ModelElementTypeInterface) { |
||||||
112 | $extendingTypes = $reference->getAllExtendingTypes(); |
||||||
0 ignored issues
–
show
The method
getAllExtendingTypes() does not exist on Xml\Type\ModelElementTypeInterface . Did you maybe mean getExtendingTypes() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
113 | $instances = []; |
||||||
114 | foreach ($extendingTypes as $modelElementType) { |
||||||
115 | if (!($modelElementType->isAbstract())) { |
||||||
116 | $instances = array_merge($instances, $modelElementType->getInstances($this)); |
||||||
117 | } |
||||||
118 | } |
||||||
119 | return $instances; |
||||||
120 | } elseif (is_string($reference)) { |
||||||
121 | return $this->getModelElementsByType($this->getModel()->getType($reference)); |
||||||
122 | } |
||||||
123 | return []; |
||||||
124 | } |
||||||
125 | |||||||
126 | public function clone(): ModelInstanceInterface |
||||||
127 | { |
||||||
128 | return new ModelInstanceImpl($this->model, $this->modelBuilder, $this->document->clone()); |
||||||
129 | } |
||||||
130 | |||||||
131 | public function validate(array $validators): ValidationResultsInterface |
||||||
132 | { |
||||||
133 | return (new ModelInstanceValidator($this, $validators))->validate(); |
||||||
134 | } |
||||||
135 | } |
||||||
136 |