-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate.php
More file actions
86 lines (80 loc) · 2.18 KB
/
Update.php
File metadata and controls
86 lines (80 loc) · 2.18 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
<?php
declare(strict_types = 1);
namespace Formal\ORM\Adapter\SQL;
use Formal\ORM\Raw\Diff;
use Formal\AccessLayer\Query;
use Innmind\Immutable\Sequence;
/**
* @internal
* @psalm-immutable
* @template T of object
*/
final class Update
{
/**
* @param MainTable<T> $mainTable
*/
private function __construct(private MainTable $mainTable)
{
}
/**
* @return Sequence<Query>
*/
public function __invoke(Diff $data): Sequence
{
$mainTable = $this->mainTable;
$main = $this->mainTable->update($data)->toSequence();
$entities = $data
->entities()
->flatMap(
static fn($entity) => $mainTable
->entity($entity->name())
->flatMap(
static fn($table) => $table->update(
$data->id(),
$entity->properties(),
),
)
->toSequence(),
);
$optionals = $data
->optionals()
->flatMap(
static fn($optional) => $mainTable
->optional($optional->name())
->toSequence()
->flatMap(static fn($table) => $table->update(
$data->id(),
$optional,
)),
);
$collections = $data
->collections()
->flatMap(
static fn($collection) => $mainTable
->collection($collection->name())
->toSequence()
->flatMap(static fn($table) => $table->update(
$data->id(),
$collection->entities(),
)),
);
return $main
->append($entities)
->append($optionals)
->append($collections);
}
/**
* @internal
* @psalm-pure
* @template A of object
*
* @param MainTable<A> $mainTable
*
* @return self<A>
*/
public static function of(MainTable $mainTable): self
{
return new self($mainTable);
}
}