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