Issues (2)

src/ValidatorChain.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
use \InvalidArgumentException;
0 ignored issues
show
The type \InvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * Class ValidatorChain
9
 *
10
 * @package Lavibi\Popoya
11
 *
12
 * @method ValidatorChain sameAs(mixed $value)
13
 * @method ValidatorChain notSameAs(mixed $value)
14
 * @method ValidatorChain notEmpty() Check not empty string
15
 * @method ValidatorChain isUpload()
16
 * @method ValidatorChain isInteger()
17
 * @method ValidatorChain isImage()
18
 */
19
class ValidatorChain extends AbstractValidator
20
{
21
    /**
22
     * @var ValidatorInterface[]
23
     */
24
    protected $validators = [];
25
26
    /**
27
     * List of special methods of validators to set option value.
28
     * Each method belongs only validator.
29
     * Use as shortcut for addValidator method
30
     *
31
     * @var array
32
     */
33
    protected $setOptionMethods = [
34
        'sameAs' => '\Lavibi\Popoya\Same',
35
        'notSameAs' => '\Lavibi\Popoya\NotSame'
36
    ];
37
38
    /**
39
     * List of special methods of no option validators
40
     * Each method belongs only validator.
41
     * Use as shortcut for addValidator method
42
     *
43
     * @var array
44
     */
45
    protected $noOptionValidator = [
46
        'notEmpty' => '\Lavibi\Popoya\NotEmptyString',
47
        'isUpload' => '\Lavibi\Popoya\Upload',
48
        'isImage' => '\Lavibi\Popoya\Image',
49
        'isInteger' => '\Lavibi\Popoya\Integer'
50
    ];
51
52
    /**
53
     *
54
     * @param $name
55
     * @param $arguments
56
     *
57 4
     * @return $this
58
     */
59 4
    public function __call($name, $arguments)
60 3
    {
61 3
        if (isset($this->noOptionValidator[$name])) {
62 3
            $validatorClass = $this->noOptionValidator[$name];
63
            $this->validators[$validatorClass] = new $validatorClass();
64
            return $this;
65 2
        }
66 1
67
        if (!isset($this->setOptionMethods[$name])) {
68
            throw new InvalidArgumentException('Set option method ' . $name . ' is not support');
69 1
        }
70
71 1
        $validatorClass = $this->setOptionMethods[$name];
72 1
73
        if (!isset($this->validators[$validatorClass])) {
74
            $this->validators[$validatorClass] = new $validatorClass();
75 1
        }
76
77 1
        call_user_func_array([$this->validators[$validatorClass], $name], $arguments);
78
79
        return $this;
80 4
    }
81
82 4
    public function addValidator(ValidatorInterface $validator)
83
    {
84 4
        $this->validators[get_class($validator)] = $validator;
85
86
        return $this;
87 9
    }
88
89 9
    public function reset()
90
    {
91 9
        $this->validators = [];
92
93 9
        parent::reset();
94
95
        return $this;
96 8
    }
97
98 8
    public function isValid($value)
99
    {
100 8
        $this->value = $this->standardValue = $value;
101 7
102
        foreach ($this->validators as $validator) {
103 6
            $result = $validator->isValid($this->standardValue);
104 3
105 3
            if (!$result) {
106
                $this->setErrorCode($validator->getMessageCode())->setErrorMessage($validator->getMessage());
107
                return false;
108 4
            }
109
110
            $this->standardValue = $validator->getStandardValue();
111 4
        }
112
113
        return true;
114 3
    }
115
116 3
    protected function setErrorMessage($message)
117
    {
118 3
        $this->message = $message;
119
120
        return $this;
121 3
    }
122
123 3
    protected function setErrorCode($code)
124
    {
125 3
        $this->messageCode = $code;
126
127
        return $this;
128
    }
129
}
130