DataTable::fields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Database;
6
7
use LogicException;
8
9
class DataTable
10
{
11
    private readonly string $name;
12
13
    private readonly DataFields $fields;
14
15
    /** @var string[] */
16
    private readonly array $primaryKey;
17
18
    /**
19
     * @param string[] $primaryKey
20
     */
21 398
    public function __construct(
22
        string $name,
23
        DataFields $fields,
24
        array $primaryKey = [],
25
        bool $withoutPrimaryKey = false
26
    ) {
27 398
        if ('' === $name) {
28
            throw new LogicException('The table name must not be empty');
29
        }
30 398
        if (0 === count($fields)) {
31 1
            throw new LogicException('The data fields map must not be empty');
32
        }
33 397
        $primaryKey = array_unique(array_filter(array_map('trim', $primaryKey)));
34 397
        if (0 === count($primaryKey) && ! $withoutPrimaryKey) {
35 321
            $primaryKey = [$fields->getByPosition(0)->name()];
36
        }
37 397
        foreach ($primaryKey as $primaryKeyName) {
38 340
            if (! $fields->exists($primaryKeyName)) {
39
                throw new LogicException('The primary key is not found in the data fields map');
40
            }
41
        }
42
43 397
        $this->name = $name;
0 ignored issues
show
Bug introduced by
The property name is declared read-only in PhpCfdi\SatCatalogosPopulate\Database\DataTable.
Loading history...
44 397
        $this->fields = $fields;
0 ignored issues
show
Bug introduced by
The property fields is declared read-only in PhpCfdi\SatCatalogosPopulate\Database\DataTable.
Loading history...
45 397
        $this->primaryKey = $primaryKey;
0 ignored issues
show
Bug introduced by
The property primaryKey is declared read-only in PhpCfdi\SatCatalogosPopulate\Database\DataTable.
Loading history...
46
    }
47
48 186
    public function name(): string
49
    {
50 186
        return $this->name;
51
    }
52
53 394
    public function fields(): DataFields
54
    {
55 394
        return $this->fields;
56
    }
57
58
    /** @return string[] */
59 159
    public function primaryKey(): array
60
    {
61 159
        return $this->primaryKey;
62
    }
63
}
64