Skip to content

Commit

Permalink
Better object hydration and DateTime fix
Browse files Browse the repository at this point in the history
  • Loading branch information
phpfui committed Nov 12, 2024
1 parent 4f1978d commit 5bff968
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
28 changes: 25 additions & 3 deletions Tests/HydrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ public function testHydration() : void
{
$json = '{
"contacts": [
{
"contact_id": "d5e4dc88-9dbf-11ef-9af2-fa163e4d7501",
"email_address": {
"address": "[email protected]",
"permission_to_send": "explicit",
"created_at": "2024-11-08T10:54:32Z",
"updated_at": "2024-11-08T10:54:32Z",
"opt_in_source": "Contact",
"opt_in_date": "2024-11-08T10:54:32Z",
"confirm_status": "off"
},
"first_name": "Petr",
"last_name": "Pavel",
"create_source": "Contact",
"created_at": "2024-11-08T10:54:32Z",
"updated_at": "2024-11-08T10:54:32Z"
},
{
"contact_id": "d5e4dc88-9dbf-11ef-9af2-fa163e4d7501",
"email_address": {
Expand All @@ -39,14 +56,19 @@ public function testHydration() : void
$this->assertIsArray($dataArray);
$this->assertIsArray($dataArray['contacts']);
$this->assertEquals('Petr', $dataArray['contacts'][0]['first_name']);
$this->assertEquals('Petr', $dataArray['contacts'][1]['first_name']);
$this->assertIsArray($dataArray['contacts'][0]['email_address']);
$this->assertIsArray($dataArray['contacts'][1]['email_address']);
$this->assertEquals('[email protected]', $dataArray['contacts'][0]['email_address']['address']);
$this->assertEquals('[email protected]', $dataArray['contacts'][1]['email_address']['address']);
$contacts = new \PHPFUI\ConstantContact\Definition\Contacts($dataArray);
$this->assertIsArray($contacts->contacts);
$this->assertEquals('d5e4dc88-9dbf-11ef-9af2-fa163e4d7501', $contacts->contacts[0]->contact_id);
$this->assertEquals('[email protected]', $contacts->contacts[0]->email_address->address);
$this->assertEquals('d5e4dc88-9dbf-11ef-9af2-fa163e4d7501', "{$contacts->contacts[0]->contact_id}");
$this->assertEquals('[email protected]', $contacts->contacts[0]->email_address->address);
$this->assertEquals('2024-11-08T10:54:32Z', $contacts->contacts[0]->created_at);
$this->assertEquals('[email protected]', $contacts->contacts[1]->email_address->address);
$time = new \PHPFUI\ConstantContact\DateTime("2024-11-08T10:54:32Z");
$this->assertEquals("{$time}", "{$contacts->contacts[0]->created_at}");
$this->assertEquals("{$time}", "{$contacts->contacts[1]->created_at}");
$newJson = $contacts->getJSON();
$this->assertJson($newJson);
$newDataArray = $contacts->toArray();
Expand Down
2 changes: 1 addition & 1 deletion src/ConstantContact/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class DateTime extends \DateTime
{
public function __toString() : string
{
return $this->format(\DateTimeInterface::ATOM);
return $this->format('Y-m-d\TH:i:s\Z');
}
}
2 changes: 1 addition & 1 deletion src/ConstantContact/Definition/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function __set(string $field, mixed $value)
}
elseif (\str_starts_with($expectedType, 'PHPFUI'))
{
if (\is_array($value))
if (\is_array($value) || $expectedType != $type)
{
$value = new $expectedType($value);
}
Expand Down

0 comments on commit 5bff968

Please sign in to comment.