Repository navigation
Expand file tree
/
Copy pathNestedNode.php
More file actions
68 lines (51 loc) · 1.72 KB
/
Copy pathNestedNode.php
File metadata and controls
68 lines (51 loc) · 1.72 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
<?php
declare(strict_types=1);
namespace TinyBlocks\Mapper\Internal\Mappings\Layout;
use TinyBlocks\Mapper\Internal\Context;
use TinyBlocks\Mapper\Internal\Deserialization\ValueReader;
final readonly class NestedNode implements LayoutNode
{
private array $children;
public function __construct(LayoutChild ...$children)
{
$this->children = $children;
}
public function read(array $row): array
{
$nested = [];
foreach ($this->children as $child) {
$nested[$child->sourceKey] = $child->node->read(row: $row);
}
return $nested;
}
public function write(mixed $value, Context $context): array
{
$flat = [];
foreach ($this->children as $child) {
$property = $child->property;
$extracted = $property->isInitialized($value) ? $property->getValue($value) : null;
foreach ($child->node->write(value: $extracted, context: $context) as $key => $entry) {
$flat[$key] = $entry;
}
}
return $flat;
}
public function arguments(array $row, ValueReader $valueReader): array
{
$arguments = [];
foreach ($this->children as $child) {
if (!$child->node->isPresentIn(row: $row)) {
continue;
}
$arguments[$child->property->getName()] = $valueReader->resolve(
type: $child->property->getType(),
value: $child->node->read(row: $row)
);
}
return $arguments;
}
public function isPresentIn(array $row): bool
{
return array_any($this->children, static fn(LayoutChild $child): bool => $child->node->isPresentIn(row: $row));
}
}