From b78a5649bf1e4f280a65dda59c14489aaffc2800 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 7 Nov 2026 16:10:06 +0100 Subject: [PATCH 1/3] Marshal values returned by mappings --- src/main/php/util/data/Marshalling.class.php | 2 +- .../util/data/unittest/MarshallingTest.class.php | 16 ++++++++++------ .../data/unittest/fixtures/Employee.class.php | 13 +++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100755 src/test/php/util/data/unittest/fixtures/Employee.class.php 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..b31fae9 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,12 +50,15 @@ 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 ['person' => $e->person, 'salary' => $e->salary]; }) + ; - Assert::equals(['id' => 6100], $marshalling->marshal(new Person(6100, 'Test'))); + Assert::equals( + ['person' => ['id' => 153], 'salary' => ['amount' => '6100', 'currency' => 'EUR']], + $marshalling->marshal(new Employee(new Person(153, 'Test'), new Money(6100, Currency::$EUR))) + ); } #[Test] 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..3a114fb --- /dev/null +++ b/src/test/php/util/data/unittest/fixtures/Employee.class.php @@ -0,0 +1,13 @@ +person= $person; + $this->salary= $salary; + } +} \ No newline at end of file From 2e12b695fe20ed816683a2c62830ecf5e06a8155 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 7 Nov 2026 16:13:16 +0100 Subject: [PATCH 2/3] Add compatibility with PHP 7.0..7.3 --- .../php/util/data/unittest/fixtures/Employee.class.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/php/util/data/unittest/fixtures/Employee.class.php b/src/test/php/util/data/unittest/fixtures/Employee.class.php index 3a114fb..34710e2 100755 --- a/src/test/php/util/data/unittest/fixtures/Employee.class.php +++ b/src/test/php/util/data/unittest/fixtures/Employee.class.php @@ -3,8 +3,12 @@ use util\Money; class Employee { - public Person $person; - public Money $salary; + + /** @type util.data.unittest.fixtures.Person */ + public $person; + + /** @type util.Money */ + public $salary; public function __construct(Person $person, Money $salary) { $this->person= $person; From 5ce814dc66f39d9512ac3978c4ed64a412659d96 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 7 Nov 2026 17:05:56 +0100 Subject: [PATCH 3/3] Show how resolving() can use unmarshal() inside the function TODO: Should the marshalling instance be passed as optional 3rd parameter? --- .../data/unittest/MarshallingTest.class.php | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/test/php/util/data/unittest/MarshallingTest.class.php b/src/test/php/util/data/unittest/MarshallingTest.class.php index b31fae9..eefe8a3 100755 --- a/src/test/php/util/data/unittest/MarshallingTest.class.php +++ b/src/test/php/util/data/unittest/MarshallingTest.class.php @@ -52,22 +52,30 @@ public function instance_as_nullable($input) { public function mapping() { $marshalling= (new Marshalling()) ->mapping(Person::class, function($p) { return ['id' => $p->id()]; }) - ->mapping(Employee::class, function($e) { return ['person' => $e->person, 'salary' => $e->salary]; }) + ->mapping(Employee::class, function($e) { return ['ref' => $e->person, 'pay' => $e->salary]; }); ; Assert::equals( - ['person' => ['id' => 153], 'salary' => ['amount' => '6100', 'currency' => 'EUR']], + ['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