forked from flow-php/etl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionEntry.php
More file actions
127 lines (106 loc) · 3.05 KB
/
CollectionEntry.php
File metadata and controls
127 lines (106 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace Flow\ETL\Row\Entry;
use Flow\ArrayComparison\ArrayWeakComparison;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Row\Entries;
use Flow\ETL\Row\Entry;
/**
* @psalm-immutable
*/
final class CollectionEntry implements Entry
{
private string $key;
private string $name;
/**
* @var Entries[]
*/
private array $entries;
public function __construct(string $name, Entries ...$entries)
{
if (empty($name)) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}
$this->key = \mb_strtolower($name);
$this->name = $name;
$this->entries = $entries;
}
public function append(Entries $entries) : self
{
return new self($this->name, ...[...$this->entries, $entries]);
}
/**
* @psalm-suppress MissingClosureReturnType
* @psalm-suppress ImpureMethodCall
*/
public function entryFromAll(string $name) : Entry
{
$entries = \array_unique($this->mapEntries(fn (Entries $entries) => $entries->get($name)->value()));
if (\count($entries) !== 1) {
throw InvalidArgumentException::because(
\sprintf(
'Entry "%s" has different values in "%s" collection entry: [%s]',
$name,
$this->name,
\implode(', ', $entries)
)
);
}
return \current($this->entries)->get($name);
}
/**
* @psalm-suppress MixedArgument
*/
public function removeFromAll(string $name) : self
{
return new self(
$this->name,
...$this->mapEntries(fn (Entries $entries) : Entries => $entries->remove($name))
);
}
public function name() : string
{
return $this->name;
}
/**
* @psalm-suppress MissingReturnType
* @phpstan-ignore-next-line
*/
public function value() : array
{
return $this->mapEntries(fn (Entries $entries) : array => $entries->toArray());
}
public function is(string $name) : bool
{
return $this->key === \mb_strtolower($name);
}
public function rename(string $name) : Entry
{
return new self($name, ...$this->entries);
}
/**
* @psalm-param callable(Entries) : bool $filter
*/
public function filterEntries(callable $filter) : self
{
return new self($this->name, ...\array_filter($this->entries, $filter));
}
/**
* @psalm-suppress MixedArgument
*/
public function map(callable $mapper) : Entry
{
return new self($this->name, ...$mapper($this->entries));
}
public function isEqual(Entry $entry) : bool
{
return $this->is($entry->name()) && $entry instanceof self && (new ArrayWeakComparison())->equals($this->value(), $entry->value());
}
/**
* @phpstan-ignore-next-line
*/
private function mapEntries(callable $callable) : array
{
return \array_map($callable, $this->entries);
}
}