Skip to content

Commit bf400ca

Browse files
author
František Mazura
committed
add support for root array
1 parent 80f0578 commit bf400ca

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22

33
/vendor/
4+
/.phpunit.result.cache

src/AbstractValueObject.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ public function toArray(): array
112112
$out = $item->toArray();
113113
break;
114114
}
115+
if ($name == self::ROOT && is_array($item)) {
116+
foreach ($item as $i) {
117+
// array of ValueObjects
118+
if ($i instanceof AbstractValueObject) {
119+
$out[] = $i->toArray();
120+
} // array of other types
121+
else {
122+
$out[] = $i;
123+
}
124+
}
125+
break;
126+
}
127+
115128
// ValueObject element
116129
if ($item instanceof AbstractValueObject) {
117130
$out[$name] = $item->toArray();

tests/ValueObjectTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use OGSoft\ValueObjects\Exceptions\ValidatorException;
77
use PHPUnit\Framework\TestCase;
88
use Tests\ValueObjects\CarValueObject;
9-
use Tests\ValueObjects\RootValueObject;
109

1110
final class ValueObjectTest extends TestCase
1211
{
@@ -63,16 +62,6 @@ public function testNull()
6362
$car = new CarValueObject();
6463
$this->expectException(ValidatorException::class);
6564
$car->init($data);
66-
$this->assertTrue(true);
67-
}
68-
69-
public function testRoot()
70-
{
71-
$data = ["winter" => true];
72-
73-
$tire = new RootValueObject();
74-
$tire->init(["root" => $data]);
75-
$this->assertEquals($data, $tire->toArray());
7665
}
7766
}
7867

tests/ValueObjects/RootValueObject.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
class RootValueObject extends AbstractMyValueObject
88
{
99
/**
10-
* @var TireValueObject
10+
* @var CarValueObject
1111
*/
1212
protected $root;
1313

1414
/**
15-
* @return TireValueObject
15+
* @return CarValueObject
1616
*/
17-
public function getRoot(): TireValueObject
17+
public function getRoot(): CarValueObject
1818
{
1919
return $this->root;
2020
}
2121

2222
/**
23-
* @param TireValueObject $root
23+
* @param CarValueObject $root
2424
* @return RootValueObject
2525
*/
26-
public function setRoot(TireValueObject $root): RootValueObject
26+
public function setRoot(CarValueObject $root): RootValueObject
2727
{
2828
$this->root = $root;
2929
return $this;

0 commit comments

Comments
 (0)