aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/TreeBuilder/Node/AbstractNode.php
blob: 0897ec71c5bced81de4c44bd6e87082693649c25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php

namespace dokuwiki\TreeBuilder\Node;

/**
 * A node represents one entry in the tree. It can have a parent and children.
 */
abstract class AbstractNode
{
    /** @var AbstractNode|null parent node */
    protected ?AbstractNode $parent = null;
    /** @var string unique ID for this node, usually the page id or external URL */
    protected string $id = '';
    /** @var string|null title of the node, may be null */
    protected ?string $title = null;

    /** @var AbstractNode[] */
    protected array $parents = [];
    /** @var AbstractNode[] */
    protected array $children = [];
    /** @var array */
    protected array $properties = [];

    /**
     * @param string $id The pageID or the external URL
     * @param string|null $title The title as given in the link
     */
    public function __construct(string $id, ?string $title)
    {
        $this->id = $id;
        $this->title = $title;
    }

    /**
     * @return string
     */
    public function getId(): string
    {
        return $this->id;
    }

    /**
     * Get the namespace of this node
     *
     * @return string
     */
    public function getNs(): string
    {
        return getNS($this->id);
    }

    /**
     * @return string|null
     */
    public function getTitle(): ?string
    {
        return $this->title;
    }

    /**
     * @param string|null $title
     */
    public function setTitle(?string $title): void
    {
        $this->title = $title;
    }

    /**
     * Get all nodes on the same level
     * @return AbstractNode[]
     */
    public function getSiblings(): array
    {
        return $this->getParent()->getChildren();
    }

    /**
     * Get all sub nodes, may return an empty array
     *
     * @return AbstractNode[]
     */
    public function getChildren(): array
    {
        return $this->children;
    }

    /**
     * Does this node have children?
     *
     * @return bool
     */
    public function hasChildren(): bool
    {
        return !empty($this->children);
    }

    /**
     * Get all sub nodes and their sub nodes and so on
     *
     * @return AbstractNode[]
     */
    public function getDescendants(): array
    {
        $descendants = [];
        foreach ($this->children as $child) {
            $descendants[] = $child;
            $descendants = array_merge($descendants, $child->getDescendants());
        }
        return $descendants;
    }

    /**
     * Get all parent nodes in reverse order
     *
     * This list is cached, so it will only be calculated once.
     *
     * @return AbstractNode[]
     */
    public function getParents(): array
    {
        if (!$this->parents) {
            $parent = $this->parent;
            while ($parent) {
                $this->parents[] = $parent;
                $parent = $parent->getParent();
            }
        }

        return $this->parents;
    }

    /**
     * Set the direct parent node
     */
    public function setParent(AbstractNode $parent): void
    {
        $this->parent = $parent;
    }

    /**
     * Get the direct parent node
     *
     * @return AbstractNode|null
     */
    public function getParent(): ?AbstractNode
    {
        return $this->parent;
    }

    /**
     * @param AbstractNode $child
     * @return void
     */
    public function addChild(AbstractNode $child): void
    {
        $child->setParent($this);
        $this->children[] = $child;
    }

    /**
     * Allows to attach an arbitrary property to the page
     *
     * @param string $name
     * @param mixed $value
     * @return void
     */
    public function setProperty(string $name, $value): void
    {
        $this->properties[$name] = $value;
    }

    /**
     * Get the named property, default is returned when the property is not set
     *
     * @param string $name
     * @param mixed $default
     * @return mixed
     */
    public function getProperty(string $name, $default = null)
    {
        if (isset($this->properties[$name])) {
            return $this->properties[$name];
        }
        return $default;
    }

    /**
     * Sort the children of this node and all its descendants
     *
     * The given comparator function will be called with two nodes as arguments and needs to
     * return an integer less than, equal to, or greater than zero if the first argument is considered
     * to be respectively less than, equal to, or greater than the second.
     *
     * @param callable $comparator
     * @return void
     */
    public function sort(callable $comparator): void
    {
        usort($this->children, $comparator);
        foreach ($this->children as $child) {
            $child->sort($comparator);
        }
    }

    /**
     * Get the string representation of the node
     *
     * Uses plus char to show the depth of the node in the tree
     *
     * @return string
     */
    public function __toString(): string
    {
        return str_pad('', count($this->getParents()), '+') . $this->id;
    }
}