-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClickHouseEntityNormalizer.php
62 lines (54 loc) · 1.72 KB
/
ClickHouseEntityNormalizer.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
<?php
namespace Borislav\Clickhouse\Serializer\Normalizer;
use Borislav\Clickhouse\Entity\Interfaces\ClickHouseEntityInterface;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
class ClickHouseEntityNormalizer implements ContextAwareNormalizerInterface
{
public function __construct(
private ObjectNormalizer $normalizer
) {}
/**
* @param $data
* @param string|null $format
* @param array $context
*
* @return bool
*/
public function supportsNormalization($data, string $format = null, array $context = [])
{
if (is_object($data) || (is_string($data) && class_exists($data))) {
if ($classes = class_implements($data)) {
return in_array(ClickHouseEntityInterface::class, $classes);
}
}
return false;
}
/**
* @param ClickHouseEntityInterface $object
* @param string|null $format
* @param array $context
*
* @return array|\ArrayObject|bool|float|int|mixed|string|null
*
* @throws ExceptionInterface
*/
public function normalize($object, string $format = null, array $context = [])
{
$data = $this->normalizer->normalize($object);
$mergeData = [];
if (array_key_exists('from', $data)) {
$mergeData['from'] = $object->getFrom()->format('Y-m-d H:i:s');
}
if (array_key_exists('to', $data)) {
$mergeData['to'] = $object->getTo()->format('Y-m-d H:i:s');
}
if (is_null($object->getId())) {
$mergeData['id'] = Uuid::uuid4();
}
$mergeData['hasBeenCalculated'] = (int) $object->isHasBeenCalculated();
return array_merge($data, $mergeData);
}
}