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