ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
class.ilCtrlStructureMapper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
29 private array $ctrl_structure;
30
36 public function __construct(array $ctrl_structure)
37 {
38 $this->ctrl_structure = $ctrl_structure;
39 $this->mapStructure();
40 }
41
48 public function getStructure(): array
49 {
51 }
52
62 private function addViseVersaMappingByClass(string $class_name, string $key_ref_from, string $key_ref_to): void
63 {
64 if (!empty($this->ctrl_structure[$class_name][$key_ref_from])) {
65 $invalid_indices = [];
66
67 foreach ($this->ctrl_structure[$class_name][$key_ref_from] as $index => $reference) {
68 $is_reference_available = isset($this->ctrl_structure[$reference]);
69 $is_reference_valid = $this->isStructureEntryValid($reference);
70
71 // the vise-versa mapping must only be processed if the
72 // reference is available and a valid structure entry.
73 if ($is_reference_available && $is_reference_valid) {
74 // create reference list if not yet initialized.
75 if (!isset($this->ctrl_structure[$reference][$key_ref_to])) {
76 $this->ctrl_structure[$reference][$key_ref_to] = [];
77 }
78
79 // only add vise-versa mapping if it doesn't already exist.
80 if (!in_array($class_name, $this->ctrl_structure[$reference][$key_ref_to], true)) {
81 $this->ctrl_structure[$reference][$key_ref_to][] = $class_name;
82 }
83 }
84
85 // if the referenced class does not exist within the current
86 // structure, the reference is removed from the reference list.
87 if (!$is_reference_available || !$is_reference_valid) {
88 $invalid_indices[] = $index;
89 }
90 }
91
92 $this->removeInvalidReferences($invalid_indices, $class_name, $key_ref_from);
93 }
94 }
95
99 private function mapStructure(): void
100 {
101 if (!empty($this->ctrl_structure)) {
102 foreach ($this->ctrl_structure as $class_name => $data) {
103 if ($this->isStructureEntryValid($class_name)) {
105 $class_name,
108 );
109
111 $class_name,
114 );
115 } else {
116 // remove/unset invalid structure entries.
117 unset($this->ctrl_structure[$class_name]);
118 }
119 }
120 }
121 }
122
126 private function removeInvalidReferences(array $invalid_indices, string $class_name, string $key_ref_from): void
127 {
128 foreach ($invalid_indices as $index) {
129 unset($this->ctrl_structure[$class_name][$key_ref_from][$index]);
130 }
131
132 // Re-index the array once after all removals
133 if (!empty($invalid_indices)) {
134 $this->ctrl_structure[$class_name][$key_ref_from] = array_values(
135 $this->ctrl_structure[$class_name][$key_ref_from]
136 );
137 }
138 }
139
147 private function isStructureEntryValid($index): bool
148 {
149 // structure entry is not a classname.
150 if (!is_string($index)) {
151 return false;
152 }
153
154 // index is not contained in the structure.
155 if (!isset($this->ctrl_structure[$index])) {
156 return false;
157 }
158
159 // structure value is not an array
160 if (!is_array($this->ctrl_structure[$index])) {
161 return false;
162 }
163
164 return true;
165 }
166}
removeInvalidReferences(array $invalid_indices, string $class_name, string $key_ref_from)
isStructureEntryValid($index)
Helper function that returns whether an entry in the current structure is valid or not.
mapStructure()
Maps the current structures references.
addViseVersaMappingByClass(string $class_name, string $key_ref_from, string $key_ref_to)
If a class has referenced another one as child or parent, this method adds a vise-versa mapping if it...
__construct(array $ctrl_structure)
ilCtrlStructureMapper Constructor
getStructure()
Returns the current structure with mapped vise-versa references of each parent-child relation.
$index
Definition: metadata.php:145