1 | <?php |
||
17 | class EnumProvider |
||
18 | { |
||
19 | /** |
||
20 | * Enum mapping as an array with : |
||
21 | * |
||
22 | * - alias for Enum class as key |
||
23 | * - Enum FQCN as value |
||
24 | * |
||
25 | * Example : |
||
26 | * |
||
27 | * ``` |
||
28 | * [ |
||
29 | * 'Civility' => Civility::class, |
||
30 | * 'Gender' => Gender::class, |
||
31 | * ] |
||
32 | * ``` |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $enumMapping = []; |
||
37 | |||
38 | public function __construct(array $enumMapping = []) |
||
45 | |||
46 | /** |
||
47 | * @param string $enumValueShortcut As <ENUM_CLASS_ALIAS_OR_ENUM_FCQN>::<ENUM_VALUE_CONSTANT> |
||
48 | * Examples: 'Gender::MALE', 'App\Enum\Gender::FEMALE', 'Permissions::READ|WRITE', etc. |
||
49 | * |
||
50 | * @throws InvalidArgumentException When the alias part of $enumValueShortcut is not a valid alias |
||
51 | */ |
||
52 | public function enum(string $enumValueShortcut): EnumInterface |
||
77 | |||
78 | /** |
||
79 | * @throws InvalidArgumentException When $enumClassAlias is not a valid alias |
||
80 | */ |
||
81 | public function randomEnum(string $enumClassOrAlias): EnumInterface |
||
92 | |||
93 | /** |
||
94 | * Generate an array of unique enum instances between $count & $min. |
||
95 | * |
||
96 | * @param int $count The max nb of enums to generate |
||
97 | * @param bool $variable If false, the exact $count of enums will be generated |
||
98 | * (unless there is no more unique value available) |
||
99 | * @param int $min The min nb of enums to generate, if variable |
||
100 | * |
||
101 | * @return EnumInterface[] |
||
102 | */ |
||
103 | public function randomEnums(string $enumClassOrAlias, int $count, bool $variable = true, int $min = 0): array |
||
128 | |||
129 | /** |
||
130 | * Make sure that $enumClass is a proper Enum class. Throws exception otherwise. |
||
131 | * |
||
132 | * @throws InvalidArgumentException When $enumClass is not a class or is not a proper Enum |
||
133 | */ |
||
134 | private function ensureEnumClass(string $enumClass) |
||
140 | } |
||
141 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.