From 2b793a629c51d5ea39b24b22ec1edf80daad653f Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Fri, 1 Nov 2024 12:30:50 +0100 Subject: [PATCH 1/2] Refactor mapping for classes inside `util`, making them overrideable --- src/main/php/util/data/Marshalling.class.php | 48 +++++++++++--------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/main/php/util/data/Marshalling.class.php b/src/main/php/util/data/Marshalling.class.php index b652c44..43e6ba0 100755 --- a/src/main/php/util/data/Marshalling.class.php +++ b/src/main/php/util/data/Marshalling.class.php @@ -18,6 +18,26 @@ class Marshalling { private $mappings= []; + /** Creates marshalling with default mappings for classes in `util` */ + public function __construct() { + $this->mappings[Date::class]= [ + function($value) { return $value->toString(DATE_ISO8601); }, + function($value) { return new Date($value); }, + ]; + $this->mappings[Bytes::class]= [ + function($value) { return base64_encode($value); }, + function($value) { return new Bytes(base64_decode($value)); }, + ]; + $this->mappings[Money::class]= [ + function($value) { return ['amount' => $value->amount(), 'currency' => $value->currency()->name()]; }, + function($value) { return new Money($value['amount'], Currency::getInstance($value['currency'])); }, + ]; + $this->mappings[XPIterator::class]= [ + function($value) { return $this->iterator($value); }, + function($value, $t) { return new Iteration($value); }, + ]; + } + /** * Maps user type marshalling * @@ -77,14 +97,6 @@ public function unmarshal($value, $type= null) { return $value; } else if ($t->isEnum()) { return Enum::valueOf($t, $value); - } else if ($t->isAssignableFrom(Date::class)) { - return new Date($value); - } else if ($t->isAssignableFrom(Bytes::class)) { - return new Bytes(base64_decode($value)); - } else if ($t->isAssignableFrom(Money::class)) { - return new Money($value['amount'], Currency::getInstance($value['currency'])); - } else if ($t->isAssignableFrom(XPIterator::class)) { - return new Iteration($value); } else if ($t->isInterface()) { return $t->cast($value); } @@ -165,23 +177,17 @@ private function iterator($in) { * @return var */ public function marshal($value) { - if ($value instanceof Date) { - return $value->toString(DATE_ISO8601); - } else if ($value instanceof Bytes) { - return base64_encode($value); - } else if ($value instanceof Money) { - return ['amount' => $value->amount(), 'currency' => $value->currency()->name()]; - } else if ($value instanceof Enum) { - return $value->name(); - } else if ($value instanceof Traversable) { - return $this->generator($value); - } else if ($value instanceof XPIterator) { - return $this->iterator($value); - } else if (is_object($value)) { + if (is_object($value)) { foreach ($this->mappings as $type => $marshal) { if ($value instanceof $type) return $marshal[0]($value); } + if ($value instanceof Enum) { + return $value->name(); + } else if ($value instanceof Traversable) { + return $this->generator($value); + } + if (method_exists($value, '__serialize')) return $value->__serialize(); if (method_exists($value, '__toString')) return $value->__toString(); From 695768917a5cca653a4c48e5f38e81cbaa83945d Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Fri, 1 Nov 2024 12:41:48 +0100 Subject: [PATCH 2/2] QA: Group import statements --- src/test/php/util/data/unittest/BytesTest.class.php | 3 +-- src/test/php/util/data/unittest/DatesTest.class.php | 3 +-- src/test/php/util/data/unittest/EnumsTest.class.php | 3 +-- src/test/php/util/data/unittest/MoneyTest.class.php | 3 +-- src/test/php/util/data/unittest/PrimitivesTest.class.php | 3 +-- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/test/php/util/data/unittest/BytesTest.class.php b/src/test/php/util/data/unittest/BytesTest.class.php index cf74a87..a9648fe 100755 --- a/src/test/php/util/data/unittest/BytesTest.class.php +++ b/src/test/php/util/data/unittest/BytesTest.class.php @@ -1,7 +1,6 @@