ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Zipper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23class Zipper
24{
28 protected array $path = [];
29
30
31 public function __construct(Node $focus)
32 {
33 $this->path[] = $focus;
34 }
35
36 public function toChild(string $id): Zipper
37 {
38 $clone = clone $this;
39 $clone->path[] = end($this->path)->getSubnode($id);
40 return $clone;
41 }
42
43 public function isTop(): bool
44 {
45 return count($this->path) == 1;
46 }
47
48 public function toParent(): Zipper
49 {
50 $clone = clone $this;
51 $last_node = array_pop($clone->path);
52 $parent = array_pop($clone->path);
53 $clone->path[] = $parent->withSubnode($last_node);
54
55 return $clone;
56 }
57
58 public function toPath(array $hops): Zipper
59 {
60 $zipper = $this;
61 foreach ($hops as $hop) {
62 if ($hop != end($this->path)->getId()) {
63 $zipper = $zipper->toChild($hop);
64 }
65 }
66 return $zipper;
67 }
68
69 public function getRoot(): Node
70 {
71 if (count($this->path) == 1) {
72 return array_pop($this->path);
73 } else {
74 return $this->toParent()->getRoot();
75 }
76 }
77
78 public function modifyFocus(callable $f): Zipper
79 {
80 $clone = clone $this;
81 $focus = array_pop($clone->path);
82 $new_focus = $f($focus);
83 $clone->path[] = $new_focus;
84 return $clone;
85 }
86
87 public function modifyAll(callable $f, ?Zipper $zipper = null): Zipper
88 {
89 $zipper = $zipper ?? $this;
90 $zipper = $zipper->modifyFocus($f);
91 foreach (end($zipper->path)->getSubnodes() as $subnode) {
92 $zipper = $zipper
93 ->modifyAll($f, $zipper->toChild($subnode->getId()))
94 ->toParent();
95 }
96 return $zipper;
97 }
98}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
modifyAll(callable $f, ?Zipper $zipper=null)
Definition: Zipper.php:87