-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimaryKeySharingMappingStruct.php
122 lines (102 loc) · 3.59 KB
/
PrimaryKeySharingMappingStruct.php
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
<?php
declare(strict_types=1);
namespace Heptacom\HeptaConnect\Storage\Base;
use Heptacom\HeptaConnect\Dataset\Base\Contract\AttachableInterface;
use Heptacom\HeptaConnect\Dataset\Base\Contract\AttachmentAwareInterface;
use Heptacom\HeptaConnect\Dataset\Base\Contract\DatasetEntityContract;
use Heptacom\HeptaConnect\Dataset\Base\Contract\ForeignKeyAwareInterface;
use Heptacom\HeptaConnect\Dataset\Base\EntityType;
use Heptacom\HeptaConnect\Dataset\Base\Support\AttachmentAwareTrait;
use Heptacom\HeptaConnect\Dataset\Base\Support\ForeignKeyTrait;
use Heptacom\HeptaConnect\Portal\Base\Mapping\Contract\MappingInterface;
use Heptacom\HeptaConnect\Portal\Base\StorageKey\Contract\MappingNodeKeyInterface;
use Heptacom\HeptaConnect\Portal\Base\StorageKey\Contract\PortalNodeKeyInterface;
use Heptacom\HeptaConnect\Storage\Base\Exception\UnsharableOwnerException;
final class PrimaryKeySharingMappingStruct implements AttachmentAwareInterface, AttachableInterface, ForeignKeyAwareInterface, MappingInterface
{
use AttachmentAwareTrait;
use ForeignKeyTrait;
/**
* @var class-string<DatasetEntityContract>
*/
private string $entityType;
/**
* @var DatasetEntityContract[]
*/
private array $owners = [];
public function __construct(
EntityType $entityType,
private ?string $externalId,
private PortalNodeKeyInterface $portalNodeKey,
private MappingNodeKeyInterface $mappingNodeKey
) {
$this->entityType = (string) $entityType;
}
public function __wakeup(): void
{
// construct for validation, but don't store to prevent serialization
// validation should always be true, as `unserialize` would fail when the class is not available
new EntityType($this->entityType);
}
public function getEntityType(): EntityType
{
/*
* We do not expect a throw here, because it has been validated in @see __construct, __wakeup
*/
return new EntityType($this->entityType);
}
public function getExternalId(): ?string
{
return $this->externalId;
}
public function setExternalId(?string $externalId): self
{
$this->externalId = $externalId;
return $this;
}
public function getPortalNodeKey(): PortalNodeKeyInterface
{
return $this->portalNodeKey;
}
public function getMappingNodeKey(): MappingNodeKeyInterface
{
return $this->mappingNodeKey;
}
public function getForeignEntityType(): EntityType
{
return $this->getEntityType();
}
public function setForeignKey(?string $foreignKey): void
{
$this->foreignKey = $foreignKey;
foreach ($this->getOwners() as $owner) {
if ($owner->getPrimaryKey() !== $foreignKey) {
$owner->setPrimaryKey($foreignKey);
}
}
}
/**
* @return iterable|DatasetEntityContract[]
*
* @psalm-return iterable<array-key, DatasetEntityContract>
*/
public function getOwners(): iterable
{
return $this->owners;
}
/**
* @throws UnsharableOwnerException
*/
public function addOwner(DatasetEntityContract $owner): void
{
if (
!$this->getForeignEntityType()->equalsObjectType($owner)
|| $owner->getPrimaryKey() !== $this->getForeignKey()
) {
throw new UnsharableOwnerException((string) $this->getForeignEntityType(), $this->getForeignKey(), $owner);
}
$this->owners[] = $owner;
$owner->attach($this);
$owner->setPrimaryKey($this->getForeignKey());
}
}