ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilHandler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Export\ImportHandler\I\File\Path\ilHandlerInterface as ilParserPathHandlerInterface;
25 
26 class ilHandler implements ilParserPathHandlerInterface
27 {
31  protected array $nodes;
32  protected int $index;
34 
35  public function __construct()
36  {
37  $this->nodes = [];
38  $this->index = 0;
39  $this->with_start_at_root_enabled = false;
40  }
41 
42  public function withStartAtRoot(bool $enabled): ilParserPathHandlerInterface
43  {
44  $clone = clone $this;
45  $clone->with_start_at_root_enabled = $enabled;
46  return $clone;
47  }
48 
49  public function withNode(ilFilePathNodeInterface $node): ilParserPathHandlerInterface
50  {
51  $clone = clone $this;
52  $clone->nodes[] = $node;
53  return $clone;
54  }
55 
56  public function toString(): string
57  {
58  $first_separator = true;
59  $path_str = '';
60  for ($i = 0; $i < count($this->nodes); $i++) {
61  $node = $this->nodes[$i];
62  if (
63  ($node->requiresPathSeparator() && $first_separator && $this->with_start_at_root_enabled) ||
64  ($node->requiresPathSeparator() && !$first_separator)
65  ) {
66  $path_str .= '/';
67  $first_separator = false;
68  }
69  if ($node->requiresPathSeparator() && $first_separator && !$this->with_start_at_root_enabled) {
70  $path_str .= '//';
71  $first_separator = false;
72  }
73  $path_str .= $node->toString();
74  }
75  return $path_str;
76  }
77 
78  public function subPath(int $start, ?int $end = null): ilParserPathHandlerInterface
79  {
80  $clone = clone $this;
81  $clone->nodes = is_null($end)
82  ? array_slice($this->nodes, $start)
83  : array_slice($this->nodes, $start, $end - $start);
84  return $clone;
85  }
86 
87  public function firstElement(): ilFilePathNodeInterface|null
88  {
89  return $this->count() > 0
90  ? $this->nodes[0]
91  : null;
92  }
93 
94  public function lastElement(): ilFilePathNodeInterface|null
95  {
96  return $this->count() > 0
97  ? $this->nodes[$this->count() - 1]
98  : null;
99  }
100 
104  public function toArray(): array
105  {
106  return $this->nodes;
107  }
108 
109  public function current(): ilFilePathNodeInterface
110  {
111  return $this->nodes[$this->index];
112  }
113 
114  public function next(): void
115  {
116  $this->index++;
117  }
118 
119  public function key(): int
120  {
121  return $this->index;
122  }
123 
124  public function valid(): bool
125  {
126  return 0 <= $this->index && $this->index < $this->count();
127  }
128 
129  public function rewind(): void
130  {
131  $this->index = 0;
132  }
133 
134  public function count(): int
135  {
136  return count($this->nodes);
137  }
138 }
bool $enabled
Whether the system instance is enabled to accept connection requests.
Definition: System.php:123