Completed
Push — master ( ecd0f8...3ad52b )
by ignace nyamagana
15:08
created

ColumnConsistency::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * League.Csv (https://csv.thephpleague.com).
5
 *
6
 * @author  Ignace Nyamagana Butera <[email protected]>
7
 * @license https://github.com/thephpleague/csv/blob/master/LICENSE (MIT License)
8
 * @version 9.1.5
9
 * @link    https://github.com/thephpleague/csv
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
declare(strict_types=1);
16
17
namespace League\Csv;
18
19
use function count;
20
use function sprintf;
21
22
/**
23
 * A class to validate column consistency when inserting records into a CSV document.
24
 *
25
 * @package League.csv
26
 * @since   7.0.0
27
 * @author  Ignace Nyamagana Butera <[email protected]>
28
 */
29
class ColumnConsistency
30
{
31
    /**
32
     * The number of column per record.
33
     *
34
     * @var int
35
     */
36
    protected $columns_count;
37
38
    /**
39
     * New Instance.
40 6
     *
41
     * @throws OutOfRangeException if the column count is lesser than -1
42 6
     */
43 2
    public function __construct(int $columns_count = -1)
44
    {
45
        if ($columns_count < -1) {
46 4
            throw new Exception(sprintf('%s() expects the column count to be greater or equal to -1 %s given', __METHOD__, $columns_count));
47 4
        }
48
49
        $this->columns_count = $columns_count;
50
    }
51
52
    /**
53
     * Returns the column count.
54 2
     */
55
    public function getColumnCount(): int
56 2
    {
57
        return $this->columns_count;
58
    }
59
60
    /**
61
     * Tell whether the submitted record is valid.
62
     */
63
    public function __invoke(array $record): bool
64
    {
65
        $count = count($record);
66 4
        if (-1 === $this->columns_count) {
67
            $this->columns_count = $count;
68 4
69 4
            return true;
70 2
        }
71
72 2
        return $count === $this->columns_count;
73
    }
74
}
75