Completed
Push — develop ( 1f2dbe...562875 )
by Freddie
03:44
created

FkConstraintValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 32
rs 10
ccs 12
cts 12
cp 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 14 3
A notEmpty() 0 6 1
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\Count;
13
use Symfony\Component\Validator\Constraints\NotBlank;
14
use Symfony\Component\Validator\ConstraintViolationListInterface;
15
use Symfony\Component\Validator\Validation;
16
17
/**
18
 * @Annotation
19
 */
20
class FkConstraintValidator
21
{
22
    /**
23
     * @param mixed $string
24
     */
25 14
    public function validate($string): ConstraintViolationListInterface
26
    {
27 14
        $validator = Validation::createValidator();
28
29 14
        if (($errors = $this->notEmpty($string))->count()) {
30 2
            return $errors;
31
        }
32
33 12
        $_vars = \is_string($string) ? \explode(',', $string) : $string;
34
35 12
        return $validator->validate($_vars, [
36 12
            new Count([
37 12
                'min' => 1,
38
                'max' => 3,
39
            ]),
40
        ]);
41
    }
42
43
    /**
44
     * @param mixed $string
45
     */
46 14
    private function notEmpty($string): ConstraintViolationListInterface
47
    {
48 14
        $validator = Validation::createValidator();
49
50 14
        return $validator->validate($string, [
51 14
            new NotBlank(),
52
        ]);
53
    }
54
}
55