Completed
Push — develop ( 6b671f...4eb6c9 )
by Freddie
11:11
created

FkConstraintValidator::validateNotEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
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\Count;
13
use Symfony\Component\Validator\Constraints\NotBlank;
14
use Symfony\Component\Validator\Constraints\Regex;
15
use Symfony\Component\Validator\ConstraintViolationListInterface;
16
use Symfony\Component\Validator\Validation;
17
18
/**
19
 * @Annotation
20
 */
21
class FkConstraintValidator
22
{
23
    /**
24
     * @param mixed $string
25
     */
26 18
    public function validate($string): ConstraintViolationListInterface
27
    {
28 18
        if (($errors = $this->validateNotEmpty($string))->count()) {
29 2
            return $errors;
30
        }
31
32 16
        if (($errors = $this->validateCount($string))->count()) {
33
            return $errors;
34
        }
35
36 16
        return $this->validateRegex($string);
37
    }
38
39
    /**
40
     * @param mixed $string
41
     */
42 18
    private function validateNotEmpty($string): ConstraintViolationListInterface
43
    {
44 18
        $validator = Validation::createValidator();
45
46 18
        return $validator->validate($string, [
47 18
            new NotBlank(),
48
        ]);
49
    }
50
51
    /**
52
     * @param mixed $string
53
     */
54 16
    private function validateCount($string): ConstraintViolationListInterface
55
    {
56 16
        $validator = Validation::createValidator();
57
58 16
        $parts = \is_string($string) ? \explode(',', $string) : $string;
59
60 16
        return $validator->validate($parts, [
61 16
            new Count([
62 16
                'min' => 1,
63
                'max' => 3,
64
                'minMessage' => 'Allow table[,name[,id]]',
65
                'maxMessage' => 'Allow table[,name[,id]]',
66
            ]),
67
        ]);
68
    }
69
70
    /**
71
     * @param mixed $string
72
     */
73 16
    private function validateRegex($string): ConstraintViolationListInterface
74
    {
75 16
        $validator = Validation::createValidator();
76
77 16
        $string = \is_array($string) ? \implode(',', $string) : $string;
78
79 16
        return $validator->validate($string, [
80 16
            new Regex([
81 16
                'pattern' => '/^[a-zA-Z][a-zA-Z0-9_,]*$/',
82
                'message' => 'Characters not allowed. Use: a-Z, 0-9 and underscore (except in begin)',
83
            ]),
84
        ]);
85
    }
86
}
87