TYPO3  7.6
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 
17 
25 abstract class Enumeration implements TypeInterface
26 {
30  protected $value;
31 
35  protected static $enumConstants;
36 
41  public function __construct($value = null)
42  {
43  if ($value === null && !defined('static::__default')) {
44  throw new Exception\InvalidEnumerationValueException(
45  sprintf('A value for %s is required if no __default is defined.', get_class($this)),
46  1381512753
47  );
48  }
49  if ($value === null) {
50  $value = static::__default;
51  }
52  static::loadValues();
53  if (!$this->isValid($value)) {
54  throw new Exception\InvalidEnumerationValueException(
55  sprintf('Invalid value %s for %s', $value, get_class($this)),
56  1381512761
57  );
58  }
59  $this->setValue($value);
60  }
61 
67  protected static function loadValues()
68  {
69  $class = get_called_class();
70 
71  if (isset(static::$enumConstants[$class])) {
72  return;
73  }
74 
75  $reflection = new \ReflectionClass($class);
76  $constants = $reflection->getConstants();
77  $defaultValue = null;
78  if (isset($constants['__default'])) {
79  $defaultValue = $constants['__default'];
80  unset($constants['__default']);
81  }
82  if (empty($constants)) {
83  throw new Exception\InvalidEnumerationValueException(
84  sprintf(
85  'No enumeration constants defined for "%s"', $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 must be of type integer or string; constant=%s; type=%s',
95  $constant,
96  is_object($value) ? get_class($value) : gettype($value)
97  ),
98  1381512797
99  );
100  }
101  }
102  $constantValueCounts = array_count_values($constants);
103  arsort($constantValueCounts, SORT_NUMERIC);
104  $constantValueCount = current($constantValueCounts);
105  $constant = key($constantValueCounts);
106  if ($constantValueCount > 1) {
107  throw new Exception\InvalidEnumerationDefinitionException(
108  sprintf(
109  'Constant value is not unique; constant=%s; value=%s; enum=%s',
110  $constant, $constantValueCount, $class
111  ),
112  1381512859
113  );
114  }
115  if ($defaultValue !== null) {
116  $constants['__default'] = $defaultValue;
117  }
118  static::$enumConstants[$class] = $constants;
119  }
120 
128  protected function setValue($value)
129  {
130  $enumKey = array_search($value, static::$enumConstants[get_class($this)]);
131  if ($enumKey === false) {
132  throw new Exception\InvalidEnumerationValueException(
133  sprintf('Invalid value %s for %s', $value, __CLASS__),
134  1381615295
135  );
136  }
137  $this->value = static::$enumConstants[get_class($this)][$enumKey];
138  }
139 
146  protected function isValid($value)
147  {
148  $value = (string)$value;
149  foreach (static::$enumConstants[get_class($this)] as $constantValue) {
150  if ($value === (string)$constantValue) {
151  return true;
152  }
153  }
154  return false;
155  }
156 
165  public static function getConstants($include_default = false)
166  {
167  static::loadValues();
168  $enumConstants = static::$enumConstants[get_called_class()];
169  if (!$include_default) {
170  unset($enumConstants['__default']);
171  }
172  return $enumConstants;
173  }
174 
181  public static function cast($value)
182  {
183  $currentClass = get_called_class();
184  if (!is_object($value) || get_class($value) !== $currentClass) {
185  $value = new $currentClass($value);
186  }
187  return $value;
188  }
189 
196  public function equals($value)
197  {
198  $value = static::cast($value);
199  return $this == $value;
200  }
201 
205  public function __toString()
206  {
207  return (string)$this->value;
208  }
209 }