‪TYPO3CMS  10.4
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 
28 abstract class ‪Enumeration implements ‪TypeInterface
29 {
33  protected ‪$value;
34 
38  protected static ‪$enumConstants;
39 
44  public function ‪__construct(‪$value = null)
45  {
46  if (‪$value === null && !defined('static::__default')) {
48  sprintf('A value for enumeration "%s" is required if no __default is defined.', static::class),
49  1381512753
50  );
51  }
52  if (‪$value === null) {
53  ‪$value = static::__default;
54  }
55  static::loadValues();
56  if (!$this->‪isValid(‪$value)) {
58  sprintf('Invalid value "%s" for enumeration "%s"', ‪$value, static::class),
59  1381512761
60  );
61  }
62  $this->‪setValue(‪$value);
63  }
64 
70  protected static function ‪loadValues()
71  {
72  $class = get_called_class();
73 
74  if (isset(static::$enumConstants[$class])) {
75  return;
76  }
77 
78  $reflection = new \ReflectionClass($class);
79  $constants = $reflection->getConstants();
80  $defaultValue = null;
81  if (isset($constants['__default'])) {
82  $defaultValue = $constants['__default'];
83  unset($constants['__default']);
84  }
85  if (empty($constants)) {
87  sprintf(
88  'No constants defined in enumeration "%s"',
89  $class
90  ),
91  1381512807
92  );
93  }
94  foreach ($constants as $constant => ‪$value) {
95  if (!is_int(‪$value) && !is_string(‪$value)) {
97  sprintf(
98  'Constant value "%s" of enumeration "%s" must be of type integer or string, got "%s" instead',
99  $constant,
100  $class,
101  is_object(‪$value) ? get_class(‪$value) : gettype(‪$value)
102  ),
103  1381512797
104  );
105  }
106  }
107  $constantValueCounts = array_count_values($constants);
108  arsort($constantValueCounts, SORT_NUMERIC);
109  $constantValueCount = current($constantValueCounts);
110  $constant = key($constantValueCounts);
111  if ($constantValueCount > 1) {
113  sprintf(
114  'Constant value "%s" of enumeration "%s" is not unique (defined %d times)',
115  $constant,
116  $class,
117  $constantValueCount
118  ),
119  1381512859
120  );
121  }
122  if ($defaultValue !== null) {
123  $constants['__default'] = $defaultValue;
124  }
125  static::$enumConstants[$class] = $constants;
126  }
127 
135  protected function ‪setValue(‪$value)
136  {
137  $enumKey = array_search((string)‪$value, static::$enumConstants[static::class]);
138  if ($enumKey === false) {
140  sprintf('Invalid value "%s" for enumeration "%s"', ‪$value, __CLASS__),
141  1381615295
142  );
143  }
144  $this->value = static::$enumConstants[static::class][$enumKey];
145  }
146 
153  protected function ‪isValid(‪$value)
154  {
155  ‪$value = (string)‪$value;
156  foreach (static::$enumConstants[static::class] as $constantValue) {
157  if (‪$value === (string)$constantValue) {
158  return true;
159  }
160  }
161  return false;
162  }
163 
172  public static function ‪getConstants($include_default = false)
173  {
174  static::loadValues();
175  ‪$enumConstants = static::$enumConstants[static::class];
176  if (!$include_default) {
177  unset(‪$enumConstants['__default']);
178  }
179  return ‪$enumConstants;
180  }
181 
188  public static function ‪cast(‪$value)
189  {
190  if (!is_object(‪$value) || get_class(‪$value) !== static::class) {
191  ‪$value = new static(‪$value);
192  }
193  return ‪$value;
194  }
195 
202  public function ‪equals(‪$value)
203  {
204  ‪$value = static::cast(‪$value);
205  return $this == ‪$value;
206  }
207 
211  public function ‪__toString()
212  {
213  return (string)‪$this->value;
214  }
215 
222  public static function ‪getName(‪$value)
223  {
224  $name = '';
225  $constants = array_flip(static::getConstants());
226  if (array_key_exists(‪$value, $constants)) {
227  $name = $constants[‪$value];
228  }
229  return $name;
230  }
231 
238  public static function ‪getHumanReadableName(‪$value)
239  {
240  $name = static::getName(‪$value);
241  return ucwords(strtolower(str_replace('_', ' ', $name)));
242  }
243 }
‪TYPO3\CMS\Core\Type\Enumeration\$enumConstants
‪static array $enumConstants
Definition: Enumeration.php:36
‪TYPO3\CMS\Core\Type\Enumeration\isValid
‪bool isValid($value)
Definition: Enumeration.php:151
‪TYPO3\CMS\Core\Type\Enumeration\setValue
‪setValue($value)
Definition: Enumeration.php:133
‪TYPO3\CMS\Core\Type\Enumeration\__toString
‪string __toString()
Definition: Enumeration.php:209
‪TYPO3\CMS\Core\Type\Enumeration\__construct
‪__construct($value=null)
Definition: Enumeration.php:42
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:186
‪TYPO3\CMS\Core\Type\Enumeration\equals
‪bool equals($value)
Definition: Enumeration.php:200
‪TYPO3\CMS\Core\Type\Enumeration\getHumanReadableName
‪static string getHumanReadableName($value)
Definition: Enumeration.php:236
‪TYPO3\CMS\Core\Type\Enumeration\getConstants
‪static array getConstants($include_default=false)
Definition: Enumeration.php:170
‪TYPO3\CMS\Core\Type\Enumeration\$value
‪mixed $value
Definition: Enumeration.php:32
‪TYPO3\CMS\Core\Type\Enumeration
Definition: Enumeration.php:29
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
Definition: InvalidEnumerationValueException.php:24
‪TYPO3\CMS\Core\Type\Enumeration\getName
‪static string getName($value)
Definition: Enumeration.php:220
‪TYPO3\CMS\Core\Type\Exception\InvalidEnumerationDefinitionException
Definition: InvalidEnumerationDefinitionException.php:24
‪TYPO3\CMS\Core\Type\TypeInterface
Definition: TypeInterface.php:24
‪TYPO3\CMS\Core\Type\Enumeration\loadValues
‪static loadValues()
Definition: Enumeration.php:68
‪TYPO3\CMS\Core\Type