forked from GwendolenLynch/msgphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomainIdentityHelper.php
More file actions
145 lines (114 loc) · 3.47 KB
/
Copy pathDomainIdentityHelper.php
File metadata and controls
145 lines (114 loc) · 3.47 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace MsgPhp\Domain;
use MsgPhp\Domain\Exception\InvalidClassException;
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class DomainIdentityHelper
{
private $mapping;
public function __construct(DomainIdentityMappingInterface $mapping)
{
$this->mapping = $mapping;
}
public function isIdentifier($value): bool
{
if ($value instanceof DomainIdInterface) {
return true;
}
if (\is_object($value)) {
try {
$this->mapping->getIdentifierFieldNames(\get_class($value));
return true;
} catch (InvalidClassException $e) {
}
}
return false;
}
public function isEmptyIdentifier($value): bool
{
if (null === $value) {
return true;
}
if ($value instanceof DomainIdInterface) {
return $value->isEmpty();
}
if (\is_object($value)) {
try {
$identity = $this->mapping->getIdentity($value);
} catch (InvalidClassException $e) {
return false;
}
return !$this->isIdentity(\get_class($value), $identity);
}
return false;
}
public function normalizeIdentifier($value)
{
if ($value instanceof DomainIdInterface) {
return $value->isEmpty() ? null : $value->toString();
}
if (\is_object($value)) {
try {
$identity = $this->mapping->getIdentity($value);
} catch (InvalidClassException $e) {
return $value;
}
$identity = array_map(function ($id) {
return $this->normalizeIdentifier($id);
}, $identity);
return 1 === \count($this->mapping->getIdentifierFieldNames(\get_class($value))) ? reset($identity) : $identity;
}
return $value;
}
/**
* @param object $object
*/
public function getIdentifiers($object): array
{
return null === ($identity = $this->mapping->getIdentity($object)) ? [] : array_values($identity);
}
/**
* @return string[]
*/
public function getIdentifierFieldNames(string $class): array
{
return $this->mapping->getIdentifierFieldNames($class);
}
public function isIdentity(string $class, $value): bool
{
if (null === $value || [] === $value) {
return false;
}
$fields = $this->mapping->getIdentifierFieldNames($class);
$count = \count($fields);
if (\is_array($value)) {
if (\count($value) !== $count) {
return false;
}
if (($oldValue = $value) !== $value = array_filter($value, function ($value): bool {
return !$this->isEmptyIdentifier($value);
})) {
return false;
}
return !array_diff_key($value, array_flip($fields));
}
return 1 === $count && !$this->isEmptyIdentifier($value);
}
public function toIdentity(string $class, $value): array
{
if (\is_array($value)) {
return $value;
}
$fields = $this->mapping->getIdentifierFieldNames($class);
return [reset($fields) => $value];
}
/**
* @param object $object
*/
public function getIdentity($object): array
{
return $this->mapping->getIdentity($object);
}
}