-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBaseValidator.php
More file actions
80 lines (70 loc) · 2.61 KB
/
BaseValidator.php
File metadata and controls
80 lines (70 loc) · 2.61 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
<?php
namespace App\Helpers\MetaFormats\Validators;
use App\Helpers\Swagger\ParameterConstraints;
/**
* Base class for all validators.
*/
class BaseValidator
{
public function __construct(bool $strict = true)
{
$this->strict = $strict;
}
/**
* @var string One of the valid swagger types (https://swagger.io/docs/specification/v3_0/data-models/data-types/).
*/
public const SWAGGER_TYPE = "invalid";
/**
* @var bool Whether strict type checking is done in validation.
*/
protected bool $strict;
/**
* Sets the strict flag.
* Expected to be changed by Attributes containing validators to change their behavior based on the Attribute type.
* @param bool $strict Whether validation type checking should be done.
* When false, the validation step will no longer enforce the correct type of the value.
*/
public function setStrict(bool $strict)
{
$this->strict = $strict;
}
/**
* @return string Returns a sample expected value to be validated by the validator.
* This value will be used in generated swagger documents.
* Can return null, signalling to the swagger generator to omit the example field.
*/
public function getExampleValue(): string | null
{
return null;
}
/**
* @return ParameterConstraints Returns all parameter constrains that will be written into the generated
* swagger document. Returns null if there are no constraints.
*/
public function getConstraints(): ?ParameterConstraints
{
// there are no default constraints
return null;
}
/**
* Validates a value with the configured validation strictness.
* @param mixed $value The value to be validated.
* @return bool Whether the value passed the test.
*/
public function validate(mixed $value): bool
{
// return false by default to enforce overriding in derived types
return false;
}
/**
* Patches the query/path parameter value after successful validation.
* This is useful for special data types that needs to be converted to a different type before being used
* in the presenter method (e.g., convert strings like "true"/"false" to booleans).
* Note: this method is not called for values that did not pass validation.
* @param mixed $value The value to be patched. Passed by reference, so it can be modified by the method.
*/
public function patchQueryParameter(mixed &$value): void
{
// no-op by default, can be overridden by validators that need to change the value before validation
}
}