Skip to content

Commit

Permalink
Fix unmarshalling object fields
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Feb 12, 2018
1 parent ba647d4 commit 3d0812f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Rest API change log

## ?.?.? / ????-??-??

## 0.1.1 / 2018-02-13

* Fixed object fields not being converted to their declared types during
unmarshalling (e.g., using `/** @var T */`).
(@thekid)

## 0.1.0 / 2018-02-12

* Hello World! First release - @thekid
19 changes: 10 additions & 9 deletions src/main/php/web/rest/Marshalling.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ public function unmarshal($value, $type) {

$n= $field->getName();
if ($m & MODIFIER_PUBLIC) {
$field->set($r, $value[$n]);
$field->set($r, $this->unmarshal($value[$n], $field->getType()));
} else if ($type->hasMethod($set= 'set'.ucfirst($n))) {
$t->getMethod($set)->invoke($r, $value[$n]);
$method= $type->getMethod($set);
$method->invoke($r, $this->unmarshal($value[$n], $method->getParameter(0)->getType()));
} else {
$field->setAccessible(true)->set($r, $value[$n]);
$field->setAccessible(true)->set($r, $this->unmarshal($value[$n], $field->getType()));
}
}
return $r;
Expand Down Expand Up @@ -122,18 +123,18 @@ public function marshal($value) {
if (method_exists($value, '__toString')) return $value->__toString();

$r= [];
$t= typeof($value);
foreach ($t->getFields() as $field) {
$type= typeof($value);
foreach ($type->getFields() as $field) {
$m= $field->getModifiers();
if ($m & MODIFIER_STATIC) continue;

$n= $field->getName();
if ($m & MODIFIER_PUBLIC) {
$r[$n]= $field->get($value);
} else if ($t->hasMethod($n)) {
$r[$n]= $t->getMethod($n)->invoke($value, []);
} else if ($t->hasMethod($get= 'get'.ucfirst($n))) {
$r[$n]= $t->getMethod($get)->invoke($value, []);
} else if ($type->hasMethod($n)) {
$r[$n]= $type->getMethod($n)->invoke($value, []);
} else if ($type->hasMethod($get= 'get'.ucfirst($n))) {
$r[$n]= $type->getMethod($get)->invoke($value, []);
} else {
$r[$n]= $field->setAccessible(true)->get($value);
}
Expand Down
9 changes: 6 additions & 3 deletions src/test/php/web/rest/unittest/MarshallingTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,14 @@ public function unmarshal_money_uses_amount_and_currency() {
);
}

#[@test]
public function unmarshal_person_value_object() {
#[@test, @values([
# [['id' => 6100, 'name' => 'Test']],
# [['id' => '6100', 'name' => 'Test']],
#])]
public function unmarshal_person_value($object) {
$this->assertEquals(
new Person(6100, 'Test'),
(new Marshalling())->unmarshal(['id' => 6100, 'name' => 'Test'], Type::forName(Person::class))
(new Marshalling())->unmarshal($object, Type::forName(Person::class))
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/php/web/rest/unittest/Person.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
class Person {
private static $ROOT = 0;

/** @var int */
private $id;

/** @var string */
public $name;

public function __construct($id, $name) {
Expand Down

0 comments on commit 3d0812f

Please sign in to comment.