ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Handler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Export\ImportHandler\I\Path\HandlerInterface as ParserPathHandlerInterface;
25
26class Handler implements ParserPathHandlerInterface
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): ParserPathHandlerInterface
43 {
44 $clone = clone $this;
45 $clone->with_start_at_root_enabled = $enabled;
46 return $clone;
47 }
48
49 public function withNode(FilePathNodeInterface $node): ParserPathHandlerInterface
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): ParserPathHandlerInterface
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(): FilePathNodeInterface|null
88 {
89 return $this->count() > 0
90 ? $this->nodes[0]
91 : null;
92 }
93
94 public function lastElement(): FilePathNodeInterface|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(): FilePathNodeInterface
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 isset($this->nodes[$this->index]);
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}
withNode(FilePathNodeInterface $node)
Definition: Handler.php:49
subPath(int $start, ?int $end=null)
Definition: Handler.php:78