Skip to content

Commit 38513a0

Browse files
committed
Custom field support for constructors
1 parent 3049c24 commit 38513a0

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

Tests/CustomFieldTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ public function testGetSet() : void
2020
$this->assertIsString($fixture->cf_string);
2121
}
2222

23+
public function testJsonOutput() : void
24+
{
25+
$class = new \Tests\Fixtures\CustomFieldTest();
26+
$class->cf_gender = 'mail';
27+
$class->cf_firstName = 'First';
28+
$class->cf_lastName = 'Class';
29+
30+
$json = $class->getJSON();
31+
$expectedJSON = '{
32+
"cf:gender": "mail",
33+
"cf:firstName": "First",
34+
"cf:lastName": "Class"
35+
}';
36+
// normalize line endings
37+
$expectedJSON = \str_replace("\r\n", "\n", $expectedJSON);
38+
$this->assertEquals($expectedJSON, $json);
39+
40+
$class = new \Tests\Fixtures\CustomFieldTest(['cf_gender' => 'mail', 'cf_firstName' => 'First', 'cf_lastName' => 'Class']);
41+
$json = $class->getJSON();
42+
$this->assertEquals($expectedJSON, $json);
43+
}
44+
2345
public function testBadIntType() : void
2446
{
2547
$fixture = new \Tests\Fixtures\CustomFieldTest();

Tests/DefinitionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public function testGetSet() : void
6161

6262
$json = $fixture->getJSON();
6363
$expectedJSON = '{
64-
"class": [],
6564
"integer": 123,
6665
"string": "A long string",
6766
"boolean": true,
@@ -73,7 +72,8 @@ public function testGetSet() : void
7372
"float": 1.23,
7473
"enum": "primary_email",
7574
"intEnum": 1,
76-
"ucEnum": "REMOVED"
75+
"ucEnum": "REMOVED",
76+
"class": []
7777
}';
7878
// normalize line endings
7979
$expectedJSON = \str_replace("\r\n", "\n", $expectedJSON);

src/ConstantContact/Definition/Base.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,32 @@ abstract class Base
5151

5252
public function __construct(array $initialValues = [])
5353
{
54-
foreach (static::$fields as $field => $type)
54+
foreach ($initialValues as $field => $value)
5555
{
56-
if (! empty($initialValues[$field]))
56+
$actualField = $field;
57+
if (str_starts_with($field, 'cf_'))
5758
{
58-
$this->{$field} = $initialValues[$field];
59+
$field = 'cf:custom_field_name';
60+
}
61+
$type = static::$fields[$field] ?? null;
62+
if (! $type)
63+
{
64+
continue;
65+
}
66+
67+
if (! empty(static::$fields[$field]))
68+
{
69+
$this->{$actualField} = $value;
5970
}
6071
elseif (! \is_array($type) && ! isset(self::$scalars[$type]))
6172
{
6273
if (\str_starts_with($type, 'array'))
6374
{
64-
$this->data[$field] = [];
75+
$this->data[$actualField] = [];
6576
}
6677
else
6778
{
68-
$this->data[$field] = new $type();
79+
$this->data[$actualField] = new $type();
6980
}
7081
}
7182
}

0 commit comments

Comments
 (0)