‪TYPO3CMS  ‪main
Enumeration.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Type;
17 
20 
30 abstract class ‪Enumeration implements ‪TypeInterface
31 {
35  protected ‪$value;
36 
40  protected static ‪$enumConstants;
41 
46  public function ‪__construct(‪$value = null)
47  {
48  trigger_error('Class ' . __CLASS__ . ' used by enumeration ' . static::class . ' will be removed in TYPO3 v14.0. Use native enums instead.', E_USER_DEPRECATED);
49 
50  if (‪$value === null && !defined('static::__default')) {
52  sprintf('A value for enumeration "%s" is required if no __default is defined.', static::class),
53  1381512753
54  );
55  }
56  if (‪$value === null) {
57  ‪$value = static::__default;
58  }
59  static::loadValues();
60  if (!$this->‪isValid(‪$value)) {
62  sprintf('Invalid value "%s" for enumeration "%s"', ‪$value, static::class),
63  1381512761
64  );
65  }
66  $this->‪setValue(‪$value);
67  }
68 
74  protected static function ‪loadValues()
75  {
76  $class = static::class;
77 
78  if (isset(static::$enumConstants[$class])) {
79  return;
80  }
81 
82  $reflection = new \ReflectionClass($class);
83  $constants = $reflection->getConstants();
84  $defaultValue = null;
85  if (isset($constants['__default'])) {
86  $defaultValue = $constants['__default'];
87  unset($constants['__default']);
88  }
89  if (empty($constants)) {
91  sprintf(
92  'No constants defined in enumeration "%s"',
93  $class
94  ),
95  1381512807
96  );
97  }
98  foreach ($constants as $constant => ‪$value) {
99  if (!is_int(‪$value) && !is_string(‪$value)) {
101  sprintf(
102  'Constant value "%s" of enumeration "%s" must be of type integer or string, got "%s" instead',
103  $constant,
104  $class,
105  get_debug_type(‪$value)
106  ),
107  1381512797
108  );
109  }
110  }
111  $constantValueCounts = array_count_values($constants);
112  arsort($constantValueCounts, SORT_NUMERIC);
113  $constantValueCount = current($constantValueCounts);
114  $constant = key($constantValueCounts);
115  if ($constantValueCount > 1) {
117  sprintf(
118  'Constant value "%s" of enumeration "%s" is not unique (defined %d times)',
119  $constant,
120  $class,
121  $constantValueCount
122  ),
123  1381512859
124  );
125  }
126  if ($defaultValue !== null) {
127  $constants['__default'] = $defaultValue;
128  }
129  static::$enumConstants[$class] = $constants;
130  }
131 
139  protected function ‪setValue(‪$value)
140  {
141  $enumKey = array_search((string)‪$value, static::$enumConstants[static::class]);
142  if ($enumKey === false) {
144  sprintf('Invalid value "%s" for enumeration "%s"', ‪$value, __CLASS__),
145  1381615295
146  );
147  }
148  $this->value = static::$enumConstants[static::class][$enumKey];
149  }
150 
157  protected function ‪isValid(‪$value)
158  {
159  ‪$value = (string)‪$value;
160  foreach (static::$enumConstants[static::class] as $constantValue) {
161  if (‪$value === (string)$constantValue) {
162  return true;
163  }
164  }
165  return false;
166  }
167 
176  public static function ‪getConstants($include_default = false)
177  {
178  static::loadValues();
179  ‪$enumConstants = static::$enumConstants[static::class];
180  if (!$include_default) {
181  unset(‪$enumConstants['__default']);
182  }
183  return ‪$enumConstants;
184  }
185 
192  public static function ‪cast(‪$value)
193  {
194  trigger_error('Class ' . __CLASS__ . ' used by enumeration ' . static::class . ' will be removed in TYPO3 v14.0. Use native enums instead.', E_USER_DEPRECATED);
195 
196  if (!is_object(‪$value) || get_class(‪$value) !== static::class) {
197  ‪$value = new static(‪$value);
198  }
199  return ‪$value;
200  }
201 
208  public function ‪equals(‪$value)
209  {
210  ‪$value = static::cast(‪$value);
211  return $this == ‪$value;
212  }
213 
217  public function ‪__toString()
218  {
219  return (string)‪$this->value;
220  }
221 
228  public static function ‪getName(‪$value)
229  {
230  $name = '';
231  $constants = array_flip(static::getConstants());
232  if (array_key_exists(‪$value, $constants)) {
233  $name = $constants[‪$value];
234  }
235  return $name;
236  }
237 
244  public static function ‪getHumanReadableName(‪$value)
245  {
246  $name = static::getName(‪$value);
247  return ucwords(strtolower(str_replace('_', ' ', $name)));
248  }
249 }
‪TYPO3\CMS\Core\Type\Enumeration\$enumConstants
‪static array $enumConstants
Definition: Enumeration.php:38
‪TYPO3\CMS\Core\Type\Enumeration\isValid
‪bool isValid($value)
Definition: Enumeration.php:155
‪TYPO3\CMS\Core\Type\Enumeration\setValue
‪setValue($value)
Definition: Enumeration.php:137
‪TYPO3\CMS\Core\Type\Enumeration\__toString
‪string __toString()
Definition: Enumeration.php:215
‪TYPO3\CMS\Core\Type\Enumeration\__construct
‪__construct($value=null)
Definition: Enumeration.php:44
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:190
‪TYPO3\CMS\Core\Type\Enumeration\equals
‪bool equals($value)
Definition: Enumeration.php:206
‪TYPO3\CMS\Core\Type\Enumeration\getHumanReadableName
‪static string getHumanReadableName($value)
Definition: Enumeration.php:242
‪TYPO3\CMS\Core\Type\Enumeration\getConstants
‪static array getConstants($include_default=false)
Definition: Enumeration.php:174
‪TYPO3\CMS\Core\Type\Enumeration\$value
‪mixed $value
Definition: Enumeration.php:34
‪TYPO3\CMS\Core\Type\Enumeration
Definition: Enumeration.php:31
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
Definition: InvalidEnumerationValueException.php:23
‪TYPO3\CMS\Core\Type\Enumeration\getName
‪static string getName($value)
Definition: Enumeration.php:226
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
Definition: InvalidEnumerationDefinitionException.php:23
‪TYPO3\CMS\Core\Type\TypeInterface
Definition: TypeInterface.php:24
‪TYPO3\CMS\Core\Type\Enumeration\loadValues
‪static loadValues()
Definition: Enumeration.php:72
‪TYPO3\CMS\Core\Type