diff --git a/src/main/php/util/data/Marshalling.class.php b/src/main/php/util/data/Marshalling.class.php index 91f3651..ca4bfed 100755 --- a/src/main/php/util/data/Marshalling.class.php +++ b/src/main/php/util/data/Marshalling.class.php @@ -186,7 +186,7 @@ private function iterator($in) { public function marshal($value) { if (is_object($value)) { foreach ($this->mappings as $type => $marshal) { - if ($value instanceof $type) return $marshal[0]($value); + if ($value instanceof $type) return $this->marshal($marshal[0]($value)); } if ($value instanceof Enum) { diff --git a/src/test/php/util/data/unittest/MarshallingTest.class.php b/src/test/php/util/data/unittest/MarshallingTest.class.php index 9929cb7..eefe8a3 100755 --- a/src/test/php/util/data/unittest/MarshallingTest.class.php +++ b/src/test/php/util/data/unittest/MarshallingTest.class.php @@ -3,7 +3,8 @@ use lang\Type; use test\{Assert, Test, Values}; use util\data\Marshalling; -use util\data\unittest\fixtures\{Date, Person}; +use util\data\unittest\fixtures\{Date, Person, Employee}; +use util\{Money, Currency}; class MarshallingTest { @@ -49,21 +50,32 @@ public function instance_as_nullable($input) { #[Test] public function mapping() { - $marshalling= (new Marshalling())->mapping( - Person::class, - function($person) { return ['id' => $person->id()]; } - ); + $marshalling= (new Marshalling()) + ->mapping(Person::class, function($p) { return ['id' => $p->id()]; }) + ->mapping(Employee::class, function($e) { return ['ref' => $e->person, 'pay' => $e->salary]; }); + ; - Assert::equals(['id' => 6100], $marshalling->marshal(new Person(6100, 'Test'))); + Assert::equals( + ['ref' => ['id' => 153], 'pay' => ['amount' => '6100', 'currency' => 'EUR']], + $marshalling->marshal(new Employee(new Person(153, 'Test'), new Money(6100, Currency::$EUR))) + ); } #[Test] public function resolving() { - $marshalling= (new Marshalling())->resolving( - Person::class, - function($value) { return new Person($value['id'], 'Test'); } - ); + $marshalling= (new Marshalling()) + ->resolving(Person::class, function($value) { return new Person($value['id'], 'Test'); }) + ->resolving(Employee::class, function($value) use(&$marshalling) { + return new Employee( + $marshalling->unmarshal($value['ref'], Person::class), + $marshalling->unmarshal($value['pay'], Money::class) + ); + }) + ; - Assert::equals(new Person(6100, 'Test'), $marshalling->unmarshal(['id' => 6100], Person::class)); + Assert::equals( + new Employee(new Person(153, 'Test'), new Money(6100, Currency::$EUR)), + $marshalling->unmarshal(['ref' => ['id' => 153], 'pay' => ['amount' => '6100', 'currency' => 'EUR']], Employee::class) + ); } } \ No newline at end of file diff --git a/src/test/php/util/data/unittest/fixtures/Employee.class.php b/src/test/php/util/data/unittest/fixtures/Employee.class.php new file mode 100755 index 0000000..34710e2 --- /dev/null +++ b/src/test/php/util/data/unittest/fixtures/Employee.class.php @@ -0,0 +1,17 @@ +person= $person; + $this->salary= $salary; + } +} \ No newline at end of file