Completed
Push — master ( 70e5bf...357c5d )
by ignace nyamagana
02:35
created

ColumnConsistency::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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;
16
17
/**
18
 *  A class to manage column consistency on data insertion into a CSV
19
 *
20
 * @package League.csv
21
 * @since   7.0.0
22
 * @author  Ignace Nyamagana Butera <[email protected]>
23
 *
24
 */
25
class ColumnConsistency
26
{
27
    use ValidatorTrait;
28
29
    /**
30
     * The number of column per record
31
     *
32
     * @var int
33
     */
34
    protected $columns_count;
35
36
    /**
37
     * New Instance
38
     *
39
     * @param int $columns_count
40
     */
41 6
    public function __construct(int $columns_count = -1)
42
    {
43 6
        $this->columns_count = $this->filterMinRange($columns_count, -1, 'The column count must be greater or equal to -1');
44 4
    }
45
46
    /**
47
     * Returns the column count
48
     *
49
     * @return int
50
     */
51 2
    public function getColumnCount()
52
    {
53 2
        return $this->columns_count;
54
    }
55
56
    /**
57
     * Tell whether the submitted record is valid
58
     *
59
     * @param array $record
60
     *
61
     * @return bool
62
     */
63 4
    public function __invoke(array $record): bool
64
    {
65 4
        $count = count($record);
66 4
        if (-1 === $this->columns_count) {
67 2
            $this->columns_count = $count;
68
69 2
            return true;
70
        }
71
72 4
        return $count === $this->columns_count;
73
    }
74
}
75