diff --git a/src/php/access/KeyAccess.php b/src/php/access/KeyAccess.php index b6e5979..b65b86d 100644 --- a/src/php/access/KeyAccess.php +++ b/src/php/access/KeyAccess.php @@ -3,7 +3,6 @@ namespace nur\sery\wip\php\access; use ArrayAccess; use nulib\cl; -use stdClass; /** * Class KeyAccess: accès @@ -13,7 +12,7 @@ use stdClass; class KeyAccess extends AbstractAccess { const ALLOW_NULL = null; const ALLOW_FALSE = null; - + function __construct(&$dest, $key=null, ?array $params=null) { parent::__construct($params); $this->dest =& $dest; diff --git a/src/php/access/PropertyAccess.php b/src/php/access/PropertyAccess.php index 832ea88..d27493f 100644 --- a/src/php/access/PropertyAccess.php +++ b/src/php/access/PropertyAccess.php @@ -6,7 +6,10 @@ use ReflectionException; use ReflectionProperty; class PropertyAccess extends AbstractAccess { - function __construct(object $object, $name, ?array $params=null) { + const ALLOW_NULL = true; + const ALLOW_FALSE = false; + + function __construct(object $object, ?string $name=null, ?array $params=null) { parent::__construct($params); $this->object = $object; $this->name = $name; @@ -18,34 +21,44 @@ class PropertyAccess extends AbstractAccess { $property = null; } $this->property = $property; - $this->allowNull = $params["allow_null"] ?? true; - $this->allowFalse = $params["allow_false"] ?? false; + $this->allowNull = $params["allow_null"] ?? static::ALLOW_NULL; + $this->allowFalse = $params["allow_false"] ?? static::ALLOW_FALSE; } protected object $object; - protected string $name; + protected ?string $name; protected ?ReflectionProperty $property; + function reset(object $object, ?string $name=null): self { + $this->object = $object; + $this->name = $name; + return $this; + } + + function resetKey($name=null): self { + $this->name = $name; + return $this; + } + protected bool $allowNull; protected bool $allowFalse; - function reset(object $object): self { - $this->object = $object; - return $this; - } - function exists(): bool { - return $this->property !== null - || property_exists($this->object, $this->name); + $name = $this->name; + return $name === null + || $this->property !== null + || property_exists($this->object, $name); } protected function _get($default=null) { $name = $this->name; $property = $this->property; - if ($property !== null) { + if ($name === null) { + return $this->object; + } elseif ($property !== null) { return $property->getValue($this->object); } elseif (property_exists($this->object, $name)) { return $this->object->$name; @@ -75,7 +88,17 @@ class PropertyAccess extends AbstractAccess { protected function _set($value): void { $name = $this->name; $property = $this->property; - if ($property !== null) { + if ($name === null) { + $this->object = $value; + $class = new ReflectionClass($value); + try { + $property = $class->getProperty($this->name); + $property->setAccessible(true); + } catch (ReflectionException $e) { + $property = null; + } + $this->property = $property; + } elseif ($property !== null) { $property->setValue($this->object, $value); } else { $this->object->$name = $value; diff --git a/src/schema/input/Input.php b/src/schema/input/Input.php index 6c63ed9..e951a62 100644 --- a/src/schema/input/Input.php +++ b/src/schema/input/Input.php @@ -1,6 +1,7 @@