1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace AlgoWeb\ODataMetadata\Library; |
||
6 | |||
7 | use AlgoWeb\ODataMetadata\Helpers\ModelHelpers; |
||
8 | use AlgoWeb\ODataMetadata\Interfaces\Annotations; |
||
9 | use AlgoWeb\ODataMetadata\Interfaces\IEntityContainer; |
||
10 | use AlgoWeb\ODataMetadata\Interfaces\IFunction; |
||
11 | use AlgoWeb\ODataMetadata\Interfaces\IModel; |
||
12 | use AlgoWeb\ODataMetadata\Interfaces\ISchemaElement; |
||
13 | use AlgoWeb\ODataMetadata\Interfaces\ISchemaType; |
||
14 | use AlgoWeb\ODataMetadata\Interfaces\IStructuredType; |
||
15 | use AlgoWeb\ODataMetadata\Interfaces\IValueTerm; |
||
16 | use AlgoWeb\ODataMetadata\Interfaces\IVocabularyAnnotatable; |
||
17 | use AlgoWeb\ODataMetadata\Internal\RegistrationHelper; |
||
18 | use AlgoWeb\ODataMetadata\Library\Annotations\EdmDirectValueAnnotationsManager; |
||
19 | use AlgoWeb\ODataMetadata\Library\Core\EdmCoreModel; |
||
20 | |||
21 | /** |
||
22 | * Represents an EDM model. |
||
23 | * |
||
24 | * @package AlgoWeb\ODataMetadata\Library |
||
25 | */ |
||
26 | abstract class EdmModelBase extends EdmElement implements IModel |
||
27 | { |
||
28 | use ModelHelpers; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
29 | /** |
||
30 | * @var EdmDirectValueAnnotationsManager |
||
31 | */ |
||
32 | private $annotationsManager; |
||
33 | /** |
||
34 | * @var array<string, IEntityContainer> |
||
35 | */ |
||
36 | private $containersDictionary = []; |
||
37 | /** |
||
38 | * @var array<string, ISchemaType> |
||
39 | */ |
||
40 | private $schemaTypeDictionary = []; |
||
41 | /** |
||
42 | * @var array<string, IValueTerm> |
||
43 | */ |
||
44 | private $valueTermDictionary = []; |
||
45 | /** |
||
46 | * @var array<string, object> |
||
47 | */ |
||
48 | private $functionDictionary = []; |
||
49 | /** |
||
50 | * @var IModel[] |
||
51 | */ |
||
52 | private $referencedModels; |
||
53 | |||
54 | /** |
||
55 | * EdmModelBase constructor. |
||
56 | * @param IModel[] $referencedModels |
||
57 | * @param EdmDirectValueAnnotationsManager $annotationsManager |
||
58 | */ |
||
59 | public function __construct(array $referencedModels, EdmDirectValueAnnotationsManager $annotationsManager) |
||
60 | { |
||
61 | $this->referencedModels = $referencedModels; |
||
62 | $this->referencedModels[] = EdmCoreModel::getInstance(); |
||
63 | $this->annotationsManager = $annotationsManager; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return ISchemaElement[] gets the collection of schema elements that are contained in this model |
||
68 | */ |
||
69 | abstract public function getSchemaElements(): array; |
||
70 | |||
71 | /** |
||
72 | * @return Annotations\IVocabularyAnnotation[] gets the collection of vocabulary annotations that are contained in this model |
||
73 | */ |
||
74 | public function getVocabularyAnnotations(): array |
||
75 | { |
||
76 | return []; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return IModel[] gets the collection of models referred to by this model |
||
81 | */ |
||
82 | public function getReferencedModels(): array |
||
83 | { |
||
84 | return $this->referencedModels; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return Annotations\IDirectValueAnnotationsManager gets the model's annotations manager |
||
89 | */ |
||
90 | public function getDirectValueAnnotationsManager(): Annotations\IDirectValueAnnotationsManager |
||
91 | { |
||
92 | return $this->annotationsManager; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Searches for an entity container with the given name in this model and returns null if no such entity container |
||
97 | * exists. |
||
98 | * |
||
99 | * @param string $qualifiedName the name of the entity container being found |
||
100 | * @return ISchemaType|null The requested entity container, or null if no such entity container exists |
||
101 | */ |
||
102 | public function findDeclaredType(string $qualifiedName): ?ISchemaType |
||
103 | { |
||
104 | return array_key_exists($qualifiedName, $this->schemaTypeDictionary) ? $this->schemaTypeDictionary[$qualifiedName] : null; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Searches for a type with the given name in this model and returns null if no such type exists. |
||
109 | * |
||
110 | * @param string $name the qualified name of the type being found |
||
111 | * @return IEntityContainer|null the requested type, or null if no such type exists |
||
112 | */ |
||
113 | public function findDeclaredEntityContainer(string $name): ?IEntityContainer |
||
114 | { |
||
115 | return array_key_exists($name, $this->containersDictionary) ? $this->containersDictionary[$name] : null; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Searches for functions with the given name in this model and returns an empty enumerable if no such function |
||
120 | * exists. |
||
121 | * |
||
122 | * @param string $qualifiedName the qualified name of the function being found |
||
123 | * @return IFunction[] a set of functions sharing the specified qualified name, or an empty enumerable if no |
||
124 | * such function exists |
||
125 | */ |
||
126 | public function findDeclaredFunctions(string $qualifiedName): array |
||
127 | { |
||
128 | if (array_key_exists($qualifiedName, $this->functionDictionary)) { |
||
129 | $element = $this->functionDictionary[$qualifiedName]; |
||
130 | if ($element instanceof IFunction) { |
||
131 | return [$element]; |
||
132 | } elseif (is_array($element)) { |
||
0 ignored issues
–
show
|
|||
133 | return $element; |
||
134 | } |
||
135 | |||
136 | return [$element]; |
||
137 | } |
||
138 | |||
139 | return []; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Searches for a value term with the given name in this model and returns null if no such value term exists. |
||
144 | * |
||
145 | * @param string $qualifiedName the qualified name of the value term being found |
||
146 | * @return IValueTerm|null the requested value term, or null if no such value term exists |
||
147 | */ |
||
148 | public function findDeclaredValueTerm(string $qualifiedName): ?IValueTerm |
||
149 | { |
||
150 | return array_key_exists($qualifiedName, $this->valueTermDictionary) ? $this->valueTermDictionary[$qualifiedName] : null; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Searches for vocabulary annotations specified by this model. |
||
155 | * |
||
156 | * @param IVocabularyAnnotatable $element the annotated element |
||
157 | * @return Annotations\IVocabularyAnnotation[] the vocabulary annotations for the element |
||
158 | */ |
||
159 | public function findDeclaredVocabularyAnnotations(IVocabularyAnnotatable $element): array |
||
160 | { |
||
161 | return []; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Finds a list of types that derive directly from the supplied type. |
||
166 | * |
||
167 | * @param IStructuredType $baseType the base type that derived types are being searched for |
||
168 | * @return IStructuredType[] a list of types from this model that derive directly from the given type |
||
169 | */ |
||
170 | abstract public function findDirectlyDerivedTypes(IStructuredType $baseType): array; |
||
171 | |||
172 | /** |
||
173 | * Adds a schema element to this model. |
||
174 | * |
||
175 | * @param ISchemaElement $element the element to register |
||
176 | */ |
||
177 | protected function registerElement(ISchemaElement $element): void |
||
178 | { |
||
179 | RegistrationHelper::registerSchemaElement($element, $this->schemaTypeDictionary, $this->valueTermDictionary, $this->functionDictionary, $this->containersDictionary); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Adds a model reference to this model. |
||
184 | * |
||
185 | * @param IModel $model the model to reference |
||
186 | */ |
||
187 | protected function addReferencedModel(IModel $model): void |
||
188 | { |
||
189 | $this->referencedModels[] = $model; |
||
190 | } |
||
191 | } |
||
192 |