Completed
Pull Request — master (#210)
by ignace nyamagana
04:10
created

ColumnConsistencyValidator::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 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 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
declare(strict_types=1);
14
15
namespace League\Csv\Plugin;
16
17
use InvalidArgumentException;
18
use League\Csv\RecordValidatorInterface;
19
20
/**
21
 *  A class to manage column consistency on data insertion into a CSV
22
 *
23
 * @package League.csv
24
 * @since   7.0.0
25
 * @author  Ignace Nyamagana Butera <[email protected]>
26
 *
27
 */
28
class ColumnConsistencyValidator implements RecordValidatorInterface
29
{
30
    /**
31
     * The number of column per row
32
     *
33
     * @var int
34
     */
35
    protected $columns_count = -1;
36
37
    /**
38
     * should the class detect the column count based the inserted row
39
     *
40
     * @var bool
41
     */
42
    protected $detect_columns_count = false;
43
44
    /**
45
     * Set Inserted row column count
46
     *
47
     * @param int $value
48
     *
49
     * @throws InvalidArgumentException If $value is lesser than -1
50
     *
51
     */
52 4
    public function setColumnsCount(int $value)
53
    {
54 4
        if ($value < -1) {
55 2
            throw new InvalidArgumentException('the column count must an integer greater or equals to -1');
56
        }
57 4
        $this->detect_columns_count = false;
58 4
        $this->columns_count = $value;
59 4
    }
60
61
    /**
62
     * Column count getter
63
     *
64
     * @return int
65
     */
66 4
    public function getColumnsCount(): int
67
    {
68 4
        return $this->columns_count;
69
    }
70
71
    /**
72
     * The method will set the $columns_count property according to the next inserted row
73
     * and therefore will also validate the next line whatever length it has no matter
74
     * the current $columns_count property value.
75
     *
76
     */
77 2
    public function autodetectColumnsCount()
78
    {
79 2
        $this->detect_columns_count = true;
80 2
    }
81
82
    /**
83
     * Is the submitted row valid
84
     *
85
     * @param array $row
86
     *
87
     * @return bool
88
     */
89 4
    public function validate(array $row): bool
90
    {
91 4
        if ($this->detect_columns_count) {
92 2
            $this->columns_count = count($row);
93 2
            $this->detect_columns_count = false;
94
95 2
            return true;
96
        }
97
98 4
        if (-1 == $this->columns_count) {
99 2
            return true;
100
        }
101
102 4
        return count($row) === $this->columns_count;
103
    }
104
}
105