1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | namespace NxSys\Library\Clients\Brex\API\Payments\Normalizer; |
12: | |
13: | use Jane\Component\JsonSchemaRuntime\Reference; |
14: | use NxSys\Library\Clients\Brex\API\Payments\Runtime\Normalizer\CheckArray; |
15: | use NxSys\Library\Clients\Brex\API\Payments\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 CreateTransferRequestNormalizer 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\\Payments\\Model\\CreateTransferRequest'; |
33: | } |
34: | |
35: | public function supportsNormalization($data, $format = null): bool |
36: | { |
37: | return is_object($data) && get_class($data) === 'NxSys\\Library\\Clients\\Brex\\API\\Payments\\Model\\CreateTransferRequest'; |
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\Payments\Model\CreateTransferRequest(); |
52: | if (null === $data || false === \is_array($data)) { |
53: | return $object; |
54: | } |
55: | if (\array_key_exists('counterparty', $data)) { |
56: | $values = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS); |
57: | foreach ($data['counterparty'] as $key => $value) { |
58: | $values[$key] = $value; |
59: | } |
60: | $object->setCounterparty($values); |
61: | unset($data['counterparty']); |
62: | } |
63: | if (\array_key_exists('amount', $data)) { |
64: | $object->setAmount($this->denormalizer->denormalize($data['amount'], 'NxSys\\Library\\Clients\\Brex\\API\\Payments\\Model\\Money', 'json', $context)); |
65: | unset($data['amount']); |
66: | } |
67: | if (\array_key_exists('description', $data)) { |
68: | $object->setDescription($data['description']); |
69: | unset($data['description']); |
70: | } |
71: | if (\array_key_exists('external_memo', $data)) { |
72: | $object->setExternalMemo($data['external_memo']); |
73: | unset($data['external_memo']); |
74: | } |
75: | if (\array_key_exists('originating_account', $data)) { |
76: | $values_1 = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS); |
77: | foreach ($data['originating_account'] as $key_1 => $value_1) { |
78: | $values_1[$key_1] = $value_1; |
79: | } |
80: | $object->setOriginatingAccount($values_1); |
81: | unset($data['originating_account']); |
82: | } |
83: | if (\array_key_exists('approval_type', $data)) { |
84: | $object->setApprovalType($data['approval_type']); |
85: | unset($data['approval_type']); |
86: | } |
87: | foreach ($data as $key_2 => $value_2) { |
88: | if (preg_match('/.*/', (string) $key_2)) { |
89: | $object[$key_2] = $value_2; |
90: | } |
91: | } |
92: | |
93: | return $object; |
94: | } |
95: | |
96: | |
97: | |
98: | |
99: | public function normalize($object, $format = null, array $context = []) |
100: | { |
101: | $data = []; |
102: | $values = []; |
103: | foreach ($object->getCounterparty() as $key => $value) { |
104: | $values[$key] = $value; |
105: | } |
106: | $data['counterparty'] = $values; |
107: | $data['amount'] = $this->normalizer->normalize($object->getAmount(), 'json', $context); |
108: | $data['description'] = $object->getDescription(); |
109: | $data['external_memo'] = $object->getExternalMemo(); |
110: | $values_1 = []; |
111: | foreach ($object->getOriginatingAccount() as $key_1 => $value_1) { |
112: | $values_1[$key_1] = $value_1; |
113: | } |
114: | $data['originating_account'] = $values_1; |
115: | if ($object->isInitialized('approvalType') && null !== $object->getApprovalType()) { |
116: | $data['approval_type'] = $object->getApprovalType(); |
117: | } |
118: | foreach ($object as $key_2 => $value_2) { |
119: | if (preg_match('/.*/', (string) $key_2)) { |
120: | $data[$key_2] = $value_2; |
121: | } |
122: | } |
123: | |
124: | return $data; |
125: | } |
126: | } |
127: | |