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)),
68 $class = get_called_class();
70 if (isset(static::$enumConstants[$class])) {
74 $reflection = new \ReflectionClass($class);
75 $constants = $reflection->getConstants();
77 if (isset($constants[
'__default'])) {
78 $defaultValue = $constants[
'__default'];
79 unset($constants[
'__default']);
81 if (empty($constants)) {
82 throw new Exception\InvalidEnumerationValueException(
84 'No enumeration constants defined for "%s"', $class
89 foreach ($constants as $constant =>
$value) {
91 throw new Exception\InvalidEnumerationDefinitionException(
93 'Constant value must be of type integer or string; constant=%s; type=%s',
101 $constantValueCounts = array_count_values($constants);
102 arsort($constantValueCounts, SORT_NUMERIC);
103 $constantValueCount = current($constantValueCounts);
104 $constant = key($constantValueCounts);
105 if ($constantValueCount > 1) {
106 throw new Exception\InvalidEnumerationDefinitionException(
108 'Constant value is not unique; constant=%s; value=%s; enum=%s',
109 $constant, $constantValueCount, $class
114 if ($defaultValue !== NULL) {
115 $constants[
'__default'] = $defaultValue;
117 static::$enumConstants[$class] = $constants;
128 $enumKey = array_search(
$value, static::$enumConstants[get_class($this)]);
129 if ($enumKey === FALSE) {
130 throw new Exception\InvalidEnumerationValueException(
131 sprintf(
'Invalid value %s for %s',
$value, __CLASS__),
135 $this->value = static::$enumConstants[get_class($this)][$enumKey];
146 foreach (static::$enumConstants[get_class($this)] as $constantValue) {
147 if (
$value === (
string) $constantValue) {
163 static::loadValues();
165 if (!$include_default) {
178 $currentClass = get_called_class();
179 if (!is_object(
$value) || get_class(
$value) !== $currentClass) {
static getConstants($include_default=FALSE)