‪TYPO3CMS  10.4
VariableValue.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 {
23 
27  private ‪$value;
28 
32  private ‪$variableNames;
33 
38 
43 
44  public static function ‪create(string ‪$value, ‪Variables ‪$defaultVariables = null): self
45  {
46  return new static(‪$value, ‪$defaultVariables);
47  }
48 
49  private function ‪__construct(string ‪$value, ‪Variables ‪$defaultVariables = null)
50  {
52  if (count(‪$variableNames) === 0) {
53  throw new \LogicException(
54  sprintf(
55  'Payload did not contain any variables "%s"',
56  ‪$value
57  ),
58  1577789315
59  );
60  }
61 
62  $this->value = ‪$value;
63  $this->variableNames = ‪$variableNames;
64  $this->defaultVariables = ‪$defaultVariables;
65  }
66 
67  public function ‪apply(‪Variables $variables): string
68  {
69  $variables = $variables->‪withDefined($this->defaultVariables);
70 
71  $this->‪assertVariableNames($variables);
72  if (!$this->‪hasAllRequiredDefinedVariableNames($variables)) {
73  return '';
74  }
75 
76  return str_replace(
77  array_map([$this, 'wrap'], $variables->‪keys()),
78  $variables->‪values(),
79  $this->value
80  );
81  }
82 
83  private function ‪assertVariableNames(‪Variables $variables): void
84  {
85  $missingVariableNames = array_diff($this->variableNames, $variables->‪keys());
86  if (!empty($missingVariableNames)) {
87  throw new \LogicException(
88  sprintf(
89  'Missing variable names "%s" for "%s"',
90  implode(',', $missingVariableNames),
91  $this->value
92  ),
93  1577789316
94  );
95  }
96  }
97 
98  private function ‪extractVariableNames(string ‪$value): array
99  {
100  if (!preg_match_all('#\[\[(?P<variableName>[^\[\]]+)\]\]#', ‪$value, $matches)) {
101  return [];
102  }
103  return array_unique($matches['variableName']);
104  }
105 
106  private function ‪wrap(string $item): string
107  {
108  return '[[' . $item . ']]';
109  }
110 }
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables\withDefined
‪withDefined(?Variables $other)
Definition: Variables.php:55
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\$variableNames
‪string[] $variableNames
Definition: VariableValue.php:29
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables
Definition: Variables.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables\values
‪values()
Definition: Variables.php:32
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariablesTrait
Definition: VariablesTrait.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\wrap
‪wrap(string $item)
Definition: VariableValue.php:101
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\create
‪static create(string $value, Variables $defaultVariables=null)
Definition: VariableValue.php:39
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables\keys
‪keys()
Definition: Variables.php:27
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\$defaultVariables
‪Variables $defaultVariables
Definition: VariableValue.php:33
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\$value
‪string $value
Definition: VariableValue.php:25
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\__construct
‪__construct(string $value, Variables $defaultVariables=null)
Definition: VariableValue.php:44
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\apply
‪apply(Variables $variables)
Definition: VariableValue.php:62
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\assertVariableNames
‪assertVariableNames(Variables $variables)
Definition: VariableValue.php:78
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder
Definition: Applicable.php:18
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\$requiredDefinedVariableNames
‪string[] $requiredDefinedVariableNames
Definition: VariableValue.php:37
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue\extractVariableNames
‪extractVariableNames(string $value)
Definition: VariableValue.php:93
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue
Definition: VariableValue.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariablesTrait\hasAllRequiredDefinedVariableNames
‪hasAllRequiredDefinedVariableNames(Variables $variables)
Definition: VariablesTrait.php:29