Completed
Push — master ( 83cea8...e47c19 )
by Anton
17s queued 12s
created

Validator::getValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Validator\Traits;
13
14
use Bluz\Validator\ValidatorChain;
15
use Bluz\Validator\ValidatorForm;
16
17
/**
18
 * Validator trait
19
 *
20
 * Example of usage
21
 * <code>
22
 *    use Bluz\Validator\Traits\Validator;
23
 *    use Bluz\Validator\Validator as v;
24
 *
25
 *    class Row extends Db\Row {
26
 *        use Validator;
27
 *        function beforeSave()
28
 *        {
29
 *             $this->addValidator(
30
 *                 'login',
31
 *                 v::required()->latin()->length(3, 255)
32
 *             );
33
 *        }
34
 *    }
35
 * </code>
36
 *
37
 * @package  Bluz\Validator\Traits
38
 * @author   Anton Shevchuk
39
 */
40
trait Validator
41
{
42
    /**
43
     * @var ValidatorForm instance
44
     */
45
    private $validatorForm;
46
47
    /**
48
     * Get ValidatorBuilder
49
     *
50
     * @return ValidatorForm
51
     */
52 2
    private function getValidatorForm(): ValidatorForm
53
    {
54 2
        if (!$this->validatorForm) {
55 2
            $this->validatorForm = new ValidatorForm();
56
        }
57 2
        return $this->validatorForm;
58
    }
59
60
    /**
61
     * Add ValidatorChain
62
     *
63
     * @param  string $name
64
     *
65
     * @return ValidatorChain
66
     */
67 2
    public function addValidator($name): ValidatorChain
68
    {
69 2
        return $this->getValidatorForm()->add($name);
70
    }
71
72
    /**
73
     * Get ValidatorChain
74
     *
75
     * @param  string $name
76
     *
77
     * @return ValidatorChain
78
     */
79
    public function getValidator($name): ValidatorChain
80
    {
81
        return $this->getValidatorForm()->get($name);
82
    }
83
84
    /**
85
     * Validate input data
86
     *
87
     * @param  array $input
88
     *
89
     * @return bool
90
     */
91
    public function validate($input): bool
92
    {
93
        return $this->getValidatorForm()->validate($input);
94
    }
95
96
    /**
97
     * Assert input data
98
     *
99
     * @param  array $input
100
     *
101
     * @throws \Bluz\Validator\Exception\ValidatorException
102
     */
103 2
    public function assert($input): void
104
    {
105 2
        $this->getValidatorForm()->assert($input);
106 2
    }
107
}
108