‪TYPO3CMS  ‪main
AbstractNode.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
28 abstract class ‪AbstractNode implements ‪NodeInterface
29 {
30  private ?string ‪$identifier = null;
31  protected string ‪$name;
32  private ?string ‪$value = null;
33  private ?string ‪$previousValue = null;
34 
38  protected array ‪$children = [];
40  private array ‪$comments = [];
41 
47  final public function ‪__serialize(): array
48  {
49  return $this->‪serialize();
50  }
51 
52  protected function ‪serialize(): array
53  {
54  $result = [
55  'name' => ‪$this->name,
56  'children' => ‪$this->children,
57  ];
58  if ($this->value !== null) {
59  $result['value'] = ‪$this->value;
60  }
61  return $result;
62  }
63 
64  public function ‪setIdentifier(string ‪$identifier): void
65  {
66  $this->identifier = hash('xxh3', ‪$identifier);
67  $childCounter = 0;
68  foreach ($this->‪getNextChild() as $child) {
69  $child->setIdentifier($this->identifier . $childCounter);
70  $childCounter++;
71  }
72  }
73 
81  public function ‪updateName(string ‪$name): void
82  {
83  $this->name = ‪$name;
84  }
85 
86  public function ‪getIdentifier(): string
87  {
88  if ($this->identifier === null) {
89  throw new \RuntimeException(
90  'Identifier has not been initialized. This happens when getIdentifier() is called on'
91  . ' trees retrieved from cache. The identifier is not supposed to be used in this context.',
92  1674620169
93  );
94  }
95  return ‪$this->identifier;
96  }
97 
98  public function ‪addChild(‪ChildNodeInterface $node): void
99  {
100  $this->children[$node->‪getName()] = $node;
101  }
102 
104  {
105  return $this->children[‪$name] ?? null;
106  }
107 
111  public function ‪removeChildByName(string ‪$name): void
112  {
113  unset($this->children[‪$name]);
114  }
115 
116  public function ‪hasChildren(): bool
117  {
118  return !empty($this->children);
119  }
120 
121  public function ‪getNextChild(): iterable
122  {
123  foreach ($this->children as $child) {
124  yield $child;
125  }
126  }
127 
128  public function ‪sortChildren(): void
129  {
130  ksort($this->children, SORT_FLAG_CASE | SORT_STRING);
131  }
132 
133  public function ‪setValue(?string ‪$value): void
134  {
135  $this->value = ‪$value;
136  }
137 
138  public function ‪appendValue(string ‪$value): void
139  {
140  $this->value .= ‪$value;
141  }
142 
143  public function ‪getValue(): ?string
144  {
145  return ‪$this->value;
146  }
147 
148  public function ‪isValueNull(): bool
149  {
150  return $this->value === null;
151  }
152 
153  public function ‪setPreviousValue(?string ‪$value): void
154  {
155  $this->previousValue = ‪$value;
156  }
157 
158  public function ‪getPreviousValue(): ?string
159  {
161  }
162 
163  public function ‪setOriginalValueTokenStream(?‪TokenStreamInterface $tokenStream): void
164  {
165  $this->originalValueTokenStream = $tokenStream;
166  }
167 
169  {
171  }
172 
173  public function ‪addComment(‪TokenStreamInterface $tokenStream): void
174  {
175  $this->comments[] = $tokenStream;
176  }
177 
181  public function ‪getComments(): array
182  {
183  return ‪$this->comments;
184  }
185 }
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getNextChild
‪getNextChild()
Definition: AbstractNode.php:121
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\$children
‪array $children
Definition: AbstractNode.php:38
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\setValue
‪setValue(?string $value)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getValue
‪getValue()
Definition: AbstractNode.php:143
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\$previousValue
‪string $previousValue
Definition: AbstractNode.php:33
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\hasChildren
‪hasChildren()
Definition: AbstractNode.php:116
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\$originalValueTokenStream
‪TokenStreamInterface $originalValueTokenStream
Definition: AbstractNode.php:39
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\$name
‪string $name
Definition: AbstractNode.php:31
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\sortChildren
‪sortChildren()
Definition: AbstractNode.php:128
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\$value
‪string $value
Definition: AbstractNode.php:32
‪TYPO3\CMS\Core\TypoScript\AST\Node\NodeInterface
Definition: NodeInterface.php:35
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\removeChildByName
‪removeChildByName(string $name)
Definition: AbstractNode.php:111
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getChildByName
‪getChildByName(string $name)
Definition: AbstractNode.php:103
‪TYPO3\CMS\Core\TypoScript\AST\Node\ChildNodeInterface
Definition: ChildNodeInterface.php:26
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\$identifier
‪string $identifier
Definition: AbstractNode.php:30
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\__serialize
‪__serialize()
Definition: AbstractNode.php:47
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\serialize
‪serialize()
Definition: AbstractNode.php:52
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getPreviousValue
‪getPreviousValue()
Definition: AbstractNode.php:158
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\isValueNull
‪isValueNull()
Definition: AbstractNode.php:148
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Token\TokenStreamInterface
Definition: TokenStreamInterface.php:30
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\appendValue
‪appendValue(string $value)
Definition: AbstractNode.php:138
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\setOriginalValueTokenStream
‪setOriginalValueTokenStream(?TokenStreamInterface $tokenStream)
Definition: AbstractNode.php:163
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\setPreviousValue
‪setPreviousValue(?string $value)
Definition: AbstractNode.php:153
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\setIdentifier
‪setIdentifier(string $identifier)
Definition: AbstractNode.php:64
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode
Definition: AbstractNode.php:29
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getIdentifier
‪getIdentifier()
Definition: AbstractNode.php:86
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getComments
‪TokenStreamInterface[] getComments()
Definition: AbstractNode.php:181
‪TYPO3\CMS\Core\TypoScript\AST\Node\NodeInterface\getName
‪getName()
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\addComment
‪addComment(TokenStreamInterface $tokenStream)
Definition: AbstractNode.php:173
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getOriginalValueTokenStream
‪getOriginalValueTokenStream()
Definition: AbstractNode.php:168
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\addChild
‪addChild(ChildNodeInterface $node)
Definition: AbstractNode.php:98
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\$comments
‪array $comments
Definition: AbstractNode.php:40
‪TYPO3\CMS\Core\TypoScript\AST\Node
Definition: AbstractChildNode.php:18
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\updateName
‪updateName(string $name)
Definition: AbstractNode.php:81