RangeConstraintValidator::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Schema\Validators\Constraints;
11
12
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
13
use Symfony\Component\Validator\ConstraintViolationListInterface;
14
use Symfony\Component\Validator\Validation;
15
16
/**
17
 * @Annotation
18
 */
19
class RangeConstraintValidator
20
{
21 9
    public function validate(array $rule): ConstraintViolationListInterface
22
    {
23 9
        $validator = Validation::createValidator();
24
25 9
        if (($errors = (new MinConstraintValidator)->validate($rule['min'] ?? null))->count()) {
26 2
            return $errors;
27
        }
28
29 7
        if (($errors = (new MaxConstraintValidator)->validate($rule['max'] ?? null))->count()) {
30 3
            return $errors;
31
        }
32
33 4
        return $validator->validate($rule['max'], [
34 4
            new GreaterThanOrEqual($rule['min']),
35
        ]);
36
    }
37
}
38