1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | namespace NxSys\Library\Clients\Brex\API\Expenses\Normalizer; |
12: | |
13: | use Jane\Component\JsonSchemaRuntime\Reference; |
14: | use NxSys\Library\Clients\Brex\API\Expenses\Runtime\Normalizer\CheckArray; |
15: | use NxSys\Library\Clients\Brex\API\Expenses\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 ExpandableExpenseNormalizer 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\\Expenses\\Model\\ExpandableExpense'; |
33: | } |
34: | |
35: | public function supportsNormalization($data, $format = null): bool |
36: | { |
37: | return is_object($data) && get_class($data) === 'NxSys\\Library\\Clients\\Brex\\API\\Expenses\\Model\\ExpandableExpense'; |
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\Expenses\Model\ExpandableExpense(); |
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('memo', $data) && $data['memo'] !== null) { |
60: | $object->setMemo($data['memo']); |
61: | unset($data['memo']); |
62: | } elseif (\array_key_exists('memo', $data) && $data['memo'] === null) { |
63: | $object->setMemo(null); |
64: | } |
65: | if (\array_key_exists('location_id', $data) && $data['location_id'] !== null) { |
66: | $object->setLocationId($data['location_id']); |
67: | unset($data['location_id']); |
68: | } elseif (\array_key_exists('location_id', $data) && $data['location_id'] === null) { |
69: | $object->setLocationId(null); |
70: | } |
71: | if (\array_key_exists('location', $data)) { |
72: | $object->setLocation($this->denormalizer->denormalize($data['location'], 'NxSys\\Library\\Clients\\Brex\\API\\Expenses\\Model\\ExpandableExpenseLocation', 'json', $context)); |
73: | unset($data['location']); |
74: | } |
75: | if (\array_key_exists('department_id', $data) && $data['department_id'] !== null) { |
76: | $object->setDepartmentId($data['department_id']); |
77: | unset($data['department_id']); |
78: | } elseif (\array_key_exists('department_id', $data) && $data['department_id'] === null) { |
79: | $object->setDepartmentId(null); |
80: | } |
81: | if (\array_key_exists('department', $data)) { |
82: | $object->setDepartment($this->denormalizer->denormalize($data['department'], 'NxSys\\Library\\Clients\\Brex\\API\\Expenses\\Model\\ExpandableExpenseDepartment', 'json', $context)); |
83: | unset($data['department']); |
84: | } |
85: | if (\array_key_exists('updated_at', $data)) { |
86: | $object->setUpdatedAt(\DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data['updated_at'])); |
87: | unset($data['updated_at']); |
88: | } |
89: | if (\array_key_exists('category', $data)) { |
90: | $object->setCategory($data['category']); |
91: | unset($data['category']); |
92: | } |
93: | if (\array_key_exists('receipts', $data) && $data['receipts'] !== null) { |
94: | $values = []; |
95: | foreach ($data['receipts'] as $value) { |
96: | $values[] = $this->denormalizer->denormalize($value, 'NxSys\\Library\\Clients\\Brex\\API\\Expenses\\Model\\Receipt', 'json', $context); |
97: | } |
98: | $object->setReceipts($values); |
99: | unset($data['receipts']); |
100: | } elseif (\array_key_exists('receipts', $data) && $data['receipts'] === null) { |
101: | $object->setReceipts(null); |
102: | } |
103: | foreach ($data as $key => $value_1) { |
104: | if (preg_match('/.*/', (string) $key)) { |
105: | $object[$key] = $value_1; |
106: | } |
107: | } |
108: | |
109: | return $object; |
110: | } |
111: | |
112: | |
113: | |
114: | |
115: | public function normalize($object, $format = null, array $context = []) |
116: | { |
117: | $data = []; |
118: | $data['id'] = $object->getId(); |
119: | if ($object->isInitialized('memo') && null !== $object->getMemo()) { |
120: | $data['memo'] = $object->getMemo(); |
121: | } |
122: | if ($object->isInitialized('locationId') && null !== $object->getLocationId()) { |
123: | $data['location_id'] = $object->getLocationId(); |
124: | } |
125: | if ($object->isInitialized('location') && null !== $object->getLocation()) { |
126: | $data['location'] = $this->normalizer->normalize($object->getLocation(), 'json', $context); |
127: | } |
128: | if ($object->isInitialized('departmentId') && null !== $object->getDepartmentId()) { |
129: | $data['department_id'] = $object->getDepartmentId(); |
130: | } |
131: | if ($object->isInitialized('department') && null !== $object->getDepartment()) { |
132: | $data['department'] = $this->normalizer->normalize($object->getDepartment(), 'json', $context); |
133: | } |
134: | $data['updated_at'] = $object->getUpdatedAt()->format('Y-m-d\\TH:i:sP'); |
135: | if ($object->isInitialized('category') && null !== $object->getCategory()) { |
136: | $data['category'] = $object->getCategory(); |
137: | } |
138: | if ($object->isInitialized('receipts') && null !== $object->getReceipts()) { |
139: | $values = []; |
140: | foreach ($object->getReceipts() as $value) { |
141: | $values[] = $this->normalizer->normalize($value, 'json', $context); |
142: | } |
143: | $data['receipts'] = $values; |
144: | } |
145: | foreach ($object as $key => $value_1) { |
146: | if (preg_match('/.*/', (string) $key)) { |
147: | $data[$key] = $value_1; |
148: | } |
149: | } |
150: | |
151: | return $data; |
152: | } |
153: | } |
154: | |