ValidatorChain::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of slick/validator package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Validator;
11
12
use Slick\Common\Base;
13
14
/**
15
 * Validator Chain
16
 *
17
 * @package Slick\Validator
18
 * @author  Filipe Silva <[email protected]>
19
 * @deprecated You should use the ValidationChain instead
20
 */
21
class ValidatorChain extends Base implements ChainInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Slick\Validator\ChainInterface has been deprecated with message: You should use the ValidationChainInterface instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
22
{
23
24
    /**
25
     * @readwrite
26
     * @var ValidatorInterface[]
27
     */
28
    protected $_validators = [];
29
30
    /**
31
     * @readwrite
32
     * @var array
33
     */
34
    protected $_messages = [];
35
36
    /**
37
     * Returns true if and only if $value meets the validation requirements
38
     *
39
     * @param mixed $value
40
     * @return bool
41
     */
42
    public function isValid($value)
43
    {
44
        $messages = [];
45
        $valid = true;
46 View Code Duplication
        foreach ($this->_validators as $validator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            if (!$validator->validates($value)) {
48
                $valid = false;
49
                $messages[] = $validator->getMessage();
50
            }
51
        }
52
        $this->_messages = $messages;
53
        return $valid;
54
    }
55
56
    /**
57
     * Returns an array of messages that explain why the most recent
58
     * isValid() call returned false. The array keys are validation failure
59
     * message identifiers, and the array values are the corresponding
60
     * human-readable message strings.
61
     *
62
     * @return array
63
     */
64
    public function getMessages()
65
    {
66
        return $this->_messages;
67
    }
68
69
    /**
70
     * Adds a validator to the chain
71
     *
72
     * @param ValidatorInterface $validator
73
     *
74
     * @return ValidatorChain
75
     */
76
    public function add(ValidatorInterface $validator)
77
    {
78
        $this->_validators[] = $validator;
79
        return $this;
80
    }
81
}