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

RowFilter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 2
cbo 1
dl 0
loc 159
rs 10

10 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
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.1.1
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);
0 ignored issues
show
Bug introduced by
It seems like validateString() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
104
105
        $this->validators[$name] = $callable;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Remove a validator from the collection
112
     *
113
     * @param string $name the validator name
114
     *
115
     * @return $this
116
     */
117
    public function removeValidator($name)
118
    {
119
        $name = $this->validateString($name);
0 ignored issues
show
Bug introduced by
It seems like validateString() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
120
        unset($this->validators[$name]);
121
122
        return $this;
123
    }
124
125
    /**
126
     * Detect if a validator is already registered
127
     *
128
     * @param string $name the validator name
129
     *
130
     * @return bool
131
     */
132
    public function hasValidator($name)
133
    {
134
        $name = $this->validateString($name);
0 ignored issues
show
Bug introduced by
It seems like validateString() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
135
136
        return isset($this->validators[$name]);
137
    }
138
139
    /**
140
     * Remove all registered validators
141
     *
142
     * @return $this
143
     */
144
    public function clearValidators()
145
    {
146
        $this->validators = [];
147
148
        return $this;
149
    }
150
151
    /**
152
     * Format the given row
153
     *
154
     * @param array $row
155
     *
156
     * @return array
157
     */
158
    protected function formatRow(array $row)
159
    {
160
        foreach ($this->formatters as $formatter) {
161
            $row = call_user_func($formatter, $row);
162
        }
163
164
        return $row;
165
    }
166
167
    /**
168
    * Validate a row
169
    *
170
    * @param array $row
171
    *
172
    * @throws InvalidRowException If the validation failed
173
    */
174
    protected function validateRow(array $row)
175
    {
176
        foreach ($this->validators as $name => $validator) {
177
            if (true !== call_user_func($validator, $row)) {
178
                throw new InvalidRowException($name, $row, 'row validation failed');
179
            }
180
        }
181
    }
182
}
183