Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new reflection library #2

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"keywords": ["module", "xp"],
"require" : {
"xp-framework/core": "^10.0 | ^9.0 | ^8.0 | ^7.0",
"xp-framework/reflection": "^1.2",
"php": ">=7.0.0"
},
"require-dev" : {
Expand Down
74 changes: 23 additions & 51 deletions src/main/php/util/data/Marshalling.class.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
<?php namespace util\data;

use lang\ArrayType;
use lang\Enum;
use lang\MapType;
use lang\Type;
use lang\XPClass;
use util\Bytes;
use util\Currency;
use util\Date;
use util\Money;
use util\XPIterator;
use lang\{ArrayType, Enum, MapType, Reflection, Type, XPClass};
use util\{Bytes, Currency, Date, Money, XPIterator};

/**
* Takes care of converting objects from and to maps
Expand Down Expand Up @@ -37,18 +29,6 @@ private function iterable($in, $type) {
}
}

/**
* Check whether a type's constructor accepts a given value
*
* @param lang.Type $type
* @param var $value
* @return bool
*/
private function constructorAccepts($type, $value) {
$params= $type->getConstructor()->getParameters();
return 1 === sizeof($params) && $params[0]->getType()->isInstance($value);
}

/**
* Unmarshals a value. Handles util.Date and util.Money instances specially,
* creates instances if the type has a single-argument constructor; treats
Expand Down Expand Up @@ -77,30 +57,27 @@ public function unmarshal($value, $type= null) {
return new Iteration($value);
} else if ($t->isInterface()) {
return $t->cast($value);
} else if ($t->hasConstructor() && $this->constructorAccepts($t, $value)) {
return $t->newInstance($value);
}

$r= $t->reflect()->newInstanceWithoutConstructor();
if (method_exists($r, '__unserialize')) {
$r->__unserialize($value);
return $r;
$reflect= Reflection::of($t);
if ($i= $reflect->initializer('__unserialize')) {
return $i->newInstance([$value]);
} else if (($c= $reflect->constructor()) && $c->parameters()->accept([$value], 1)) {
return $c->newInstance([$value]);
}

foreach ($t->getFields() as $field) {
$m= $field->getModifiers();
if ($m & MODIFIER_STATIC) continue;
$r= $reflect->initializer(null)->newInstance();
foreach ($reflect->properties() as $property) {
$m= $property->modifiers();
if ($m->isStatic()) continue;

$n= $field->getName();
$n= $property->name();
if (!isset($value[$n])) continue;

if ($m & MODIFIER_PUBLIC) {
$field->set($r, $this->unmarshal($value[$n], $field->getType()));
} else if ($t->hasMethod($set= 'set'.ucfirst($n))) {
$method= $t->getMethod($set);
$method->invoke($r, [$this->unmarshal($value[$n], $method->getParameter(0)->getType())]);
if (!$m->isPublic() && ($setter= $reflect->method('set'.ucfirst($n)))) {
$setter->invoke($r, [$this->unmarshal($value[$n], $setter->parameter(0)->constraint()->type())]);
} else {
$field->setAccessible(true)->set($r, $this->unmarshal($value[$n], $field->getType()));
$property->set($r, $this->unmarshal($value[$n], $property->constraint()->type()), $reflect);
}
}
return $r;
Expand Down Expand Up @@ -174,23 +151,18 @@ public function marshal($value) {
if (method_exists($value, '__serialize')) return $value->__serialize();
if (method_exists($value, '__toString')) return $value->__toString();

$reflect= Reflection::of($value);
$r= [];
$type= typeof($value);
foreach ($type->getFields() as $field) {
$m= $field->getModifiers();
if ($m & MODIFIER_STATIC) continue;
foreach ($reflect->properties() as $property) {
$m= $property->modifiers();
if ($m->isStatic()) continue;

$n= $field->getName();
if ($m & MODIFIER_PUBLIC) {
$v= $field->get($value);
} else if ($type->hasMethod($n)) {
$v= $type->getMethod($n)->invoke($value, []);
} else if ($type->hasMethod($get= 'get'.ucfirst($n))) {
$v= $type->getMethod($get)->invoke($value, []);
$n= $property->name();
if (!$m->isPublic() && ($method= $reflect->method($n) ?? $reflect->method('set'.ucfirst($n)))) {
$r[$n]= $this->marshal($method->invoke($value, []));
} else {
$v= $field->setAccessible(true)->get($value);
$r[$n]= $this->marshal($property->get($value, $reflect));
}
$r[$n]= $this->marshal($v);
}
return $r;
} else if (is_array($value)) {
Expand Down