| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* League.Csv (https://csv.thephpleague.com) |
| 5 |
* |
| 6 |
* (c) Ignace Nyamagana Butera <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace League\Csv; |
| 15 |
|
| 16 |
use function count; |
| 17 |
|
| 18 |
/** |
| 19 |
* Validates column consistency when inserting records into a CSV document. |
| 20 |
*/ |
| 21 |
class ColumnConsistency |
| 22 |
{ |
| 23 |
protected int $columns_count; |
| 24 |
|
| 25 |
/** |
| 26 |
* @throws InvalidArgument if the column count is lesser than -1 |
| 27 |
*/ |
| 28 |
public function __construct(int $columns_count = -1) |
| 29 |
{ |
| 30 |
if ($columns_count < -1) { |
| 31 |
throw InvalidArgument::dueToInvalidColumnCount($columns_count, __METHOD__); |
| 32 |
} |
| 33 |
|
| 34 |
$this->columns_count = $columns_count; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Returns the column count. |
| 39 |
*/ |
| 40 |
public function getColumnCount(): int |
| 41 |
{ |
| 42 |
return $this->columns_count; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Tell whether the submitted record is valid. |
| 47 |
*/ |
| 48 |
public function __invoke(array $record): bool |
| 49 |
{ |
| 50 |
$count = count($record); |
| 51 |
if (-1 === $this->columns_count) { |
| 52 |
$this->columns_count = $count; |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
return $count === $this->columns_count; |
| 58 |
} |
| 59 |
} |
| 60 |
|