Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/php/util/data/Marshalling.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 23 additions & 11 deletions src/test/php/util/data/unittest/MarshallingTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
);
}
}
17 changes: 17 additions & 0 deletions src/test/php/util/data/unittest/fixtures/Employee.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace util\data\unittest\fixtures;

use util\Money;

class Employee {

/** @type util.data.unittest.fixtures.Person */
public $person;

/** @type util.Money */
public $salary;

public function __construct(Person $person, Money $salary) {
$this->person= $person;
$this->salary= $salary;
}
}
Loading