-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathEnumValidation.php
More file actions
83 lines (71 loc) · 1.93 KB
/
EnumValidation.php
File metadata and controls
83 lines (71 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace MyCLabs\Tests\Enum\StaticAnalysis;
use MyCLabs\Enum\Enum;
/**
* @psalm-immutable
* @psalm-template T of 'A'|'C'
* @template-extends Enum<T>
*/
final class ValidationEnum extends Enum
{
const A = 'A';
const C = 'C';
}
/**
* @psalm-pure
* @param mixed $input
* @psalm-return 'A'|'C'
*
* @psalm-suppress MixedReturnStatement
* @psalm-suppress MixedInferredReturnType at the time of this writing, we did not yet find
* a proper approach to constraint input values through
* validation via static methods.
*/
function canValidateValue($input): string
{
ValidationEnum::assertValidValue($input);
return $input;
}
/**
* @psalm-pure
* @param mixed $input
* @psalm-return 'A'|'C'
*/
function canAssertValidEnumValue($input): string
{
ValidationEnum::assertValidEnumValue(ValidationEnum::class, $input);
return $input;
}
/**
* @psalm-pure
* @param mixed $input
* @psalm-return 'A'|'C'
*
* @psalm-suppress MixedReturnStatement
* @psalm-suppress MixedInferredReturnType at the time of this writing, we did not yet find
* a proper approach to constraint input values through
* validation via static methods.
*/
function canValidateValueThroughIsValid($input): string
{
if (! ValidationEnum::isValid($input)) {
throw new \InvalidArgumentException('Value not valid');
}
return $input;
}
/**
* @psalm-pure
* @param mixed $input
* @psalm-return 'A'|'C'|1
*
* @psalm-suppress InvalidReturnType https://github.com/vimeo/psalm/issues/5372
* @psalm-suppress InvalidReturnStatement https://github.com/vimeo/psalm/issues/5372
*/
function canValidateValueThroughIsValidEnumValue($input)
{
if (! ValidationEnum::isValidEnumValue(ValidationEnum::class, $input)) {
return 1;
}
return $input;
}