‪TYPO3CMS  9.5
Enumeration.php
Go to the documentation of this file.
1 <?php
2 namespace ‪TYPO3\CMS\Core\Type;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
24 abstract class ‪Enumeration implements ‪TypeInterface
25 {
29  protected ‪$value;
30 
34  protected static ‪$enumConstants;
35 
40  public function ‪__construct(‪$value = null)
41  {
42  if (‪$value === null && !defined('static::__default')) {
43  throw new Exception\InvalidEnumerationValueException(
44  sprintf('A value for enumeration "%s" is required if no __default is defined.', static::class),
45  1381512753
46  );
47  }
48  if (‪$value === null) {
49  ‪$value = static::__default;
50  }
51  static::loadValues();
52  if (!$this->‪isValid(‪$value)) {
53  throw new Exception\InvalidEnumerationValueException(
54  sprintf('Invalid value "%s" for enumeration "%s"', ‪$value, static::class),
55  1381512761
56  );
57  }
58  $this->‪setValue(‪$value);
59  }
60 
66  protected static function ‪loadValues()
67  {
68  $class = get_called_class();
69 
70  if (isset(static::$enumConstants[$class])) {
71  return;
72  }
73 
74  $reflection = new \ReflectionClass($class);
75  $constants = $reflection->getConstants();
76  $defaultValue = null;
77  if (isset($constants['__default'])) {
78  $defaultValue = $constants['__default'];
79  unset($constants['__default']);
80  }
81  if (empty($constants)) {
82  throw new Exception\InvalidEnumerationValueException(
83  sprintf(
84  'No constants defined in enumeration "%s"',
85  $class
86  ),
87  1381512807
88  );
89  }
90  foreach ($constants as $constant => ‪$value) {
91  if (!is_int(‪$value) && !is_string(‪$value)) {
92  throw new Exception\InvalidEnumerationDefinitionException(
93  sprintf(
94  'Constant value "%s" of enumeration "%s" must be of type integer or string, got "%s" instead',
95  $constant,
96  $class,
97  is_object(‪$value) ? get_class(‪$value) : gettype(‪$value)
98  ),
99  1381512797
100  );
101  }
102  }
103  $constantValueCounts = array_count_values($constants);
104  arsort($constantValueCounts, SORT_NUMERIC);
105  $constantValueCount = current($constantValueCounts);
106  $constant = key($constantValueCounts);
107  if ($constantValueCount > 1) {
108  throw new Exception\InvalidEnumerationDefinitionException(
109  sprintf(
110  'Constant value "%s" of enumeration "%s" is not unique (defined %d times)',
111  $constant,
112  $class,
113  $constantValueCount
114  ),
115  1381512859
116  );
117  }
118  if ($defaultValue !== null) {
119  $constants['__default'] = $defaultValue;
120  }
121  static::$enumConstants[$class] = $constants;
122  }
123 
131  protected function ‪setValue(‪$value)
132  {
133  $enumKey = array_search((string)‪$value, static::$enumConstants[static::class]);
134  if ($enumKey === false) {
135  throw new Exception\InvalidEnumerationValueException(
136  sprintf('Invalid value "%s" for enumeration "%s"', ‪$value, __CLASS__),
137  1381615295
138  );
139  }
140  $this->value = static::$enumConstants[static::class][$enumKey];
141  }
142 
149  protected function ‪isValid(‪$value)
150  {
151  ‪$value = (string)‪$value;
152  foreach (static::$enumConstants[static::class] as $constantValue) {
153  if (‪$value === (string)$constantValue) {
154  return true;
155  }
156  }
157  return false;
158  }
159 
168  public static function ‪getConstants($include_default = false)
169  {
170  static::loadValues();
171  ‪$enumConstants = static::$enumConstants[static::class];
172  if (!$include_default) {
173  unset(‪$enumConstants['__default']);
174  }
175  return ‪$enumConstants;
176  }
177 
184  public static function ‪cast(‪$value)
185  {
186  if (!is_object(‪$value) || get_class(‪$value) !== static::class) {
187  ‪$value = new static(‪$value);
188  }
189  return ‪$value;
190  }
191 
198  public function ‪equals(‪$value)
199  {
200  ‪$value = static::cast(‪$value);
201  return $this == ‪$value;
202  }
203 
207  public function ‪__toString()
208  {
209  return (string)‪$this->value;
210  }
211 
218  public static function ‪getName(‪$value)
219  {
220  $name = '';
221  $constants = array_flip(static::getConstants());
222  if (array_key_exists(‪$value, $constants)) {
223  $name = $constants[‪$value];
224  }
225  return $name;
226  }
227 
234  public static function ‪getHumanReadableName(‪$value)
235  {
236  $name = static::getName(‪$value);
237  return ucwords(strtolower(str_replace('_', ' ', $name)));
238  }
239 }
‪TYPO3\CMS\Core\Type\Enumeration\$enumConstants
‪static array $enumConstants
Definition: Enumeration.php:32
‪TYPO3\CMS\Core\Type\Enumeration\isValid
‪bool isValid($value)
Definition: Enumeration.php:147
‪TYPO3\CMS\Core\Type\Enumeration\setValue
‪setValue($value)
Definition: Enumeration.php:129
‪TYPO3\CMS\Core\Type\Enumeration\__toString
‪string __toString()
Definition: Enumeration.php:205
‪TYPO3\CMS\Core\Type\Enumeration\__construct
‪__construct($value=null)
Definition: Enumeration.php:38
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:182
‪TYPO3\CMS\Core\Type\Enumeration\equals
‪bool equals($value)
Definition: Enumeration.php:196
‪TYPO3\CMS\Core\Type\Enumeration\getHumanReadableName
‪static string getHumanReadableName($value)
Definition: Enumeration.php:232
‪TYPO3\CMS\Core\Type\Enumeration\getConstants
‪static array getConstants($include_default=false)
Definition: Enumeration.php:166
‪TYPO3\CMS\Core\Type\Enumeration\$value
‪mixed $value
Definition: Enumeration.php:28
‪TYPO3\CMS\Core\Type\Enumeration
Definition: Enumeration.php:25
‪TYPO3\CMS\Core\Type\Enumeration\getName
‪static string getName($value)
Definition: Enumeration.php:216
‪TYPO3\CMS\Core\Type\TypeInterface
Definition: TypeInterface.php:23
‪TYPO3\CMS\Core\Type\Enumeration\loadValues
‪static loadValues()
Definition: Enumeration.php:64
‪TYPO3\CMS\Core\Type