-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPrepareEntityArray.php
More file actions
159 lines (126 loc) · 4.57 KB
/
PrepareEntityArray.php
File metadata and controls
159 lines (126 loc) · 4.57 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php declare(strict_types = 1);
namespace Spameri\Elastic\Model\Insert;
class PrepareEntityArray
{
public const ENTITY_CLASS = 'entityClass';
/**
* @var array<string, bool>
*/
private array $insertedEntities;
public function __construct(
private readonly \Spameri\Elastic\Model\ServiceLocatorInterface $serviceLocator,
)
{
}
/**
* @return array<mixed>
*/
public function prepare(
\Spameri\Elastic\Entity\ElasticEntityInterface $entity,
bool $hasSti = false,
): array
{
$this->insertedEntities = [];
$this->insertedEntities[$entity->id()->value()] = true;
$entityVariables = $entity->entityVariables();
if ($hasSti === true) {
$entityVariables[self::ENTITY_CLASS] = $entity::class;
}
return $this->iterateVariables($entityVariables);
}
/**
* @param array<mixed> $variables
* @return array<mixed>
*/
public function iterateVariables(
array $variables,
): array
{
$preparedArray = [];
foreach ($variables as $key => $property) {
if ($property instanceof \Spameri\Elastic\Entity\AbstractElasticEntity) {
if (\in_array($property->id->value(), $this->insertedEntities, true)) {
$preparedArray[$key] = $property->id()->value();
} else {
$preparedArray[$key] = $this->serviceLocator->locate($property)->insert($property);
$this->insertedEntities[$property->id()->value()] = true;
}
} elseif ($property instanceof \Spameri\Elastic\Entity\ElasticEntityInterface) {
throw new \Spameri\Elastic\Exception\DocumentInsertFailed(
'Entity ' . $property::class . ' must be extend AbstractElasticEntity.',
);
} elseif ($property instanceof \Spameri\Elastic\Entity\EntityInterface) {
$preparedArray[$key] = $this->iterateVariables($property->entityVariables());
} elseif ($property instanceof \Spameri\Elastic\Entity\ValueInterface) {
$preparedArray[$key] = $property->value();
} elseif ($property instanceof \Spameri\Elastic\Entity\Collection\STIEntityCollection) {
$preparedArray[$key] = [];
foreach ($property as $item) {
$iterateVariables = $this->iterateVariables($item->entityVariables());
$iterateVariables[self::ENTITY_CLASS] = $item::class;
$preparedArray[$key][] = $iterateVariables;
}
} elseif ($property instanceof \Spameri\Elastic\Entity\EntityCollectionInterface) {
$preparedArray[$key] = [];
/** @var \Spameri\Elastic\Entity\EntityInterface $item */
foreach ($property as $item) {
$preparedArray[$key][] = $this->iterateVariables($item->entityVariables());
}
// TODO kolekce bez klíčů
} elseif ($property instanceof \Spameri\Elastic\Entity\ElasticEntityCollectionInterface) {
$preparedArray[$key] = [];
if ( ! $property->initialized()) {
$preparedArray[$key] = $property->elasticIds();
} else {
/** @var \Spameri\Elastic\Entity\AbstractElasticEntity $item */
foreach ($property as $item) {
if (\in_array($item->id()->value(), $this->insertedEntities)) {
$preparedArray[$key][] = $item->id()->value();
} else {
$preparedArray[$key][] = $this->serviceLocator->locate($item)->insert($item);
$this->insertedEntities[$item->id()->value()] = true;
}
}
}
} elseif ($property instanceof \Spameri\Elastic\Entity\ValueCollectionInterface) {
$preparedArray[$key] = [];
/** @var \Spameri\Elastic\Entity\ValueInterface|mixed $value */
foreach ($property as $value) {
if ($value instanceof \Spameri\Elastic\Entity\ValueInterface) {
$preparedArray[$key][] = $value->value();
} else {
$preparedArray[$key][] = $value;
}
}
} elseif (
\is_string($property)
|| \is_int($property)
|| \is_bool($property)
|| \is_float($property)
|| $property === null
) {
$preparedArray[$key] = $property;
} elseif (\is_array($property)) {
$preparedArray[$key] = $this->iterateVariables($property);
} elseif ($property instanceof \Spameri\Elastic\Entity\DateTimeInterface) {
$preparedArray[$key] = $property->format();
} elseif ($property instanceof \DateTime) {
$preparedArray[$key] = $property->format(\Spameri\Elastic\Entity\Property\DateTime::FORMAT);
} elseif (
$property instanceof \BackedEnum
) {
$preparedArray[$key] = $property->value;
} else {
if (\is_object($property)) {
throw new \Spameri\Elastic\Exception\EntityIsNotValid(
'Property ' . $key . ' in ' . $property::class . ' is not supported.',
);
}
throw new \Spameri\Elastic\Exception\EntityIsNotValid(
'Property ' . $key . ' with value' . $property . ' is not supported.',
);
}
}
return $preparedArray;
}
}