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

ColumnConsistency   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getColumnCount() 0 4 1
A __invoke() 0 11 2
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