|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Enum package. |
|
5
|
|
|
* For the full copyright information please view the LICENCE file that was |
|
6
|
|
|
* distributed with this package. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Simon Deeley 2017 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace simondeeley; |
|
12
|
|
|
|
|
13
|
|
|
use simondeeley\Type\EnumType; |
|
14
|
|
|
use simondeeley\ImmutableObject; |
|
15
|
|
|
use simondeeley\Exceptions\InvalidEnumValueException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Eum class |
|
19
|
|
|
* |
|
20
|
|
|
* Allows you to construct an ENUM type immutable object using class constants. |
|
21
|
|
|
* To use, extend this class and add class constants to your child class. These |
|
22
|
|
|
* constants will be the enumerable types checked against when you instantiate |
|
23
|
|
|
* a new instance of your ENUM object. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Simon Deeley <[email protected]> |
|
26
|
|
|
* |
|
27
|
|
|
* @abstract |
|
28
|
|
|
* @uses ImmutableObject |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class Enum extends ImmutableObject implements EnumType |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var mixed $value |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $value; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Construct a new ENUM object |
|
39
|
|
|
* |
|
40
|
|
|
* Takes one paramater which is the type of the ENUM object. If that type |
|
41
|
|
|
* does not exist in the ENUM object then an exception is thrown. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $value - The value of the ENUM |
|
44
|
|
|
* @return void |
|
45
|
|
|
* @throws InvalidEnumValueException - Thrown if the value passed is invalid |
|
46
|
|
|
*/ |
|
47
|
20 |
|
final public function __construct(string $type) |
|
48
|
|
|
{ |
|
49
|
20 |
|
if (false === defined("static::$type") || null === constant("static::$type")) { |
|
50
|
7 |
|
throw new InvalidEnumValueException; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
13 |
|
$this->value = constant("static::$type"); |
|
54
|
13 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Call a static method to construct a new enum |
|
58
|
|
|
* |
|
59
|
|
|
* Attempts to convert the called method name to uppercase but if that fails |
|
60
|
|
|
* then it resorts to using whatever case the method name was passed in. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $name - the name of the method to invoke |
|
63
|
|
|
* @param array $args - an array of arguments passed to the method call |
|
64
|
|
|
* @return self |
|
65
|
|
|
* @throws InvalidEnumValueException - Thrown if the value passed is not an enum constant |
|
66
|
|
|
*/ |
|
67
|
4 |
|
final public static function __callStatic(string $method, array $args = [ ]): self |
|
68
|
|
|
{ |
|
69
|
|
|
try { |
|
70
|
4 |
|
return new static(strtoupper($method)); |
|
71
|
|
|
} catch (InvalidEnumValueException $exception) { |
|
72
|
|
|
return new static($method); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the value of the ENUM |
|
78
|
|
|
* |
|
79
|
|
|
* @final |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
13 |
|
final public function getValue() |
|
83
|
|
|
{ |
|
84
|
13 |
|
return $this->value; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns the type of the ENUM |
|
89
|
|
|
* |
|
90
|
|
|
* @see simondeeley\Type\Type |
|
91
|
|
|
* @static |
|
92
|
|
|
* |
|
93
|
|
|
* @return string - String literal of the enum object |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function getType(): string |
|
96
|
|
|
{ |
|
97
|
|
|
return 'ENUM'; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|