-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVArray.php
More file actions
114 lines (97 loc) · 3.41 KB
/
VArray.php
File metadata and controls
114 lines (97 loc) · 3.41 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace App\Helpers\MetaFormats\Validators;
use App\Helpers\Swagger\ParameterConstraints;
/**
* Validates arrays and their nested elements.
*/
class VArray extends BaseValidator
{
public const SWAGGER_TYPE = "array";
// validator used for elements
private ?BaseValidator $nestedValidator;
/**
* Creates an array validator.
* @param ?BaseValidator $nestedValidator A validator that will be applied on all elements
* (validator arrays are not supported).
*/
public function __construct(?BaseValidator $nestedValidator = null, bool $strict = true)
{
parent::__construct($strict);
$this->nestedValidator = $nestedValidator;
}
public function getExampleValue(): string | null
{
if ($this->nestedValidator !== null) {
return $this->nestedValidator->getExampleValue();
}
return null;
}
/**
* @return string|null Returns the bottommost element swagger type. Can be null if the element validator is not set.
*/
public function getElementSwaggerType(): mixed
{
// return null if the element type is unspecified
if ($this->nestedValidator === null) {
return null;
}
// traverse the VArray chain to get the final element type
if ($this->nestedValidator instanceof VArray) {
return $this->nestedValidator->getElementSwaggerType();
}
return $this->nestedValidator::SWAGGER_TYPE;
}
/**
* @return int Returns the defined depth of the array.
* 1 for arrays containing the final elements, 2 for arrays of arrays etc.
*/
public function getArrayDepth(): int
{
if ($this->nestedValidator instanceof VArray) {
return $this->nestedValidator->getArrayDepth() + 1;
}
return 1;
}
/**
* @return ParameterConstraints Returns all parameter constrains of the bottommost element type that will be
* written into the generated swagger document. Returns null if there are no constraints.
*/
public function getConstraints(): ?ParameterConstraints
{
return $this->nestedValidator?->getConstraints();
}
/**
* Sets the strict flag for this validator and the element validator if present.
* 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)
{
parent::setStrict($strict);
$this->nestedValidator?->setStrict($strict);
}
public function validate(mixed $value): bool
{
if (!is_array($value)) {
return false;
}
// validate all elements if there is a nested validator
if ($this->nestedValidator != null) {
foreach ($value as $element) {
if (!$this->nestedValidator->validate($element)) {
return false;
}
}
}
return true;
}
public function patchQueryParameter(mixed &$value): void
{
if (is_array($value) && $this->nestedValidator !== null) {
foreach ($value as &$element) {
$this->nestedValidator->patchQueryParameter($element);
}
}
}
}