1 | <?php |
||
11 | trait EnumTrait |
||
12 | { |
||
13 | /** |
||
14 | * |
||
15 | * @var int Represents the value of the enumerative instance. |
||
16 | */ |
||
17 | protected $value; |
||
18 | |||
19 | /** |
||
20 | * |
||
21 | * @var string Represents the description of the enumerative instance. |
||
22 | */ |
||
23 | protected $description; |
||
24 | |||
25 | /** |
||
26 | * |
||
27 | * @var array Represents possible values according to the enumerative class. |
||
28 | */ |
||
29 | protected static $values = []; |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | * @var array Represents the possible descriptive values according to the enumerative class. |
||
34 | */ |
||
35 | protected static $descriptions = []; |
||
36 | |||
37 | /** |
||
38 | * |
||
39 | * @var array Represents the names of the constants of the enumerative class. |
||
40 | */ |
||
41 | protected static $constants = []; |
||
42 | |||
43 | /** |
||
44 | * Gets all possible values for the enumerative class. |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | public static function getValues(): array |
||
54 | |||
55 | /** |
||
56 | * Gets all possible descriptions for the enumerative class. |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | public static function getDescriptions(): array |
||
66 | |||
67 | /** |
||
68 | * Get value of the enumerative instance. |
||
69 | * |
||
70 | * @return int |
||
71 | */ |
||
72 | public function getValue(): int |
||
76 | |||
77 | /** |
||
78 | * Get description of the enumerative instance. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getDescription(): string |
||
86 | |||
87 | /** |
||
88 | * |
||
89 | * @param string $name |
||
90 | * @param array $arguments |
||
91 | * @return Enum |
||
92 | */ |
||
93 | public static function __callStatic(string $name, array $arguments): EnumInterface |
||
100 | |||
101 | /** |
||
102 | * Gets the descriptive value that represents the enumerative instance. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function __toString(): string |
||
113 | |||
114 | /** |
||
115 | * Get the instance of the enumerative class |
||
116 | * |
||
117 | * @param int $value |
||
118 | * @param string $description |
||
119 | * @return EnumInterface |
||
120 | */ |
||
121 | abstract protected static function getInstance(int $value, string $description = null): EnumInterface; |
||
122 | |||
123 | /** |
||
124 | * Get the instance of the enumerative class by constant |
||
125 | * |
||
126 | * @param string $constant |
||
127 | * @throws \InvalidArgumentException |
||
128 | * @return EnumInterface |
||
129 | */ |
||
130 | protected static function getEnumByConstant(string $constant): EnumInterface |
||
141 | |||
142 | /** |
||
143 | * Get the instance of the enumerative class by description |
||
144 | * |
||
145 | * @param string $constant |
||
146 | * @throws \InvalidArgumentException |
||
147 | * @return EnumInterface |
||
148 | */ |
||
149 | protected static function getEnumByDescription(string $constant): EnumInterface |
||
159 | |||
160 | /** |
||
161 | * Get the instance of the enumerative class by value, if it's in values. |
||
162 | * |
||
163 | * @param string $value |
||
164 | * @param array $values |
||
165 | * @throws \InvalidArgumentException |
||
166 | * @return EnumInterface |
||
167 | */ |
||
168 | protected static function getEnumByValue(string $value, array $values): EnumInterface |
||
181 | |||
182 | /** |
||
183 | * Assigns the possible values according to the enumerative class. |
||
184 | */ |
||
185 | protected static function assignValues() |
||
189 | |||
190 | /** |
||
191 | * Assigns the constants of the enumerative class. |
||
192 | */ |
||
193 | protected static function assignConstants() |
||
197 | } |
||
198 |