-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVBool.php
More file actions
50 lines (41 loc) · 1.18 KB
/
VBool.php
File metadata and controls
50 lines (41 loc) · 1.18 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
<?php
namespace App\Helpers\MetaFormats\Validators;
/**
* Validates boolean values. Accepts only boolean true and false.
*/
class VBool extends BaseValidator
{
public const SWAGGER_TYPE = "boolean";
public function getExampleValue(): string
{
return "true";
}
public function validate(mixed $value): bool
{
if (is_bool($value)) {
return true;
}
if (!$this->strict) {
// FILTER_VALIDATE_BOOL is not used because it additionally allows "on", "yes", "off", "no" and ""
if ($value === 0 || $value === 1) {
return true;
}
if (is_string($value)) {
$lower = strtolower(trim($value));
return $lower === "0"
|| $lower === "1"
|| $lower === "false"
|| $lower === "true";
}
}
return false;
}
public function patchQueryParameter(mixed &$value): void
{
if (is_string($value)) {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
} else {
$value = (bool)$value;
}
}
}