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 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;

View File

@ -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;

View File

@ -1,6 +1,7 @@
<?php
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\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 */
function isPresent($key=null): bool {