‪TYPO3CMS  9.5
Variable.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 {
20  public const ‪CAST_NONE = 0;
21  public const ‪CAST_STRING = 1;
22  public const ‪CAST_INT = 2;
23  public const ‪CAST_FLOAT = 3;
24 
28  private ‪$variableName;
29 
33  private ‪$cast;
34 
35  public static function ‪create(string ‪$variableName, int ‪$cast = self::CAST_NONE): self
36  {
37  return new static(‪$variableName, ‪$cast);
38  }
39 
40  private function ‪__construct(string ‪$variableName, int ‪$cast = self::CAST_NONE)
41  {
42  $this->variableName = ‪$variableName;
43  $this->‪cast = ‪$cast;
44  }
45 
46  public function ‪apply(‪Variables $variables)
47  {
48  if (!isset($variables[$this->variableName])) {
49  throw new \LogicException(
50  sprintf(
51  'Missing variable name "%s"',
52  $this->variableName
53  ),
54  1577789317
55  );
56  }
57  return $this->‪cast($variables[$this->variableName]);
58  }
59 
60  private function ‪cast($value)
61  {
62  switch ($this->‪cast) {
63  case ‪self::CAST_NONE:
64  return $value;
66  return (string)$value;
67  case ‪self::CAST_INT:
68  return (int)$value;
70  return (float)$value;
71  }
72  return $value;
73  }
74 }
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\__construct
‪__construct(string $variableName, int $cast=self::CAST_NONE)
Definition: Variable.php:38
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable
Definition: Variable.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\CAST_FLOAT
‪const CAST_FLOAT
Definition: Variable.php:23
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\CAST_NONE
‪const CAST_NONE
Definition: Variable.php:20
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\$cast
‪int $cast
Definition: Variable.php:31
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\apply
‪apply(Variables $variables)
Definition: Variable.php:44
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables
Definition: Variables.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\CAST_INT
‪const CAST_INT
Definition: Variable.php:22
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder
Definition: Applicable.php:3
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\cast
‪cast($value)
Definition: Variable.php:58
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\$variableName
‪string $variableName
Definition: Variable.php:27
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\create
‪static create(string $variableName, int $cast=self::CAST_NONE)
Definition: Variable.php:33
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variable\CAST_STRING
‪const CAST_STRING
Definition: Variable.php:21