1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | namespace NxSys\Library\Clients\Brex\API\Webhooks\Normalizer; |
12: | |
13: | use Jane\Component\JsonSchemaRuntime\Reference; |
14: | use NxSys\Library\Clients\Brex\API\Webhooks\Runtime\Normalizer\CheckArray; |
15: | use NxSys\Library\Clients\Brex\API\Webhooks\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 TransferEventNormalizer 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\\Webhooks\\Model\\TransferEvent'; |
33: | } |
34: | |
35: | public function supportsNormalization($data, $format = null): bool |
36: | { |
37: | return is_object($data) && get_class($data) === 'NxSys\\Library\\Clients\\Brex\\API\\Webhooks\\Model\\TransferEvent'; |
38: | } |
39: | |
40: | |
41: | |
42: | |
43: | public function denormalize($data, $class, $format = null, array $context = []) |
44: | { |
45: | if (array_key_exists('event_type', $data) and 'TRANSFER_FAILED' === $data['event_type']) { |
46: | return $this->denormalizer->denormalize($data, 'NxSys\\Library\\Clients\\Brex\\API\\Webhooks\\Model\\TransferFailedEvent', $format, $context); |
47: | } |
48: | if (array_key_exists('event_type', $data) and 'TRANSFER_PROCESSED' === $data['event_type']) { |
49: | return $this->denormalizer->denormalize($data, 'NxSys\\Library\\Clients\\Brex\\API\\Webhooks\\Model\\TransferProcessedEvent', $format, $context); |
50: | } |
51: | if (isset($data['$ref'])) { |
52: | return new Reference($data['$ref'], $context['document-origin']); |
53: | } |
54: | if (isset($data['$recursiveRef'])) { |
55: | return new Reference($data['$recursiveRef'], $context['document-origin']); |
56: | } |
57: | $object = new \NxSys\Library\Clients\Brex\API\Webhooks\Model\TransferEvent(); |
58: | if (null === $data || false === \is_array($data)) { |
59: | return $object; |
60: | } |
61: | if (\array_key_exists('company_id', $data)) { |
62: | $object->setCompanyId($data['company_id']); |
63: | unset($data['company_id']); |
64: | } |
65: | if (\array_key_exists('payment_type', $data)) { |
66: | $object->setPaymentType($data['payment_type']); |
67: | unset($data['payment_type']); |
68: | } |
69: | if (\array_key_exists('return_for_id', $data) && $data['return_for_id'] !== null) { |
70: | $object->setReturnForId($data['return_for_id']); |
71: | unset($data['return_for_id']); |
72: | } elseif (\array_key_exists('return_for_id', $data) && $data['return_for_id'] === null) { |
73: | $object->setReturnForId(null); |
74: | } |
75: | foreach ($data as $key => $value) { |
76: | if (preg_match('/.*/', (string) $key)) { |
77: | $object[$key] = $value; |
78: | } |
79: | } |
80: | |
81: | return $object; |
82: | } |
83: | |
84: | |
85: | |
86: | |
87: | public function normalize($object, $format = null, array $context = []) |
88: | { |
89: | $data = []; |
90: | if (null !== $object->getEventType() and 'TRANSFER_FAILED' === $object->getEventType()) { |
91: | return $this->normalizer->normalize($object, $format, $context); |
92: | } |
93: | if (null !== $object->getEventType() and 'TRANSFER_PROCESSED' === $object->getEventType()) { |
94: | return $this->normalizer->normalize($object, $format, $context); |
95: | } |
96: | $data['company_id'] = $object->getCompanyId(); |
97: | $data['payment_type'] = $object->getPaymentType(); |
98: | if ($object->isInitialized('returnForId') && null !== $object->getReturnForId()) { |
99: | $data['return_for_id'] = $object->getReturnForId(); |
100: | } |
101: | foreach ($object as $key => $value) { |
102: | if (preg_match('/.*/', (string) $key)) { |
103: | $data[$key] = $value; |
104: | } |
105: | } |
106: | |
107: | return $data; |
108: | } |
109: | } |
110: | |