1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Thunder\Platenum\Exception; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
final class PlatenumException extends \LogicException |
9
|
|
|
{ |
10
|
|
|
/* --- OVERRIDE --- */ |
11
|
|
|
|
12
|
9 |
|
public static function fromInvalidMember(string $fqcn, string $member, array $members): self |
13
|
|
|
{ |
14
|
9 |
|
return new self(sprintf('Enum `%s` does not contain member `%s` among `%s`.', $fqcn, $member, implode(',', array_keys($members)))); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** @param mixed $value */ |
18
|
6 |
|
public static function fromInvalidValue(string $fqcn, $value): self |
19
|
|
|
{ |
20
|
6 |
|
return new self(sprintf('Enum `%s` does not contain any member with value `%s`.', $fqcn, is_scalar($value) ? strval($value) : gettype($value))); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/* --- GENERIC --- */ |
24
|
|
|
|
25
|
|
|
/** @param mixed $value */ |
26
|
1 |
|
public static function fromIllegalValue(string $fqcn, $value): self |
27
|
|
|
{ |
28
|
1 |
|
return new self(sprintf('Enum `%s` value must be a scalar, `%s` given.', $fqcn, gettype($value))); |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
public static function fromMismatchedClass(string $fqcn, string $other): self |
32
|
|
|
{ |
33
|
2 |
|
return new self(sprintf('Attempting to recreate enum %s from instance of %s.', $fqcn, $other)); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public static function fromConstantArguments(string $fqcn): self |
37
|
|
|
{ |
38
|
1 |
|
return new self(sprintf('Enum `%s` constant methods must not have any arguments.', $fqcn)); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public static function fromNonUniqueMembers(string $fqcn): self |
42
|
|
|
{ |
43
|
1 |
|
return new self(sprintf('Enum `%s` members values are not unique.', $fqcn)); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public static function fromMissingResolve(string $fqcn): self |
47
|
|
|
{ |
48
|
1 |
|
return new self(sprintf('Enum `%s` does not implement resolve() method.', $fqcn)); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public static function fromEmptyMembers(string $fqcn): self |
52
|
|
|
{ |
53
|
1 |
|
return new self(sprintf('Enum `%s` does not contain any members.', $fqcn)); |
54
|
|
|
} |
55
|
|
|
|
56
|
7 |
|
public static function fromMagicMethod(string $fqcn, string $method): self |
57
|
|
|
{ |
58
|
7 |
|
return new self(sprintf('Enum `%s` does not allow magic `%s` method.', $fqcn, $method)); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public static function fromNonStringMembers(string $fqcn): self |
62
|
|
|
{ |
63
|
1 |
|
return new self(sprintf('Enum `%s` requires all members to be strings.', $fqcn)); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public static function fromNonUniformMemberValues(string $fqcn, array $members): self |
67
|
|
|
{ |
68
|
|
|
/** @psalm-suppress MissingClosureParamType */ |
69
|
1 |
|
$callback = function($value): string { return gettype($value); }; |
70
|
1 |
|
$values = array_unique(array_map($callback, $members)); |
71
|
|
|
|
72
|
1 |
|
return new self(sprintf('Enum `%s` member values must be of the same type, `%s` given.', $fqcn, implode(',', $values))); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/* --- DOCBLOCK --- */ |
76
|
|
|
|
77
|
1 |
|
public static function fromMissingDocblock(string $fqcn): self |
78
|
|
|
{ |
79
|
1 |
|
return new self(sprintf('Enum %s does not have a docblock with member definitions.', $fqcn)); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public static function fromEmptyDocblock(string $fqcn): self |
83
|
|
|
{ |
84
|
1 |
|
return new self(sprintf('Enum %s contains docblock without any member definitions.', $fqcn)); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public static function fromMalformedDocblock(string $fqcn): self |
88
|
|
|
{ |
89
|
1 |
|
return new self(sprintf('Enum %s contains malformed docblock member definitions.', $fqcn)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* --- STATIC --- */ |
93
|
|
|
|
94
|
1 |
|
public static function fromMissingMappingProperty(string $fqcn): self |
95
|
|
|
{ |
96
|
1 |
|
return new self(sprintf('Enum %s requires static property $mapping with members definitions.', $fqcn)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* --- CALLBACK --- */ |
100
|
|
|
|
101
|
2 |
|
public static function fromInvalidCallback(string $fqcn): self |
102
|
|
|
{ |
103
|
2 |
|
return new self(sprintf('Enum %s requires static property $callback to be a valid callable returning members and values mapping.', $fqcn)); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public static function fromAlreadyInitializedCallback(string $fqcn): self |
107
|
|
|
{ |
108
|
1 |
|
return new self(sprintf('Enum %s callback was already initialized.', $fqcn)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|