1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?php
/*
* The MIT License
*
* @author Brian Dayhoff <mopsyd@me.com>
* @copyright (c) 2017, Brian Dayhoff <mopsyd@me.com> all rights reserved.
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace oroboros\core\abstracts\libraries\enum;
/**
* AbstractEnum
* Provides an abstraction that emulates splEnum,
* and provides it with checkval methods.
* This should be extended by concrete implementations
* that provide their own enumerable set.
* Enumerable sets can then be easily created from
* interface constants by implementing them on an
* extension of this class, and can insure exact constant names
* or validate ambiguous parameters.
* @author Brian Dayhoff <mopsyd@me.com>
*/
abstract class AbstractEnum
implements \oroboros\core\interfaces\api\EnumApi
{
const CLASS_SCOPE = \oroboros\core\interfaces\api\ClassScopeApi::CLASS_SCOPE_ENUM_ABSTRACT;
const API = '\\oroboros\\core\\interfaces\\api\\EnumApi';
private $_value;
private static $_blacklist = array(
//exclude the system interoperability constants, they will never be enumerated.
'global' => array(
'__default',
'OROBOROS_VERSION',
'OROBOROS_VENDOR_NAMESPACE',
'CLASS_SCOPE',
'CLASS_TITLE'
)
);
public function __construct($value = null) {
$class = get_class($this);
//set the default value if nothing was passed.
if (defined($class . '::__default') && is_null($value)) {
$value = $class::__default;
}
if (!self::isValid($value)) {
throw new \UnexpectedValueException(sprintf('Value [%s] not a const in %s', $value, get_class($this)));
}
$this->_value = self::_getConstName($value);
}
public function __toString() {
return $this->_value;
}
/**
* Returns all valid enumeration keys;
* @return array
*/
final public static function valid() {
$class = get_called_class();
if (!array_key_exists($class, self::$_blacklist)) {
self::$_blacklist[$class] = $class::blacklist();
}
$reflector = new \ReflectionClass($class);
$constants = $reflector->getConstants();
$vals = array();
foreach ($constants as $key => $value) {
if (!in_array($key, self::_getBlacklist())) {
$vals[] = $value;
}
}
return $vals;
}
/**
* Returns whether a specified key is valid for this enumerable class.
* @param string $value
* @return bool
*/
final public static function isValid($value) {
$keys = self::valid();
return in_array($value, $keys);
}
/**
* Returns a list of the constant names for this class,
* which represent all valid values.
* The default value will not be provided.
*/
final public static function values() {
$reflector = new \ReflectionClass(get_called_class());
$constants = $reflector->getConstants();
$vals = array();
foreach ($constants as $key => $value) {
if (!in_array($key, self::_getBlacklist())) {
$vals[] = $key;
}
}
return $vals;
}
/**
* Override this function if you need certain class constants not to
* return as valid enumerable values, and return an array of the ones
* to exclude.
* This function is called internally while deciding what values are valid.
* @return array
*/
public static function blacklist() {
return array();
}
private static function _getBlacklist() {
$class = get_called_class();
if (!array_key_exists($class, self::$_blacklist)) {
self::$_blacklist[$class] = $class::blacklist();
}
$blacklist = array_merge(self::$_blacklist['global'], self::$_blacklist[$class]);
return $blacklist;
}
private static function _getConstName($key) {
$reflector = new \ReflectionClass(get_called_class());
$constants = $reflector->getConstants();
foreach ($constants as $constant => $value) {
if ($key == $value && !in_array($constant, self::_getBlacklist())) {
return $constant;
}
}
}
}