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