Completed
Pull Request — master (#178)
by ignace nyamagana
02:27
created

RowFilter::validateRow()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
namespace League\Csv\Config;
14
15
use League\Csv\InvalidRowException;
16
17
/**
18
 * Trait to format and validate the record before insertion
19
 *
20
 * @package League.csv
21
 * @since  7.0.0
22
 */
23
trait RowFilter
24
{
25
    /**
26
     * Callables to validate the row before insertion
27
     *
28
     * @var callable[]
29
     */
30
    protected $validators = [];
31
32
    /**
33
     * Callables to format the row before insertion
34
     *
35
     * @var callable[]
36
     */
37
    protected $formatters = [];
38
39
    /**
40
     * add a formatter to the collection
41
     *
42
     * @param callable $callable
43
     *
44
     * @return $this
45
     */
46
    public function addFormatter(callable $callable)
47
    {
48
        $this->formatters[] = $callable;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Remove a formatter from the collection
55
     *
56
     * @param callable $callable
57
     *
58
     * @return $this
59
     */
60
    public function removeFormatter(callable $callable)
61
    {
62
        $res = array_search($callable, $this->formatters, true);
63
        unset($this->formatters[$res]);
64
65
        return $this;
66
    }
67
68
    /**
69
     * Detect if the formatter is already registered
70
     *
71
     * @param callable $callable
72
     *
73
     * @return bool
74
     */
75
    public function hasFormatter(callable $callable)
76
    {
77
        return false !== array_search($callable, $this->formatters, true);
78
    }
79
80
    /**
81
     * Remove all registered formatter
82
     *
83
     * @return $this
84
     */
85
    public function clearFormatters()
86
    {
87
        $this->formatters = [];
88
89
        return $this;
90
    }
91
92
    /**
93
     * add a Validator to the collection
94
     *
95
     * @param callable $callable
96
     * @param string   $name     the rule name
97
     *
98
     * @return $this
99
     */
100
    public function addValidator(callable $callable, $name)
101
    {
102
        $name = $this->filterString($name);
103
104
        $this->validators[$name] = $callable;
105
106
        return $this;
107
    }
108
109
    /**
110
     * validate a string
111
     *
112
     * @param mixed $str the value to evaluate as a string
113
     *
114
     * @throws InvalidArgumentException if the submitted data can not be converted to string
115
     *
116
     * @return string
117
     */
118
    abstract protected function filterString($str);
119
120
    /**
121
     * Remove a validator from the collection
122
     *
123
     * @param string $name the validator name
124
     *
125
     * @return $this
126
     */
127
    public function removeValidator($name)
128
    {
129
        $name = $this->filterString($name);
130
        unset($this->validators[$name]);
131
132
        return $this;
133
    }
134
135
    /**
136
     * Detect if a validator is already registered
137
     *
138
     * @param string $name the validator name
139
     *
140
     * @return bool
141
     */
142
    public function hasValidator($name)
143
    {
144
        $name = $this->filterString($name);
145
146
        return isset($this->validators[$name]);
147
    }
148
149
    /**
150
     * Remove all registered validators
151
     *
152
     * @return $this
153
     */
154
    public function clearValidators()
155
    {
156
        $this->validators = [];
157
158
        return $this;
159
    }
160
161
    /**
162
     * Format the given row
163
     *
164
     * @param array $row
165
     *
166
     * @return array
167
     */
168
    protected function formatRow(array $row)
169
    {
170
        foreach ($this->formatters as $formatter) {
171
            $row = call_user_func($formatter, $row);
172
        }
173
174
        return $row;
175
    }
176
177
    /**
178
    * Validate a row
179
    *
180
    * @param array $row
181
    *
182
    * @throws InvalidRowException If the validation failed
183
    */
184
    protected function validateRow(array $row)
185
    {
186
        foreach ($this->validators as $name => $validator) {
187
            if (true !== call_user_func($validator, $row)) {
188
                throw new InvalidRowException($name, $row, 'row validation failed');
189
            }
190
        }
191
    }
192
}
193