‪TYPO3CMS  ‪main
AbstractChildNode.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 
27 {
28  public function ‪__construct(protected string ‪$name) {}
29 
34  public function ‪__clone(): void
35  {
36  foreach ($this->children as $childName => $child) {
37  $this->children[$childName] = clone $child;
38  }
39  }
40 
41  public function ‪getName(): string
42  {
43  return ‪$this->name;
44  }
45 
46  public function ‪toArray(): ?array
47  {
48  if (!$this->‪hasChildren()) {
49  return null;
50  }
51  $result = [];
52  foreach ($this->‪getNextChild() as $child) {
53  $childName = $child->getName();
54  $childValue = $child->getValue();
55  if ($child instanceof ‪ReferenceChildNode) {
56  // Hack for b/w compat parsing of `=<` operator. See ContentObjectRenderer cObjGetSingle() and mergeTSRef()
57  // @todo: adding the whitespace after '<' is another bit of a hack here ... maybe solve in tokenizer?
58  // compare this for what happens when doing 'foo = bar' in old parser: Is the whitespace kept for
59  // value to not trigger the ref lookup to often if doing 'foo = <div...' ?
60  // @todo: same situation in RootNode!
61  $childValue = '< ' . $child->getReferenceSourceStream();
62  }
63  if ($childValue !== null) {
64  $result[$child->getName()] = $childValue;
65  }
66  $grandChildren = $child->toArray();
67  if ($grandChildren !== null) {
68  $result[$childName . '.'] = $grandChildren;
69  }
70  }
71  return $result;
72  }
73 
74  public function ‪flatten(string $prefix = ''): array
75  {
76  $flatArray = [];
77  $prefixedQuotedNodeName = $prefix . addcslashes($this->‪getName(), '.');
78  if (!$this->‪isValueNull()) {
79  $flatArray[$prefixedQuotedNodeName] = $this->‪getValue();
80  }
81  foreach ($this->‪getNextChild() as $child) {
82  $flatArray = array_merge($flatArray, $child->flatten($prefixedQuotedNodeName . '.'));
83  }
84  return $flatArray;
85  }
86 }
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getNextChild
‪getNextChild()
Definition: AbstractNode.php:121
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractChildNode
Definition: AbstractChildNode.php:27
‪TYPO3\CMS\Core\TypoScript\AST\Node\ReferenceChildNode
Definition: ReferenceChildNode.php:39
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\getValue
‪getValue()
Definition: AbstractNode.php:143
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\hasChildren
‪hasChildren()
Definition: AbstractNode.php:116
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\$name
‪string $name
Definition: AbstractNode.php:31
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractChildNode\flatten
‪flatten(string $prefix='')
Definition: AbstractChildNode.php:74
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractChildNode\toArray
‪toArray()
Definition: AbstractChildNode.php:46
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractChildNode\getName
‪getName()
Definition: AbstractChildNode.php:41
‪TYPO3\CMS\Core\TypoScript\AST\Node\ChildNodeInterface
Definition: ChildNodeInterface.php:26
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractChildNode\__construct
‪__construct(protected string $name)
Definition: AbstractChildNode.php:28
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractChildNode\__clone
‪__clone()
Definition: AbstractChildNode.php:34
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode\isValueNull
‪isValueNull()
Definition: AbstractNode.php:148
‪TYPO3\CMS\Core\TypoScript\AST\Node\AbstractNode
Definition: AbstractNode.php:29
‪TYPO3\CMS\Core\TypoScript\AST\Node
Definition: AbstractChildNode.php:18