ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCtrlStructureMapper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /* Copyright (c) 2021 Thibeau Fuhrer <thf@studer-raimann.ch> Extended GPL, see docs/LICENSE */
6 
13 {
17  private array $ctrl_structure;
18 
24  public function __construct(array $ctrl_structure)
25  {
26  $this->ctrl_structure = $ctrl_structure;
27  $this->mapStructure();
28  }
29 
36  public function getStructure(): array
37  {
38  return $this->ctrl_structure;
39  }
40 
50  private function addViseVersaMappingByClass(string $class_name, string $key_ref_from, string $key_ref_to): void
51  {
52  if (!empty($this->ctrl_structure[$class_name][$key_ref_from])) {
53  foreach ($this->ctrl_structure[$class_name][$key_ref_from] as $index => $reference) {
54  $is_reference_available = isset($this->ctrl_structure[$reference]);
55  $is_reference_valid = $this->isStructureEntryValid($reference);
56 
57  // the vise-versa mapping must only be processed if the
58  // reference is available and a valid structure entry.
59  if ($is_reference_available && $is_reference_valid) {
60  // create reference list if not yet initialized.
61  if (!isset($this->ctrl_structure[$reference][$key_ref_to])) {
62  $this->ctrl_structure[$reference][$key_ref_to] = [];
63  }
64 
65  // only add vise-versa mapping if it doesn't already exist.
66  if (!in_array($class_name, $this->ctrl_structure[$reference][$key_ref_to], true)) {
67  $this->ctrl_structure[$reference][$key_ref_to][] = $class_name;
68  }
69  }
70 
71  // if the referenced class does not exist within the current
72  // structure, the reference is removed from the reference list.
73  if (!$is_reference_available || !$is_reference_valid) {
74  $this->removeReference($this->ctrl_structure[$class_name][$key_ref_from], $index);
75  }
76  }
77  }
78  }
79 
83  private function mapStructure(): void
84  {
85  if (!empty($this->ctrl_structure)) {
86  foreach ($this->ctrl_structure as $class_name => $data) {
87  if ($this->isStructureEntryValid($class_name)) {
89  $class_name,
92  );
93 
95  $class_name,
98  );
99  } else {
100  // remove/unset invalid structure entries.
101  unset($this->ctrl_structure[$class_name]);
102  }
103  }
104  }
105  }
106 
114  private function removeReference(array &$reference_list, $index): void
115  {
116  // remove the reference of the current index.
117  unset($reference_list[$index]);
118 
119  // re-index the reference list.
120  $reference_list = array_values($reference_list);
121  }
122 
130  private function isStructureEntryValid($index): bool
131  {
132  // structure entry is not a classname.
133  if (!is_string($index)) {
134  return false;
135  }
136 
137  // index is not contained in the structure.
138  if (!isset($this->ctrl_structure[$index])) {
139  return false;
140  }
141 
142  // structure value is not an array
143  if (!is_array($this->ctrl_structure[$index])) {
144  return false;
145  }
146 
147  return true;
148  }
149 }
mapStructure()
Maps the current structures references.
Class ilCtrlStructureMapper.
$index
Definition: metadata.php:145
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...
isStructureEntryValid($index)
Helper function that returns whether an entry in the current structure is valid or not...
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