Skip to content

Commit

Permalink
export other typed objects in arrays correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlmoon committed Jun 30, 2019
1 parent 44b7a00 commit 9eb9e0a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/TypedProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ public function to_array() {
) {
$output[$key] = $value->to_array();
} else {
if (is_array($value)) {
foreach ($value as $k => $v) {
if (
is_object($v) &&
(
$v instanceof TypedProperty ||
$v instanceof TypedArray
)
) {
$value[$k] = $v->to_array();
}
}
}
$output[$key] = $value;
}
}
Expand Down
35 changes: 27 additions & 8 deletions tests/ExampleTypedProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,32 @@ class ExampleTypedProperty extends TypedProperty {
protected $parent;

protected const CONSTRAINTS = [
'name' => ['type' => 'string'],
'hire_date' => ['type' => '\DealNews\TypedObjects\Tests\ExampleTypedSubProperty'],
'parent' => ['type' => '\DealNews\TypedObjects\Tests\ExampleTypedProperty'],
'position' => ['type' => 'string'],
'array_a' => ['type' => 'array'],
'boolean_a' => ['type' => 'boolean'],
'float_a' => ['type' => 'double'],
'int_a' => ['type' => 'integer'],
'name' => [
'type' => 'string'
],
'hire_date' => [
'type' => '\DealNews\TypedObjects\Tests\ExampleTypedSubProperty'
],
'parent' => [
'type' => '\DealNews\TypedObjects\Tests\ExampleTypedProperty'
],
'position' => [
'type' => 'string'
],
'array_a' => [
'type' => 'array',
'constraint' => [
'type' => '\DealNews\TypedObjects\Tests\ExampleTypedSubProperty'
]
],
'boolean_a' => [
'type' => 'boolean'
],
'float_a' => [
'type' => 'double'
],
'int_a' => [
'type' => 'integer'
],
];
}
10 changes: 9 additions & 1 deletion tests/TypedPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function testToArray() {
$hire_date->date = '1997-01-01';
$object->hire_date = $hire_date;

$object->array_a = [$hire_date];

$expected = [
'name' => 'test',
'hire_date' =>
Expand All @@ -106,7 +108,13 @@ public function testToArray() {
'daylight_savings_time' => null,
],
'position' => null,
'array_a' => null,
'array_a' => [
[
'time' => null,
'date' => '1997-01-01',
'daylight_savings_time' => null,
],
],
'boolean_a' => null,
'float_a' => null,
'int_a' => null,
Expand Down

0 comments on commit 9eb9e0a

Please sign in to comment.