1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akizuki\enum; |
4
|
|
|
|
5
|
|
|
use Akizuki\enum\Exceptions\ { |
6
|
|
|
EnumConstantTypeException, |
7
|
|
|
InvalidInitializerException, |
8
|
|
|
DuplicatedConstantException |
9
|
|
|
}; |
10
|
|
|
use BadMethodCallException; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use Akizuki\BSC\StandardClass; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* [ Abstract Class ] Enumeration |
16
|
|
|
* |
17
|
|
|
* Provides basic function of Enumeration. |
18
|
|
|
* |
19
|
|
|
* @author 4kizuki <[email protected]> |
20
|
|
|
* @copyright 2017 4kizuki. All Rights Reserved. |
21
|
|
|
* @package 4kizuki/php-enum |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
abstract class Enum extends StandardClass { |
25
|
|
|
|
26
|
|
|
private static $constants = [ ]; |
27
|
|
|
protected $scalar; |
28
|
|
|
|
29
|
14 |
|
public function __construct( $scalar ) { |
30
|
|
|
|
31
|
14 |
|
self::_init( ); |
32
|
|
|
|
33
|
8 |
|
if( !static::_validate_constants( $scalar ) or !isset( self::$constants[ static::class ][ 1 ][ $scalar ] ) ) |
34
|
5 |
|
throw new InvalidInitializerException; |
35
|
|
|
|
36
|
3 |
|
$this->scalar = $scalar; |
37
|
|
|
|
38
|
3 |
|
} |
39
|
|
|
|
40
|
5 |
|
final public static function __callStatic( string $name, array $args ) { |
41
|
|
|
|
42
|
5 |
|
if( !empty( $args ) ) throw new BadMethodCallException; |
43
|
|
|
|
44
|
4 |
|
self::_init( ); |
45
|
|
|
|
46
|
2 |
|
if( !isset( self::$constants[ static::class ][ 0 ][ $name ] ) ) |
47
|
1 |
|
throw new InvalidInitializerException; |
48
|
|
|
|
49
|
1 |
|
$class = static::class; |
50
|
1 |
|
return new $class( self::$constants[ static::class ][ 0 ][ $name ] ); |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
final public static function IsValid( $scalar ) : bool { |
55
|
|
|
|
56
|
|
|
return static::_validate_constants( $scalar ) && isset( self::$constants[ static::class ][ 1 ][ $scalar ] ); |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
17 |
|
private static function _init( ) { |
61
|
|
|
|
62
|
17 |
|
if( isset( self::$constants[ static::class ] ) ) return; |
63
|
|
|
|
64
|
11 |
|
$csts = ( new ReflectionClass( static::class ) )->getConstants(); |
65
|
11 |
|
$vals = [ ]; |
66
|
|
|
|
67
|
11 |
|
foreach( $csts as $key => $value ) { |
68
|
|
|
|
69
|
11 |
|
assert( static::_validate_constants( $value ), new EnumConstantTypeException( static::class, $key, gettype( $value ) ) ); |
70
|
5 |
|
assert( !isset( $vals[ $value ] ), new DuplicatedConstantException( static::class, $key ) ); |
71
|
|
|
|
72
|
5 |
|
$vals[ $value ] = true; |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
self::$constants[ static::class ] = [ $csts, $vals ]; |
77
|
3 |
|
} |
78
|
|
|
|
79
|
|
|
public function value( ) { return $this->scalar; } |
80
|
|
|
|
81
|
9 |
|
protected static function _validate_constants( $value ) : bool { |
82
|
|
|
|
83
|
9 |
|
if( is_int( $value ) ) return true; |
84
|
8 |
|
if( is_string( $value ) && $value !== '' ) return true; |
85
|
6 |
|
return false; |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |