modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-03-19 20:34:46 +04:00
parent f4e252a6e0
commit 6e8245dbcb
3 changed files with 39 additions and 16 deletions

View File

@ -3,7 +3,6 @@ namespace nur\sery\wip\php\access;
use ArrayAccess; use ArrayAccess;
use nulib\cl; use nulib\cl;
use stdClass;
/** /**
* Class KeyAccess: accès * Class KeyAccess: accès
@ -13,7 +12,7 @@ use stdClass;
class KeyAccess extends AbstractAccess { class KeyAccess extends AbstractAccess {
const ALLOW_NULL = null; const ALLOW_NULL = null;
const ALLOW_FALSE = null; const ALLOW_FALSE = null;
function __construct(&$dest, $key=null, ?array $params=null) { function __construct(&$dest, $key=null, ?array $params=null) {
parent::__construct($params); parent::__construct($params);
$this->dest =& $dest; $this->dest =& $dest;

View File

@ -6,7 +6,10 @@ use ReflectionException;
use ReflectionProperty; use ReflectionProperty;
class PropertyAccess extends AbstractAccess { 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); parent::__construct($params);
$this->object = $object; $this->object = $object;
$this->name = $name; $this->name = $name;
@ -18,34 +21,44 @@ class PropertyAccess extends AbstractAccess {
$property = null; $property = null;
} }
$this->property = $property; $this->property = $property;
$this->allowNull = $params["allow_null"] ?? true; $this->allowNull = $params["allow_null"] ?? static::ALLOW_NULL;
$this->allowFalse = $params["allow_false"] ?? false; $this->allowFalse = $params["allow_false"] ?? static::ALLOW_FALSE;
} }
protected object $object; protected object $object;
protected string $name; protected ?string $name;
protected ?ReflectionProperty $property; 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 $allowNull;
protected bool $allowFalse; protected bool $allowFalse;
function reset(object $object): self {
$this->object = $object;
return $this;
}
function exists(): bool { function exists(): bool {
return $this->property !== null $name = $this->name;
|| property_exists($this->object, $this->name); return $name === null
|| $this->property !== null
|| property_exists($this->object, $name);
} }
protected function _get($default=null) { protected function _get($default=null) {
$name = $this->name; $name = $this->name;
$property = $this->property; $property = $this->property;
if ($property !== null) { if ($name === null) {
return $this->object;
} elseif ($property !== null) {
return $property->getValue($this->object); return $property->getValue($this->object);
} elseif (property_exists($this->object, $name)) { } elseif (property_exists($this->object, $name)) {
return $this->object->$name; return $this->object->$name;
@ -75,7 +88,17 @@ class PropertyAccess extends AbstractAccess {
protected function _set($value): void { protected function _set($value): void {
$name = $this->name; $name = $this->name;
$property = $this->property; $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); $property->setValue($this->object, $value);
} else { } else {
$this->object->$name = $value; $this->object->$name = $value;

View File

@ -1,6 +1,7 @@
<?php <?php
namespace nur\sery\wip\schema\input; namespace nur\sery\wip\schema\input;
use nur\sery\wip\php\access\IAccess;
use nur\sery\wip\php\access\KeyAccess; use nur\sery\wip\php\access\KeyAccess;
use nur\sery\wip\php\access\PropertyAccess; use nur\sery\wip\php\access\PropertyAccess;
@ -26,7 +27,7 @@ class Input implements IInput {
} }
} }
protected KeyAccess $access; protected IAccess $access;
/** tester si la valeur existe sans tenir compte de $allowEmpty */ /** tester si la valeur existe sans tenir compte de $allowEmpty */
function isPresent($key=null): bool { function isPresent($key=null): bool {