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 CardTransactionNormalizer 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\\CardTransaction'; |
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\\CardTransaction'; |
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\CardTransaction(); |
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('card_id', $data) && $data['card_id'] !== null) { |
60: | $object->setCardId($data['card_id']); |
61: | unset($data['card_id']); |
62: | } elseif (\array_key_exists('card_id', $data) && $data['card_id'] === null) { |
63: | $object->setCardId(null); |
64: | } |
65: | if (\array_key_exists('description', $data)) { |
66: | $object->setDescription($data['description']); |
67: | unset($data['description']); |
68: | } |
69: | if (\array_key_exists('amount', $data)) { |
70: | $object->setAmount($this->denormalizer->denormalize($data['amount'], 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\Money', 'json', $context)); |
71: | unset($data['amount']); |
72: | } |
73: | if (\array_key_exists('initiated_at_date', $data)) { |
74: | $object->setInitiatedAtDate(\DateTime::createFromFormat('Y-m-d', $data['initiated_at_date'])->setTime(0, 0, 0)); |
75: | unset($data['initiated_at_date']); |
76: | } |
77: | if (\array_key_exists('posted_at_date', $data)) { |
78: | $object->setPostedAtDate(\DateTime::createFromFormat('Y-m-d', $data['posted_at_date'])->setTime(0, 0, 0)); |
79: | unset($data['posted_at_date']); |
80: | } |
81: | if (\array_key_exists('type', $data)) { |
82: | $object->setType($data['type']); |
83: | unset($data['type']); |
84: | } |
85: | if (\array_key_exists('merchant', $data)) { |
86: | $object->setMerchant($this->denormalizer->denormalize($data['merchant'], 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\CardTransactionMerchant', 'json', $context)); |
87: | unset($data['merchant']); |
88: | } |
89: | if (\array_key_exists('card_metadata', $data) && $data['card_metadata'] !== null) { |
90: | $values = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS); |
91: | foreach ($data['card_metadata'] as $key => $value) { |
92: | $values[$key] = $value; |
93: | } |
94: | $object->setCardMetadata($values); |
95: | unset($data['card_metadata']); |
96: | } elseif (\array_key_exists('card_metadata', $data) && $data['card_metadata'] === null) { |
97: | $object->setCardMetadata(null); |
98: | } |
99: | if (\array_key_exists('expense_id', $data) && $data['expense_id'] !== null) { |
100: | $object->setExpenseId($data['expense_id']); |
101: | unset($data['expense_id']); |
102: | } elseif (\array_key_exists('expense_id', $data) && $data['expense_id'] === null) { |
103: | $object->setExpenseId(null); |
104: | } |
105: | foreach ($data as $key_1 => $value_1) { |
106: | if (preg_match('/.*/', (string) $key_1)) { |
107: | $object[$key_1] = $value_1; |
108: | } |
109: | } |
110: | |
111: | return $object; |
112: | } |
113: | |
114: | |
115: | |
116: | |
117: | public function normalize($object, $format = null, array $context = []) |
118: | { |
119: | $data = []; |
120: | $data['id'] = $object->getId(); |
121: | if ($object->isInitialized('cardId') && null !== $object->getCardId()) { |
122: | $data['card_id'] = $object->getCardId(); |
123: | } |
124: | $data['description'] = $object->getDescription(); |
125: | $data['amount'] = $this->normalizer->normalize($object->getAmount(), 'json', $context); |
126: | $data['initiated_at_date'] = $object->getInitiatedAtDate()->format('Y-m-d'); |
127: | $data['posted_at_date'] = $object->getPostedAtDate()->format('Y-m-d'); |
128: | if ($object->isInitialized('type') && null !== $object->getType()) { |
129: | $data['type'] = $object->getType(); |
130: | } |
131: | if ($object->isInitialized('merchant') && null !== $object->getMerchant()) { |
132: | $data['merchant'] = $this->normalizer->normalize($object->getMerchant(), 'json', $context); |
133: | } |
134: | if ($object->isInitialized('cardMetadata') && null !== $object->getCardMetadata()) { |
135: | $values = []; |
136: | foreach ($object->getCardMetadata() as $key => $value) { |
137: | $values[$key] = $value; |
138: | } |
139: | $data['card_metadata'] = $values; |
140: | } |
141: | if ($object->isInitialized('expenseId') && null !== $object->getExpenseId()) { |
142: | $data['expense_id'] = $object->getExpenseId(); |
143: | } |
144: | foreach ($object as $key_1 => $value_1) { |
145: | if (preg_match('/.*/', (string) $key_1)) { |
146: | $data[$key_1] = $value_1; |
147: | } |
148: | } |
149: | |
150: | return $data; |
151: | } |
152: | } |
153: | |