ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Handler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31
32class Handler implements HandlerInterface
33{
34 protected PathFactory $path_factory;
37
41 protected array $paths_by_slot_id = [];
42
46 protected array $identifiers_by_path;
47
48 public function __construct(
49 PathFactory $path_factory,
52 ) {
53 $this->path_factory = $path_factory;
54 $this->navigator_factory = $navigator_factory;
55 $this->structure = $structure;
56 }
57
58 public function pathForSlot(Identifier $identifier): PathInterface
59 {
60 if (isset($this->paths_by_slot_id[$identifier->value])) {
61 return $this->paths_by_slot_id[$identifier->value];
62 }
63 return $this->paths_by_slot_id[$identifier->value] = match ($identifier) {
64 Identifier::GENERAL_STRUCTURE => $this->buildPath('general', 'structure', 'value'),
65 Identifier::GENERAL_AGGREGATION_LEVEL => $this->buildPath('general', 'aggregationLevel', 'value'),
66 Identifier::GENERAL_COVERAGE => $this->buildPath('general', 'coverage', 'string'),
67 Identifier::GENERAL_IDENTIFIER_CATALOG => $this->buildPath('general', 'identifier', 'catalog'),
68 Identifier::LIFECYCLE_STATUS => $this->buildPath('lifeCycle', 'status', 'value'),
69 Identifier::LIFECYCLE_CONTRIBUTE_ROLE => $this->buildPath('lifeCycle', 'contribute', 'role', 'value'),
70 Identifier::LIFECYCLE_CONTRIBUTE_PUBLISHER => $this->buildPath('lifeCycle', 'contribute', 'entity'),
71 Identifier::METAMETADATA_IDENTIFIER_CATALOG => $this->buildPath('metaMetadata', 'identifier', 'catalog'),
72 Identifier::METAMETADATA_CONTRIBUTE_ROLE => $this->buildPath('metaMetadata', 'contribute', 'role', 'value'),
73 Identifier::METAMETADATA_SCHEMA => $this->buildPath('metaMetadata', 'metadataSchema'),
74 Identifier::TECHNICAL_REQUIREMENT_TYPE => $this->buildPath('technical', 'requirement', 'orComposite', 'type', 'value'),
75 Identifier::TECHNICAL_REQUIREMENT_BROWSER, Identifier::TECHNICAL_REQUIREMENT_OS => $this->buildPath('technical', 'requirement', 'orComposite', 'name', 'value'),
76 Identifier::TECHNICAL_OTHER_PLATFORM_REQUIREMENTS => $this->buildPath('technical', 'otherPlatformRequirements', 'string'),
77 Identifier::TECHNICAL_FORMAT => $this->buildPath('technical', 'format'),
78 Identifier::EDUCATIONAL_INTERACTIVITY_TYPE => $this->buildPath('educational', 'interactivityType', 'value'),
79 Identifier::EDUCATIONAL_LEARNING_RESOURCE_TYPE => $this->buildPath('educational', 'learningResourceType', 'value'),
80 Identifier::EDUCATIONAL_INTERACTIVITY_LEVEL => $this->buildPath('educational', 'interactivityLevel', 'value'),
81 Identifier::EDUCATIONAL_SEMANTIC_DENSITY => $this->buildPath('educational', 'semanticDensity', 'value'),
82 Identifier::EDCUCATIONAL_INTENDED_END_USER_ROLE => $this->buildPath('educational', 'intendedEndUserRole', 'value'),
83 Identifier::EDUCATIONAL_CONTEXT => $this->buildPath('educational', 'context', 'value'),
84 Identifier::EDUCATIONAL_DIFFICULTY => $this->buildPath('educational', 'difficulty', 'value'),
85 Identifier::EDUCATIONAL_TYPICAL_AGE_RANGE => $this->buildPath('educational', 'typicalAgeRange', 'string'),
86 Identifier::RIGHTS_COST => $this->buildPath('rights', 'cost', 'value'),
87 Identifier::RIGHTS_CP_AND_OTHER_RESTRICTIONS => $this->buildPath('rights', 'copyrightAndOtherRestrictions', 'value'),
88 Identifier::RIGHTS_DESCRIPTION => $this->buildPath('rights', 'description', 'string'),
89 Identifier::RELATION_KIND => $this->buildPath('relation', 'kind', 'value'),
90 Identifier::RELATION_RESOURCE_IDENTIFIER_CATALOG => $this->buildPath('relation', 'resource', 'identifier', 'catalog'),
91 Identifier::CLASSIFICATION_PURPOSE => $this->buildPath('classification', 'purpose', 'value'),
92 Identifier::CLASSIFICATION_KEYWORD => $this->buildPath('classification', 'keyword', 'string'),
93 Identifier::CLASSIFICATION_TAXPATH_SOURCE => $this->buildPath('classification', 'taxonPath', 'source', 'string'),
94 Identifier::CLASSIFICATION_TAXON_ENTRY => $this->buildPath('classification', 'taxonPath', 'taxon', 'entry', 'string'),
95 Identifier::NULL => $this->buildPath()
96 };
97 }
98
99 public function isSlotConditional(Identifier $identifier): bool
100 {
101 return !is_null($this->conditionForSlot($identifier));
102 }
103
104 public function conditionForSlot(Identifier $identifier): ?ConditionInterface
105 {
106 return match ($identifier) {
107 Identifier::LIFECYCLE_CONTRIBUTE_PUBLISHER => $this->buildCondition(
108 'publisher',
110 'role',
111 'value'
112 ),
113 Identifier::TECHNICAL_REQUIREMENT_BROWSER => $this->buildCondition(
114 'browser',
117 'type',
118 'value'
119 ),
120 Identifier::TECHNICAL_REQUIREMENT_OS => $this->buildCondition(
121 'operating system',
124 'type',
125 'value'
126 ),
127 default => null
128 };
129 }
130
132 PathInterface $path_to_element,
133 ?PathInterface $path_to_condition,
134 ?string $condition_value
135 ): Identifier {
136 foreach (Identifier::cases() as $identifier) {
137 if ($this->pathForSlot($identifier)->toString() !== $path_to_element->toString()) {
138 continue;
139 }
140
141 $condition = $this->conditionForSlot($identifier);
142 if (
143 $condition?->value() !== $condition_value ||
144 $condition?->path()?->toString() !== $path_to_condition?->toString()
145 ) {
146 continue;
147 }
148
149 return $identifier;
150 }
151 return Identifier::NULL;
152 }
153
154 public function allSlotsForPath(PathInterface $path_to_element): \Generator
155 {
156 if (!isset($this->identifiers_by_path)) {
157 $this->identifiers_by_path = [];
158 foreach (Identifier::cases() as $identifier) {
159 $path_for_slot = $this->pathForSlot($identifier);
160 $this->identifiers_by_path[$path_for_slot->toString()][] = $identifier;
161 }
162 }
163 yield from $this->identifiers_by_path[$path_to_element->toString()] ?? [];
164 }
165
166 public function doesSlotExist(
167 PathInterface $path_to_element,
168 ?PathInterface $path_to_condition,
169 ?string $condition_value
170 ): bool {
171 $identifier = $this->identiferFromPathAndCondition(
172 $path_to_element,
173 $path_to_condition,
174 $condition_value
175 );
176 return $identifier !== Identifier::NULL;
177 }
178
179 protected function buildPath(string ...$steps): PathInterface
180 {
181 $builder = $this->path_factory->custom();
182 foreach ($steps as $step) {
183 $builder = $builder->withNextStep($step);
184 }
185 return $builder->get();
186 }
187
188 protected function buildCondition(
189 string $condition_value,
190 string|StepToken ...$steps_to_condition,
192 $builder = $this->path_factory->custom();
193 foreach ($steps_to_condition as $step) {
194 if ($step === StepToken::SUPER) {
195 $builder = $builder->withNextStepToSuperElement();
196 continue;
197 }
198 $builder = $builder->withNextStep($step);
199 }
200 $path = $builder->withRelative(true)->get();
201
202 return new Condition(
203 $condition_value,
204 $path
205 );
206 }
207
208 public function dataTypeForSlot(Identifier $identifier): DataType
209 {
210 return $this->navigator_factory->structureNavigator(
211 $this->pathForSlot($identifier),
212 $this->structure->getRoot()
213 )->elementAtFinalStep()->getDefinition()->dataType();
214 }
215}
StructureSetInterface $structure
Definition: Handler.php:36
identiferFromPathAndCondition(PathInterface $path_to_element, ?PathInterface $path_to_condition, ?string $condition_value)
Definition: Handler.php:131
pathForSlot(Identifier $identifier)
Definition: Handler.php:58
NavigatorFactoryInterface $navigator_factory
Definition: Handler.php:35
isSlotConditional(Identifier $identifier)
Definition: Handler.php:99
doesSlotExist(PathInterface $path_to_element, ?PathInterface $path_to_condition, ?string $condition_value)
Definition: Handler.php:166
dataTypeForSlot(Identifier $identifier)
Definition: Handler.php:208
__construct(PathFactory $path_factory, NavigatorFactoryInterface $navigator_factory, StructureSetInterface $structure)
Definition: Handler.php:48
conditionForSlot(Identifier $identifier)
Definition: Handler.php:104
buildCondition(string $condition_value, string|StepToken ... $steps_to_condition,)
Definition: Handler.php:188
allSlotsForPath(PathInterface $path_to_element)
Definition: Handler.php:154
$path
Definition: ltiservices.php:30
StepToken
The string representation of these tokens must not occur as names of metadata elements.
Definition: StepToken.php:28