ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
Path.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Paths;
22 
25 
26 class Path implements PathInterface, \Stringable
27 {
31  protected array $steps;
32  protected bool $is_relative;
33  protected bool $leads_to_one;
34 
35  protected string $as_string;
36 
37  public function __construct(
38  bool $is_relative,
39  bool $leads_to_one,
40  StepInterface ...$steps
41  ) {
42  $this->is_relative = $is_relative;
43  $this->leads_to_one = $leads_to_one;
44  $this->steps = $steps;
45  }
46 
50  public function steps(): \Generator
51  {
52  yield from $this->steps;
53  }
54 
55  public function isRelative(): bool
56  {
57  return $this->is_relative;
58  }
59 
60  public function leadsToExactlyOneElement(): bool
61  {
62  return $this->leads_to_one;
63  }
64 
65  public function toString(): string
66  {
67  if (isset($this->as_string)) {
68  return $this->as_string;
69  }
70 
71  $string = '';
72 
73  if ($this->leadsToExactlyOneElement()) {
74  $string .= Token::LEADS_TO_EXACTLY_ONE->value;
75  }
76  if ($this->isRelative()) {
77  $string .= Token::START_AT_CURRENT->value;
78  } else {
79  $string .= Token::START_AT_ROOT->value;
80  }
81  foreach ($this->steps() as $step) {
82  $string .= Token::SEPARATOR->value;
83  $string .= $this->stepToString($step);
84  }
85 
86  return $this->as_string = $string;
87  }
88 
89  protected function stepToString(StepInterface $step): string
90  {
91  $string = $step->name();
92  if ($string instanceof StepToken) {
93  $string = $string->value;
94  }
95  foreach ($step->filters() as $filter) {
96  $string .= Token::FILTER_SEPARATOR->value .
97  $filter->type()->value .
99 
100  $string .= implode(
102  iterator_to_array($filter->values())
103  );
104  }
105  return $string;
106  }
107 
108  public function __toString(): string
109  {
110  return $this->toString();
111  }
112 }
filters()
Filters restrict the elements a step leads to.
__construct(bool $is_relative, bool $leads_to_one, StepInterface ... $steps)
Definition: Path.php:37
leadsToExactlyOneElement()
Specifies whether the path should point to exactly one element, or whether it can also lead to no or ...
Definition: Path.php:60
name()
Steps are identified by the names of LOM elements, or a token to specify a step to the super-element...
StepToken
The string representation of these tokens must not occur as names of metadata elements.
Definition: StepToken.php:27
isRelative()
Relative paths start at some otherwise determined element, absolute paths start at root...
Definition: Path.php:55
stepToString(StepInterface $step)
Definition: Path.php:89