1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | namespace NxSys\Library\Clients\Brex\API\Transactions\Normalizer; |
12: | |
13: | use Jane\Component\JsonSchemaRuntime\Reference; |
14: | use NxSys\Library\Clients\Brex\API\Transactions\Runtime\Normalizer\CheckArray; |
15: | use NxSys\Library\Clients\Brex\API\Transactions\Runtime\Normalizer\ValidatorTrait; |
16: | use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
17: | use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
18: | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
19: | use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
20: | use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
21: | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
22: | |
23: | class CashTransactionNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface |
24: | { |
25: | use DenormalizerAwareTrait; |
26: | use NormalizerAwareTrait; |
27: | use CheckArray; |
28: | use ValidatorTrait; |
29: | |
30: | public function supportsDenormalization($data, $type, $format = null): bool |
31: | { |
32: | return $type === 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\CashTransaction'; |
33: | } |
34: | |
35: | public function supportsNormalization($data, $format = null): bool |
36: | { |
37: | return is_object($data) && get_class($data) === 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\CashTransaction'; |
38: | } |
39: | |
40: | |
41: | |
42: | |
43: | public function denormalize($data, $class, $format = null, array $context = []) |
44: | { |
45: | if (isset($data['$ref'])) { |
46: | return new Reference($data['$ref'], $context['document-origin']); |
47: | } |
48: | if (isset($data['$recursiveRef'])) { |
49: | return new Reference($data['$recursiveRef'], $context['document-origin']); |
50: | } |
51: | $object = new \NxSys\Library\Clients\Brex\API\Transactions\Model\CashTransaction(); |
52: | if (null === $data || false === \is_array($data)) { |
53: | return $object; |
54: | } |
55: | if (\array_key_exists('id', $data)) { |
56: | $object->setId($data['id']); |
57: | unset($data['id']); |
58: | } |
59: | if (\array_key_exists('description', $data)) { |
60: | $object->setDescription($data['description']); |
61: | unset($data['description']); |
62: | } |
63: | if (\array_key_exists('amount', $data)) { |
64: | $object->setAmount($this->denormalizer->denormalize($data['amount'], 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\CashTransactionAmount', 'json', $context)); |
65: | unset($data['amount']); |
66: | } |
67: | if (\array_key_exists('initiated_at_date', $data)) { |
68: | $object->setInitiatedAtDate(\DateTime::createFromFormat('Y-m-d', $data['initiated_at_date'])->setTime(0, 0, 0)); |
69: | unset($data['initiated_at_date']); |
70: | } |
71: | if (\array_key_exists('posted_at_date', $data)) { |
72: | $object->setPostedAtDate(\DateTime::createFromFormat('Y-m-d', $data['posted_at_date'])->setTime(0, 0, 0)); |
73: | unset($data['posted_at_date']); |
74: | } |
75: | if (\array_key_exists('type', $data)) { |
76: | $object->setType($data['type']); |
77: | unset($data['type']); |
78: | } |
79: | if (\array_key_exists('transfer_id', $data) && $data['transfer_id'] !== null) { |
80: | $object->setTransferId($data['transfer_id']); |
81: | unset($data['transfer_id']); |
82: | } elseif (\array_key_exists('transfer_id', $data) && $data['transfer_id'] === null) { |
83: | $object->setTransferId(null); |
84: | } |
85: | foreach ($data as $key => $value) { |
86: | if (preg_match('/.*/', (string) $key)) { |
87: | $object[$key] = $value; |
88: | } |
89: | } |
90: | |
91: | return $object; |
92: | } |
93: | |
94: | |
95: | |
96: | |
97: | public function normalize($object, $format = null, array $context = []) |
98: | { |
99: | $data = []; |
100: | $data['id'] = $object->getId(); |
101: | $data['description'] = $object->getDescription(); |
102: | if ($object->isInitialized('amount') && null !== $object->getAmount()) { |
103: | $data['amount'] = $this->normalizer->normalize($object->getAmount(), 'json', $context); |
104: | } |
105: | $data['initiated_at_date'] = $object->getInitiatedAtDate()->format('Y-m-d'); |
106: | $data['posted_at_date'] = $object->getPostedAtDate()->format('Y-m-d'); |
107: | if ($object->isInitialized('type') && null !== $object->getType()) { |
108: | $data['type'] = $object->getType(); |
109: | } |
110: | if ($object->isInitialized('transferId') && null !== $object->getTransferId()) { |
111: | $data['transfer_id'] = $object->getTransferId(); |
112: | } |
113: | foreach ($object as $key => $value) { |
114: | if (preg_match('/.*/', (string) $key)) { |
115: | $data[$key] = $value; |
116: | } |
117: | } |
118: | |
119: | return $data; |
120: | } |
121: | } |
122: | |