diff --git a/src/TypedProperty.php b/src/TypedProperty.php index 471ac87..b0e64a9 100644 --- a/src/TypedProperty.php +++ b/src/TypedProperty.php @@ -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; } } diff --git a/tests/ExampleTypedProperty.php b/tests/ExampleTypedProperty.php index 58fdb1d..315d85b 100644 --- a/tests/ExampleTypedProperty.php +++ b/tests/ExampleTypedProperty.php @@ -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' + ], ]; } diff --git a/tests/TypedPropertyTest.php b/tests/TypedPropertyTest.php index b2a9397..ec2412a 100644 --- a/tests/TypedPropertyTest.php +++ b/tests/TypedPropertyTest.php @@ -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' => @@ -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,