-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDenormalizer.php
112 lines (97 loc) · 3.66 KB
/
Denormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace JGI\Ratsit;
use JGI\Ratsit\Exception\InvalidJsonException;
use JGI\Ratsit\Model\Address;
use JGI\Ratsit\Model\Company;
use JGI\Ratsit\Model\Person;
use JGI\Ratsit\Model\SearchResult;
class Denormalizer
{
const RESPONSE_CODE_NOT_FOUND = 'NotFound';
/**
* @param array|null $data
*
* @return Person|null
*/
public function denormalizerPersonInformation(?array $data): ?Person
{
if (!$data || !array_key_exists('basic', $data)) {
if ($data && array_key_exists('responseCode', $data) && $data['responseCode'] == self::RESPONSE_CODE_NOT_FOUND) {
return null;
}
if ($data && array_key_exists('type', $data) && $data['type'] == 'InvalidParameters') {
return null;
}
throw new InvalidJsonException();
}
$person = new Person();
$person->setFirstName($data['basic']['firstName']);
$person->setGivenName($data['basic']['givenName']);
$person->setSurName($data['basic']['surName']);
$person->setMiddleName($data['basic']['middleName']);
$person->setLastName($data['basic']['lastName']);
$address = new Address();
$address->setStreet($data['basic']['street']);
$address->setCo($data['basic']['co']);
$address->setPostalCode($data['basic']['zipCode']);
$address->setCity($data['basic']['city']);
$person->setAddress($address);
$person->setPhoneNumbers($data['phoneNumbers']['phoneNumbers']);
return $person;
}
/**
* @param array|null $data
*
* @return SearchResult|Person[]
*/
public function denormalizerPersonSearch(?array $data)
{
if (!$data || !array_key_exists('extendedResult', $data)) {
throw new InvalidJsonException();
}
$persons = new SearchResult();
$persons->setTotalRecordsFound($data['extendedResult']['totalRecordsFound']);
foreach ($data['extendedResult']['records'] as $row) {
$person = new Person();
$person->setSocialSecurityNumber($row['ssn']);
$person->setFirstName($row['firstName']);
$person->setGivenName($row['givenName']);
$person->setLastName($row['lastName']);
$address = new Address();
$address->setStreet($row['street']);
$address->setPostalCode($row['zipCode']);
$address->setCity($row['city']);
$person->setAddress($address);
if (array_key_exists('phoneNumbers', $row)) { // Ratsit only return phone numbers if only one person is found
$person->setPhoneNumbers($row['phoneNumbers']);
}
$persons->add($person);
}
return $persons;
}
/**
* @param array|null $data
*
* @return Company|null
*/
public function denormalizerCompanyInformation(?array $data)
{
if ($data && array_key_exists('responseCode', $data) && $data['responseCode'] == self::RESPONSE_CODE_NOT_FOUND) {
return null;
}
if (!$data || !array_key_exists('basic', $data)) {
throw new InvalidJsonException();
}
$company = new Company();
$company->setName($data['basic']['companyName']);
$company->setPhoneNumber($data['basic']['phoneNumber']);
$address = new Address();
$address->setStreet($data['basic']['street']);
$address->setCo($data['basic']['co']);
$address->setPostalCode($data['basic']['zipCode']);
$address->setCity($data['basic']['city']);
$company->setAddress($address);
return $company;
}
}