ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ConditionTypeRegistry.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use LogicException;
26use ReflectionClass;
27
37{
41 private array $condition_types;
42
46 private ?array $condition_classes = null;
47
51 public function __construct(array $condition_types)
52 {
53 $this->condition_types = $condition_types;
54 }
55
56 public function getAllInputConditions(): array
57 {
58 return $this->filterBy(InputConditionInterface::class);
59 }
60
61 public function getAllOutputConditions(): array
62 {
63 return $this->filterBy(OutputConditionInterface::class);
64 }
65
69 public function getAllConditions(): array
70 {
71 return $this->condition_classes ??= $this->validate(array_values(array_unique(array_map(
72 static fn(ConditionType $type): string => $type->getConditionClass(),
73 $this->condition_types
74 ))));
75 }
76
77 public function getConditionByName(string $name_to_find): ?string
78 {
79 return array_find(
80 $this->getAllConditions(),
81 static fn(string $class): bool => AbstractCondition::getIdentifierForClass($class) === $name_to_find
82 );
83 }
84
94 private function validate(array $classes): array
95 {
96 $classes_by_name = [];
97 foreach ($classes as $class) {
98 if (!class_exists($class)) {
99 throw new LogicException("Contributed condition class '{$class}' does not exist.");
100 }
101 if (!is_subclass_of($class, AbstractCondition::class)) {
102 throw new LogicException(
103 "Contributed condition class '{$class}' does not extend " . AbstractCondition::class . '.'
104 );
105 }
106 if (!(new ReflectionClass($class))->isInstantiable()) {
107 throw new LogicException("Contributed condition class '{$class}' cannot be instantiated.");
108 }
109 if (!is_subclass_of($class, InputConditionInterface::class)
110 && !is_subclass_of($class, OutputConditionInterface::class)
111 ) {
112 throw new LogicException(
113 "Contributed condition class '{$class}' is neither an input nor an output condition."
114 );
115 }
116
118 if (isset($classes_by_name[$name])) {
119 throw new LogicException(
120 "Contributed condition classes '{$classes_by_name[$name]}' and '{$class}' "
121 . "share the name '{$name}'."
122 );
123 }
124 $classes_by_name[$name] = $class;
125 }
126
127 return $classes;
128 }
129
133 private function filterBy(string $interface): array
134 {
135 return array_values(array_filter(
136 $this->getAllConditions(),
137 static fn(string $class): bool => is_subclass_of($class, $interface)
138 ));
139 }
140}
static getIdentifierForClass(string $class)
Extracts the identifier for the condition type from the class name.
validate(array $classes)
Makes sure every class is an instantiable condition which is either an input or an output condition,...
A type of condition, contributed to the LearningSequence component.
All condition types known to the system.
Marker interface for input conditions in the Learning Sequence condition system.
Marker interface for output conditions in the Learning Sequence condition system.