ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCtrlArrayClassPath.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 {
18 
26  public function __construct(ilCtrlStructureInterface $structure, ilCtrlContextInterface $context, array $target_classes)
27  {
28  parent::__construct($structure);
29 
30  $this->context = $context;
31 
32  try {
33  $this->cid_path = $this->getCidPathByArray($target_classes);
34  } catch (ilCtrlException $exception) {
35  $this->exception = $exception;
36  }
37  }
38 
50  private function getCidPathByArray(array $target_classes): string
51  {
52  // abort if the provided targets are empty.
53  if (empty($target_classes)) {
54  throw new ilCtrlException(__METHOD__ . " must be provided with a list of classes.");
55  }
56 
57  // loop through each provided class in descending order
58  // and check if they are all related to one another and
59  // convert them to a cid path.
60  $cid_path = $previous_class = null;
61  foreach ($target_classes as $current_class) {
62  $current_cid = $this->structure->getClassCidByName($current_class);
63 
64  // abort if the current class cannot be found.
65  if (null === $current_cid) {
66  throw new ilCtrlException("Class '$current_class' was not found in the control structure, try `composer du` to read artifacts.");
67  }
68 
69  // abort if the current and previous classes are
70  // not related.
71  if (null !== $previous_class && !$this->isClassParentOf($previous_class, $current_class)) {
72  throw new ilCtrlException("Class '$current_class' is not a child of '$previous_class'.");
73  }
74 
75  $cid_path = $this->appendCid($current_cid, $cid_path);
76  $previous_class = $current_class;
77  }
78 
79  // if the first provided class is a baseclass the
80  // created cid path can be returned.
81  $first_array_class = $target_classes[array_key_first($target_classes)];
82  if ($this->structure->isBaseClass($first_array_class)) {
83  return $cid_path;
84  }
85 
86  // check if the first command class is related to one
87  // of the current context.
88  $related_class_path = $this->getPathToRelatedClassInContext($this->context, $first_array_class);
89  if (null === $related_class_path) {
90  throw new ilCtrlException("Class '$first_array_class' is not a baseclass and the current context doesn't have one either.");
91  }
92 
93  return $this->appendCid($cid_path, $related_class_path);
94  }
95 }
Class ilCtrlArrayClassPath.
Class ilCtrlAbstractPath.
isClassParentOf(string $parent_class, string $child_class)
Returns whether the given target class is a parent of the other given class.
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilCtrlStructureInterface $structure, ilCtrlContextInterface $context, array $target_classes)
ilCtrlArrayClassPath Constructor
appendCid(string $cid, string $path=null)
Helper function to add CIDs to a given path.
ilCtrl exceptions
__construct(Container $dic, ilPlugin $plugin)
ilCtrlContextInterface $context
getCidPathByArray(array $target_classes)
Generates a cid path from the given class array.
ilCtrlStructureInterface $structure