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

RowFilter::validateString()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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