Completed
Push — master ( 6f2bb4...6400d0 )
by Anton
11s
created

Validator::getValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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