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
48 changes: 27 additions & 21 deletions src/main/php/util/data/Marshalling.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 1 addition & 2 deletions src/test/php/util/data/unittest/BytesTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace util\data\unittest;

use test\Assert;
use test\{Test, TestCase};
use test\{Assert, Test};
use util\Bytes;
use util\data\Marshalling;

Expand Down
3 changes: 1 addition & 2 deletions src/test/php/util/data/unittest/DatesTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace util\data\unittest;

use test\Assert;
use test\{Test, TestCase, Values};
use test\{Assert, Test, Values};
use util\Date;
use util\data\Marshalling;

Expand Down
3 changes: 1 addition & 2 deletions src/test/php/util/data/unittest/EnumsTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace util\data\unittest;

use test\Assert;
use test\{Test, TestCase, Values};
use test\{Assert, Test, Values};
use util\Currency;
use util\data\Marshalling;

Expand Down
3 changes: 1 addition & 2 deletions src/test/php/util/data/unittest/MoneyTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace util\data\unittest;

use test\Assert;
use test\{Test, TestCase};
use test\{Assert, Test};
use util\data\Marshalling;
use util\{Currency, Money};

Expand Down
3 changes: 1 addition & 2 deletions src/test/php/util/data/unittest/PrimitivesTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace util\data\unittest;

use test\Assert;
use test\{Test, TestCase, Values};
use test\{Assert, Test, Values};
use util\data\Marshalling;

class PrimitivesTest {
Expand Down