-
Notifications
You must be signed in to change notification settings - Fork 1
/
NumericConstraint.php
45 lines (37 loc) · 1.15 KB
/
NumericConstraint.php
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
<?php
declare(strict_types=1);
namespace Chubbyphp\Validation\Constraint;
use Chubbyphp\Validation\Error\Error;
use Chubbyphp\Validation\Error\ErrorInterface;
use Chubbyphp\Validation\ValidatorContextInterface;
use Chubbyphp\Validation\ValidatorInterface;
final class NumericConstraint implements ConstraintInterface
{
/**
* @param mixed $value
*
* @return array<ErrorInterface>
*/
public function validate(
string $path,
$value,
ValidatorContextInterface $context,
?ValidatorInterface $validator = null
) {
if (null === $value || '' === $value) {
return [];
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
return [new Error(
$path,
'constraint.numeric.invalidtype',
['type' => \is_object($value) ? \get_class($value) : \gettype($value)]
)];
}
$value = (string) $value;
if (!is_numeric($value)) {
return [new Error($path, 'constraint.numeric.notnumeric', ['value' => $value])];
}
return [];
}
}