Skip to content

Commit c400a1f

Browse files
committed
implemented more validators
1 parent cd8c805 commit c400a1f

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

app/helpers/MetaFormats/PhpTypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ enum PhpTypes: string
1111
case Double = "double";
1212
case Object = "object";
1313
case Null = "NULL";
14+
case Bool = "boolean";
1415
}
1516
// @codingStandardsIgnoreEnd

app/helpers/MetaFormats/Validators/VArray.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class VArray
1212
// validator used for elements
1313
private mixed $nestedValidator;
1414

15+
/**
16+
* Creates an array validator.
17+
* @param mixed $nestedValidator A validator that will be applied on all elements
18+
* (validator arrays are not supported).
19+
*/
1520
public function __construct(mixed $nestedValidator = null)
1621
{
1722
$this->nestedValidator = $nestedValidator;
@@ -40,7 +45,18 @@ public function getElementSwaggerType(): mixed
4045

4146
public function validate(mixed $value)
4247
{
43-
///TODO: check if array, check content
48+
if (!is_array($value)) {
49+
return false;
50+
}
51+
52+
// validate all elements if there is a nested validator
53+
if ($this->nestedValidator != null) {
54+
foreach ($value as $element) {
55+
if (!$this->nestedValidator->validate($element)) {
56+
return false;
57+
}
58+
}
59+
}
4460
return true;
4561
}
4662
}

app/helpers/MetaFormats/Validators/VBool.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Helpers\MetaFormats\Validators;
44

5+
use App\Helpers\MetaFormats\MetaFormatHelper;
56
use App\Helpers\MetaFormats\PhpTypes;
67
use App\Helpers\MetaFormats\PrimitiveFormatValidators;
78

@@ -11,7 +12,7 @@ class VBool
1112

1213
public function validate(mixed $value)
1314
{
14-
///TODO: check if bool
15-
return true;
15+
// support stringified values as well
16+
return MetaFormatHelper::checkType($value, PhpTypes::Bool) || $value == "true" || $value == "false";
1617
}
1718
}

app/helpers/MetaFormats/Validators/VDouble.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Helpers\MetaFormats\Validators;
44

5+
use App\Helpers\MetaFormats\MetaFormatHelper;
56
use App\Helpers\MetaFormats\PhpTypes;
67
use App\Helpers\MetaFormats\PrimitiveFormatValidators;
78

@@ -11,7 +12,12 @@ class VDouble
1112

1213
public function validate(mixed $value)
1314
{
14-
///TODO: check if float
15-
return true;
15+
// check if it is a double
16+
if (MetaFormatHelper::checkType($value, PhpTypes::Double)) {
17+
return true;
18+
}
19+
20+
// the value may be a string containing the number
21+
return is_numeric($value);
1622
}
1723
}

0 commit comments

Comments
 (0)