|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogosPopulate\Database; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayIterator; |
|
8
|
|
|
use Countable; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use IteratorAggregate; |
|
11
|
|
|
use OutOfBoundsException; |
|
12
|
|
|
use UnexpectedValueException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @implements IteratorAggregate<DataFieldInterface> |
|
16
|
|
|
*/ |
|
17
|
|
|
class DataFields implements Countable, IteratorAggregate |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var array<string, DataFieldInterface> */ |
|
20
|
|
|
private array $map = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param DataFieldInterface[] $dataFields |
|
24
|
|
|
*/ |
|
25
|
403 |
|
public function __construct(array $dataFields) |
|
26
|
|
|
{ |
|
27
|
403 |
|
foreach ($dataFields as $dataField) { |
|
28
|
400 |
|
if (! $dataField instanceof DataFieldInterface) { |
|
29
|
1 |
|
throw new InvalidArgumentException('There is a datafield with invalid type'); |
|
30
|
|
|
} |
|
31
|
399 |
|
$this->map[$dataField->name()] = $dataField; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return string[] |
|
37
|
|
|
*/ |
|
38
|
166 |
|
public function keys(): array |
|
39
|
|
|
{ |
|
40
|
166 |
|
return array_keys($this->map); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
340 |
|
public function exists(string $key): bool |
|
44
|
|
|
{ |
|
45
|
340 |
|
return array_key_exists($key, $this->map); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
166 |
|
public function get(string $key): DataFieldInterface |
|
49
|
|
|
{ |
|
50
|
166 |
|
if (! array_key_exists($key, $this->map)) { |
|
51
|
1 |
|
throw new UnexpectedValueException("The data field with name $key does not exists"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
165 |
|
return $this->map[$key]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param array<int, scalar> $input |
|
59
|
|
|
* @return array<string, scalar> |
|
60
|
|
|
*/ |
|
61
|
219 |
|
public function transform(array $input): array |
|
62
|
|
|
{ |
|
63
|
219 |
|
$data = []; |
|
64
|
219 |
|
$index = 0; |
|
65
|
219 |
|
foreach ($this->map as $name => $dataField) { |
|
66
|
219 |
|
$data[$name] = $dataField->transform($input[$index] ?? ''); |
|
67
|
219 |
|
$index = $index + 1; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
215 |
|
return $data; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
321 |
|
public function getByPosition(int $position): DataFieldInterface |
|
74
|
|
|
{ |
|
75
|
321 |
|
$mapByPosition = array_values($this->map); |
|
76
|
321 |
|
if (! isset($mapByPosition[$position])) { |
|
77
|
|
|
throw new OutOfBoundsException("Position $position not found"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
321 |
|
return $mapByPosition[$position]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @return ArrayIterator<string, DataFieldInterface> */ |
|
84
|
21 |
|
public function getIterator(): ArrayIterator |
|
85
|
|
|
{ |
|
86
|
21 |
|
return new ArrayIterator($this->map); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
400 |
|
public function count(): int |
|
90
|
|
|
{ |
|
91
|
400 |
|
return count($this->map); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|