ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCtrlStructureMapper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
31 private array $ctrl_structure;
32
38 public function __construct(array $ctrl_structure)
39 {
40 $this->ctrl_structure = $ctrl_structure;
41 $this->mapStructure();
42 }
43
50 public function getStructure(): array
51 {
53 }
54
64 private function addViseVersaMappingByClass(string $class_name, string $key_ref_from, string $key_ref_to): void
65 {
66 if (!empty($this->ctrl_structure[$class_name][$key_ref_from])) {
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 $this->removeReference($this->ctrl_structure[$class_name][$key_ref_from], $index);
89 }
90 }
91 }
92 }
93
97 private function mapStructure(): void
98 {
99 if (!empty($this->ctrl_structure)) {
100 foreach ($this->ctrl_structure as $class_name => $data) {
101 if ($this->isStructureEntryValid($class_name)) {
103 $class_name,
106 );
107
109 $class_name,
112 );
113 } else {
114 // remove/unset invalid structure entries.
115 unset($this->ctrl_structure[$class_name]);
116 }
117 }
118 }
119 }
120
128 private function removeReference(array &$reference_list, $index): void
129 {
130 // remove the reference of the current index.
131 unset($reference_list[$index]);
132
133 // re-index the reference list.
134 $reference_list = array_values($reference_list);
135 }
136
144 private function isStructureEntryValid($index): bool
145 {
146 // structure entry is not a classname.
147 if (!is_string($index)) {
148 return false;
149 }
150
151 // index is not contained in the structure.
152 if (!isset($this->ctrl_structure[$index])) {
153 return false;
154 }
155
156 // structure value is not an array
157 if (!is_array($this->ctrl_structure[$index])) {
158 return false;
159 }
160
161 return true;
162 }
163}
Class ilCtrlStructureMapper.
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.
removeReference(array &$reference_list, $index)
Removes an entry within the given reference list for the given index and re-indexes the reference lis...