Completed
Push — master ( 8096c4...8558a8 )
by ignace nyamagana
13:21
created

RowFilter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 13
c 6
b 0
f 0
lcom 2
cbo 1
dl 0
loc 164
ccs 37
cts 37
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addFormatter() 0 6 1
A removeFormatter() 0 7 1
A hasFormatter() 0 4 1
A clearFormatters() 0 6 1
A addValidator() 0 8 1
validateString() 0 1 ?
A removeValidator() 0 7 1
A hasValidator() 0 6 1
A clearValidators() 0 6 1
A formatRow() 0 8 2
A validateRow() 0 8 3
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 8.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\Modifier;
14
15
use League\Csv\Exception\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 6
    public function addFormatter(callable $callable)
48
    {
49 6
        $this->formatters[] = $callable;
50
51 6
        return $this;
52
    }
53
54
    /**
55
     * Remove a formatter from the collection
56
     *
57
     * @param callable $callable
58
     *
59
     * @return $this
60
     */
61 3
    public function removeFormatter(callable $callable)
62
    {
63 3
        $res = array_search($callable, $this->formatters, true);
64 3
        unset($this->formatters[$res]);
65
66 3
        return $this;
67
    }
68
69
    /**
70
     * Detect if the formatter is already registered
71
     *
72
     * @param callable $callable
73
     *
74
     * @return bool
75
     */
76 3
    public function hasFormatter(callable $callable)
77
    {
78 3
        return false !== array_search($callable, $this->formatters, true);
79
    }
80
81
    /**
82
     * Remove all registered formatter
83
     *
84
     * @return $this
85
     */
86 3
    public function clearFormatters()
87
    {
88 3
        $this->formatters = [];
89
90 3
        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 12
    public function addValidator(callable $callable, $name)
102
    {
103 12
        $name = $this->validateString($name);
104
105 12
        $this->validators[$name] = $callable;
106
107 12
        return $this;
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    abstract protected function validateString($str);
114
115
    /**
116
     * Remove a validator from the collection
117
     *
118
     * @param string $name the validator name
119
     *
120
     * @return $this
121
     */
122 3
    public function removeValidator($name)
123
    {
124 3
        $name = $this->validateString($name);
125 3
        unset($this->validators[$name]);
126
127 3
        return $this;
128
    }
129
130
    /**
131
     * Detect if a validator is already registered
132
     *
133
     * @param string $name the validator name
134
     *
135
     * @return bool
136
     */
137 3
    public function hasValidator($name)
138
    {
139 3
        $name = $this->validateString($name);
140
141 3
        return isset($this->validators[$name]);
142
    }
143
144
    /**
145
     * Remove all registered validators
146
     *
147
     * @return $this
148
     */
149 3
    public function clearValidators()
150
    {
151 3
        $this->validators = [];
152
153 3
        return $this;
154
    }
155
156
    /**
157
     * Format the given row
158
     *
159
     * @param array $row
160
     *
161
     * @return array
162
     */
163 39
    protected function formatRow(array $row)
164
    {
165 39
        foreach ($this->formatters as $formatter) {
166 3
            $row = call_user_func($formatter, $row);
167 26
        }
168
169 39
        return $row;
170
    }
171
172
    /**
173
    * Validate a row
174
    *
175
    * @param array $row
176
    *
177
    * @throws InvalidRowException If the validation failed
178
    */
179 39
    protected function validateRow(array $row)
180
    {
181 39
        foreach ($this->validators as $name => $validator) {
182 9
            if (true !== call_user_func($validator, $row)) {
183 9
                throw new InvalidRowException($name, $row, 'row validation failed');
184
            }
185 24
        }
186 36
    }
187
}
188