Completed
Push — develop ( 1a3273...1f2dbe )
by Freddie
05:42
created

FkConstraintValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 27
rs 10
ccs 8
cts 11
cp 0.7272
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 2
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 string $string
24
     */
25 14
    public function validate($string): ConstraintViolationListInterface
26
    {
27 14
        $validator = Validation::createValidator();
28
29 14
        if (($errors = $this->notEmpty($string))) {
30 14
            return $errors;
31
        }
32
33
        return $validator->validate(\explode(',', $string), [
34
            new Count([
35
                'min' => 1,
36
                'max' => 3,
37
            ]),
38
        ]);
39
    }
40
41 14
    private function notEmpty($string): ConstraintViolationListInterface
42
    {
43 14
        $validator = Validation::createValidator();
44
45 14
        return $validator->validate($string, [
46 14
            new NotBlank(),
47
        ]);
48
    }
49
}
50