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 CardAccountNormalizer 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\\CardAccount'; |
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\\CardAccount'; |
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\CardAccount(); |
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('status', $data)) { |
60: | $object->setStatus($data['status']); |
61: | unset($data['status']); |
62: | } |
63: | if (\array_key_exists('current_balance', $data)) { |
64: | $object->setCurrentBalance($this->denormalizer->denormalize($data['current_balance'], 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\CardAccountCurrentBalance', 'json', $context)); |
65: | unset($data['current_balance']); |
66: | } |
67: | if (\array_key_exists('available_balance', $data)) { |
68: | $object->setAvailableBalance($this->denormalizer->denormalize($data['available_balance'], 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\CardAccountAvailableBalance', 'json', $context)); |
69: | unset($data['available_balance']); |
70: | } |
71: | if (\array_key_exists('account_limit', $data)) { |
72: | $object->setAccountLimit($this->denormalizer->denormalize($data['account_limit'], 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\CardAccountAccountLimit', 'json', $context)); |
73: | unset($data['account_limit']); |
74: | } |
75: | if (\array_key_exists('current_statement_period', $data)) { |
76: | $object->setCurrentStatementPeriod($this->denormalizer->denormalize($data['current_statement_period'], 'NxSys\\Library\\Clients\\Brex\\API\\Transactions\\Model\\StatementPeriod', 'json', $context)); |
77: | unset($data['current_statement_period']); |
78: | } |
79: | foreach ($data as $key => $value) { |
80: | if (preg_match('/.*/', (string) $key)) { |
81: | $object[$key] = $value; |
82: | } |
83: | } |
84: | |
85: | return $object; |
86: | } |
87: | |
88: | |
89: | |
90: | |
91: | public function normalize($object, $format = null, array $context = []) |
92: | { |
93: | $data = []; |
94: | $data['id'] = $object->getId(); |
95: | if ($object->isInitialized('status') && null !== $object->getStatus()) { |
96: | $data['status'] = $object->getStatus(); |
97: | } |
98: | if ($object->isInitialized('currentBalance') && null !== $object->getCurrentBalance()) { |
99: | $data['current_balance'] = $this->normalizer->normalize($object->getCurrentBalance(), 'json', $context); |
100: | } |
101: | if ($object->isInitialized('availableBalance') && null !== $object->getAvailableBalance()) { |
102: | $data['available_balance'] = $this->normalizer->normalize($object->getAvailableBalance(), 'json', $context); |
103: | } |
104: | if ($object->isInitialized('accountLimit') && null !== $object->getAccountLimit()) { |
105: | $data['account_limit'] = $this->normalizer->normalize($object->getAccountLimit(), 'json', $context); |
106: | } |
107: | $data['current_statement_period'] = $this->normalizer->normalize($object->getCurrentStatementPeriod(), 'json', $context); |
108: | foreach ($object as $key => $value) { |
109: | if (preg_match('/.*/', (string) $key)) { |
110: | $data[$key] = $value; |
111: | } |
112: | } |
113: | |
114: | return $data; |
115: | } |
116: | } |
117: | |