Skip to content

Commit 85b1ef0

Browse files
committed
Better Definition construct from array
1 parent 87dd954 commit 85b1ef0

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

Tests/HydrateTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHPFUI\ConstantContact package
5+
*
6+
* (c) Bruce Wells
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source
10+
* code
11+
*/
12+
class HydrateTest extends \PHPUnit\Framework\TestCase
13+
{
14+
public function testHydration() : void
15+
{
16+
$json = '{
17+
"contacts": [
18+
{
19+
"contact_id": "d5e4dc88-9dbf-11ef-9af2-fa163e4d7501",
20+
"email_address": {
21+
"address": "xxxx@mailinator.com",
22+
"permission_to_send": "explicit",
23+
"created_at": "2024-11-08T10:54:32Z",
24+
"updated_at": "2024-11-08T10:54:32Z",
25+
"opt_in_source": "Contact",
26+
"opt_in_date": "2024-11-08T10:54:32Z",
27+
"confirm_status": "off"
28+
},
29+
"first_name": "Petr",
30+
"last_name": "Pavel",
31+
"create_source": "Contact",
32+
"created_at": "2024-11-08T10:54:32Z",
33+
"updated_at": "2024-11-08T10:54:32Z"
34+
}
35+
]
36+
}';
37+
$this->assertJson($json);
38+
$dataArray = \json_decode($json, true);
39+
$this->assertIsArray($dataArray);
40+
$this->assertIsArray($dataArray['contacts']);
41+
$this->assertEquals('Petr', $dataArray['contacts'][0]['first_name']);
42+
$this->assertIsArray($dataArray['contacts'][0]['email_address']);
43+
$this->assertEquals('xxxx@mailinator.com', $dataArray['contacts'][0]['email_address']['address']);
44+
$contacts = new \PHPFUI\ConstantContact\Definition\Contacts($dataArray);
45+
$this->assertIsArray($contacts->contacts);
46+
$this->assertEquals('d5e4dc88-9dbf-11ef-9af2-fa163e4d7501', $contacts->contacts[0]->contact_id);
47+
$this->assertEquals('xxxx@mailinator.com', $contacts->contacts[0]->email_address->address);
48+
$this->assertEquals('xxxx@mailinator.com', $contacts->contacts[0]->email_address->address);
49+
$this->assertEquals('2024-11-08T10:54:32Z', $contacts->contacts[0]->created_at);
50+
$newJson = $contacts->getJSON();
51+
$this->assertJson($newJson);
52+
$newDataArray = $contacts->toArray();
53+
$this->assertEquals($dataArray, $newDataArray);
54+
}
55+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"roave/security-advisories": "dev-latest",
2424
"friendsofphp/php-cs-fixer": "^3.0",
2525
"symfony/yaml": "^7.0",
26-
"phpstan/phpstan": "^1.8"
26+
"phpstan/phpstan": "^2.0"
2727
},
2828
"autoload": {
2929
"psr-4": {"PHPFUI\\ConstantContact\\": "src/ConstantContact/"}

src/ConstantContact/Definition/Base.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
abstract class Base
66
{
77
/**
8-
* @var array indexed by field name containing field type.
8+
* @var array<string, string | array> $fields indexed by field name containing field type.
99
*
1010
* Valid types are:
1111
* - bool
1212
* - float
1313
* - int
1414
* - string
1515
* - FQN (fully qualified name) PHP class
16-
* - array&lt;FQN&gt;
16+
* - array<FQN>
1717
* - array of case sensitive string or integer enums
1818
*/
1919
protected static array $fields = [];
@@ -183,6 +183,13 @@ public function __set(string $field, mixed $value)
183183
{
184184
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$actualField} is of type {$type} but should be type {$expectedType}");
185185
}
186+
elseif (\str_starts_with($expectedType, 'PHPFUI'))
187+
{
188+
if (\is_array($value))
189+
{
190+
$value = new $expectedType($value);
191+
}
192+
}
186193
}
187194

188195
if (isset(static::$minLength[$field]))

0 commit comments

Comments
 (0)