2 namespace TYPO3\CMS\Core\Type;
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)),
50 $value = static::__default;
54 throw new Exception\InvalidEnumerationValueException(
55 sprintf(
'Invalid value %s for %s',
$value, get_class($this)),
69 $class = get_called_class();
71 if (isset(static::$enumConstants[$class])) {
75 $reflection = new \ReflectionClass($class);
76 $constants = $reflection->getConstants();
78 if (isset($constants[
'__default'])) {
79 $defaultValue = $constants[
'__default'];
80 unset($constants[
'__default']);
82 if (empty($constants)) {
83 throw new Exception\InvalidEnumerationValueException(
85 'No enumeration constants defined for "%s"', $class
90 foreach ($constants as $constant =>
$value) {
92 throw new Exception\InvalidEnumerationDefinitionException(
94 'Constant value must be of type integer or string; constant=%s; type=%s',
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(
109 'Constant value is not unique; constant=%s; value=%s; enum=%s',
110 $constant, $constantValueCount, $class
115 if ($defaultValue !== null) {
116 $constants[
'__default'] = $defaultValue;
118 static::$enumConstants[$class] = $constants;
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__),
137 $this->value = static::$enumConstants[get_class($this)][$enumKey];
149 foreach (static::$enumConstants[get_class($this)] as $constantValue) {
150 if (
$value === (
string)$constantValue) {
167 static::loadValues();
169 if (!$include_default) {
183 $currentClass = get_called_class();
184 if (!is_object(
$value) || get_class(
$value) !== $currentClass) {