ColumnConsistencyValidator   A
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 1
Dependencies 1

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnsCount() 0 4 1
A setColumnsCount() 0 5 1
A autodetectColumnsCount() 0 4 1
A __invoke() 0 14 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 League\Csv\ValidatorTrait;
18
19
/**
20
 *  A class to manage column consistency on data insertion into a CSV
21
 *
22
 * @package League.csv
23
 * @since   7.0.0
24
 * @author  Ignace Nyamagana Butera <[email protected]>
25
 *
26
 */
27
class ColumnConsistencyValidator
28
{
29
    use ValidatorTrait;
30
31
    /**
32
     * The number of column per row
33
     *
34
     * @var int
35
     */
36
    protected $columns_count = -1;
37
38
    /**
39
     * should the class detect the column count based the inserted row
40
     *
41
     * @var bool
42
     */
43
    protected $detect_columns_count = false;
44
45
    /**
46
     * Set Inserted row column count
47
     *
48
     * @param int $value
49
     */
50 6
    public function setColumnsCount(int $value)
51
    {
52 6
        $this->detect_columns_count = false;
53 6
        $this->columns_count = $this->filterMinRange($value, -1, __METHOD__.': the column count must be greater or equal to -1');
54 6
    }
55
56
    /**
57
     * Column count getter
58
     *
59
     * @return int
60
     */
61 4
    public function getColumnsCount(): int
62
    {
63 4
        return $this->columns_count;
64
    }
65
66
    /**
67
     * The method will set the $columns_count property according to the next inserted record
68
     * and therefore will also validate the next line whatever length it has no matter
69
     * the current $columns_count property value.
70
     */
71 2
    public function autodetectColumnsCount()
72
    {
73 2
        $this->detect_columns_count = true;
74 2
    }
75
76
    /**
77
     * Is the submitted row valid
78
     *
79
     * @param array $record
80
     *
81
     * @return bool
82
     */
83 4
    public function __invoke(array $record): bool
84
    {
85 4
        if ($this->detect_columns_count) {
86 2
            $this->setColumnsCount(count($record));
87
88 2
            return true;
89
        }
90
91 4
        if (-1 === $this->columns_count) {
92 2
            return true;
93
        }
94
95 4
        return count($record) === $this->columns_count;
96
    }
97
}
98