ILIAS  release_8 Revision v8.24
class.ilCtrlAbstractPath.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/* Copyright (c) 2021 Thibeau Fuhrer <thf@studer-raimann.ch> Extended GPL, see docs/LICENSE */
6
12abstract class ilCtrlAbstractPath implements ilCtrlPathInterface
13{
18
22 protected ?ilCtrlException $exception = null;
23
27 protected ?string $cid_path = null;
28
35 {
36 $this->structure = $structure;
37 }
38
42 public function getCidPath(): ?string
43 {
44 // cannot use empty(), since '0' would be considered
45 // empty and that's an actual cid.
46 if (null !== $this->cid_path && '' !== $this->cid_path) {
47 return $this->cid_path;
48 }
49
50 return null;
51 }
52
56 public function getCurrentCid(): ?string
57 {
58 if (null !== $this->getCidPath()) {
59 // use default order (command- to baseclass) and
60 // retrieve the last command class (index 0).
61 return $this->getCidArray()[0];
62 }
63
64 return null;
65 }
66
70 public function getNextCid(string $current_class): ?string
71 {
72 $current_cid = $this->structure->getClassCidByName($current_class);
73 $cid_array = $this->getCidArray(SORT_ASC);
74 $cid_count = count($cid_array);
75
76 foreach ($cid_array as $index => $cid) {
77 if ($current_cid === $cid && ($index + 1) < $cid_count) {
78 return $cid_array[$index + 1];
79 }
80 }
81
82 return null;
83 }
84
88 public function getCidPaths(int $order = SORT_DESC): array
89 {
90 if (null === $this->getCidPath()) {
91 return [];
92 }
93
94 // cid array must be ascending, because the
95 // paths should always begin at the baseclass.
96 $cid_array = $this->getCidArray(SORT_ASC);
97 $cid_paths = [];
98
99 foreach ($cid_array as $index => $cid) {
100 $cid_paths[] = (0 !== $index) ?
101 $this->appendCid($cid, $cid_paths[$index - 1]) :
102 $cid
103 ;
104 }
105
106 if (SORT_DESC === $order) {
107 $cid_paths = array_reverse($cid_paths);
108 }
109
110 return $cid_paths;
111 }
112
116 public function getCidArray(int $order = SORT_DESC): array
117 {
118 if (null === $this->getCidPath()) {
119 return [];
120 }
121
122 $cid_array = explode(self::CID_PATH_SEPARATOR, $this->cid_path);
123 if (SORT_DESC === $order) {
124 $cid_array = array_reverse($cid_array);
125 }
126
127 return $cid_array;
128 }
129
133 public function getBaseClass(): ?string
134 {
135 if (null !== $this->cid_path) {
136 $cid_array = $this->getCidArray(SORT_ASC);
137 $class_name = $this->structure->getClassNameByCid($cid_array[0]);
138 if (null !== $class_name && $this->structure->isBaseClass($class_name)) {
139 return $class_name;
140 }
141 }
142
143 return null;
144 }
145
149 public function getException(): ?ilCtrlException
150 {
151 return $this->exception;
152 }
153
162 protected function getPathToRelatedClassInContext(ilCtrlContextInterface $context, string $target_class): ?string
163 {
164 if (null !== $context->getPath()->getCidPath()) {
165 foreach ($context->getPath()->getCidArray() as $index => $cid) {
166 $current_class = $this->structure->getClassNameByCid($cid);
167 if (null !== $current_class && $this->isClassChildOf($target_class, $current_class)) {
168 $cid_paths = $context->getPath()->getCidPaths();
169
170 // return the path to the class related to the
171 // target class.
172 return $cid_paths[$index];
173 }
174 }
175 }
176
177 return null;
178 }
179
188 protected function isClassChildOf(string $child_class, string $parent_class): bool
189 {
190 $children = $this->structure->getChildrenByName($parent_class);
191 if (null !== $children) {
192 return in_array(strtolower($child_class), $children, true);
193 }
194
195 return false;
196 }
197
206 protected function isClassParentOf(string $parent_class, string $child_class): bool
207 {
208 $parents = $this->structure->getParentsByName($child_class);
209 if (null !== $parents) {
210 return in_array(strtolower($parent_class), $parents, true);
211 }
212
213 return false;
214 }
215
223 protected function appendCid(string $cid, string $path = null): string
224 {
225 if (null === $path) {
226 return $cid;
227 }
228
229 return $path . self::CID_PATH_SEPARATOR . $cid;
230 }
231}
Class ilCtrlAbstractPath.
getPathToRelatedClassInContext(ilCtrlContextInterface $context, string $target_class)
Returns the path to a class within the given contexts current path that has a relation to the given t...
ilCtrlStructureInterface $structure
appendCid(string $cid, string $path=null)
Helper function to add CIDs to a given path.
isClassParentOf(string $parent_class, string $child_class)
Returns whether the given target class is a parent of the other given class.
getCidArray(int $order=SORT_DESC)
@inheritDoc
getNextCid(string $current_class)
@inheritDoc
__construct(ilCtrlStructureInterface $structure)
ilCtrlAbstractPath Constructor
isClassChildOf(string $child_class, string $parent_class)
Returns whether the given target class is a child of the other given class.
getCidPaths(int $order=SORT_DESC)
@inheritDoc
ilCtrl exceptions
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilCtrlPathInterface is responsible for holding and manipulating a valid ilCtrl class-path (...
$path
Definition: ltiservices.php:32
$index
Definition: metadata.php:145
$context
Definition: webdav.php:29