modifs.mineures sans commentaires
This commit is contained in:
parent
1fc4e7637b
commit
39af99ffa4
@ -3,7 +3,7 @@ namespace nur\sery\wip\php\access;
|
||||
|
||||
use nulib\cl;
|
||||
|
||||
class ChainAccess extends AbstractAccess {
|
||||
class ChainKeyAccess extends AbstractAccess {
|
||||
function __construct(IAccess $access, $key) {
|
||||
parent::__construct();
|
||||
$this->access = $access;
|
||||
@ -52,6 +52,6 @@ class ChainAccess extends AbstractAccess {
|
||||
}
|
||||
|
||||
function addKey($key): IAccess {
|
||||
return new ChainAccess($this->access, cl::merge($this->key, $key));
|
||||
return new ChainKeyAccess($this->access, cl::merge($this->key, $key));
|
||||
}
|
||||
}
|
95
src/php/access/PropertyAccess.php
Normal file
95
src/php/access/PropertyAccess.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
namespace nur\sery\wip\php\access;
|
||||
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionProperty;
|
||||
|
||||
class PropertyAccess extends AbstractAccess {
|
||||
function __construct(object &$object, $name, ?array $params=null) {
|
||||
parent::__construct($params);
|
||||
$this->object =& $object;
|
||||
$this->class = new ReflectionClass($object);
|
||||
$this->name = $name;
|
||||
$this->allowNull = $params["allow_null"] ?? true;
|
||||
$this->allowFalse = $params["allow_false"] ?? false;
|
||||
}
|
||||
|
||||
protected object $object;
|
||||
|
||||
protected ReflectionClass $class;
|
||||
|
||||
protected string $name;
|
||||
|
||||
protected bool $allowNull;
|
||||
|
||||
protected bool $allowFalse;
|
||||
|
||||
function reset(object &$object): self {
|
||||
$this->object =& $object;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function exists(): bool {
|
||||
return $this->class->hasProperty($this->name)
|
||||
|| property_exists($this->object, $this->name);
|
||||
}
|
||||
|
||||
protected function _property(): ReflectionProperty {
|
||||
$property = $this->class->getProperty($this->name);
|
||||
$property->setAccessible(true);
|
||||
return $property;
|
||||
}
|
||||
|
||||
protected function _get($default=null) {
|
||||
try {
|
||||
return $this->_property()->getValue($this->object);
|
||||
} catch (ReflectionException $e) {
|
||||
}
|
||||
$name = $this->name;
|
||||
if (property_exists($this->object, $name)) {
|
||||
return $this->object->$name;
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
function available(): bool {
|
||||
if (!$this->exists()) return false;
|
||||
$value = $this->_get();
|
||||
if ($value === "") return $this->allowEmpty;
|
||||
if ($value === null) return $this->allowNull;
|
||||
if ($value === false) return $this->allowFalse;
|
||||
return true;
|
||||
}
|
||||
|
||||
function get($default=null) {
|
||||
if (!$this->exists()) return $default;
|
||||
$value = $this->_get();
|
||||
if ($value === "" && !$this->allowEmpty) return $default;
|
||||
if ($value === null && !$this->allowNull) return $default;
|
||||
if ($value === false && !$this->allowFalse) return $default;
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function _set($value): void {
|
||||
try {
|
||||
$this->_property()->setValue($this->object, $value);
|
||||
} catch (ReflectionException $e) {
|
||||
$name = $this->name;
|
||||
$this->object->$name = $value;
|
||||
}
|
||||
}
|
||||
|
||||
function set($value): void {
|
||||
$this->_set($value);
|
||||
}
|
||||
|
||||
function del(): void {
|
||||
$this->_set(null);
|
||||
}
|
||||
|
||||
function addKey($key): IAccess {
|
||||
return new ChainKeyAccess($this, $key);
|
||||
}
|
||||
}
|
@ -55,6 +55,6 @@ class ShadowAccess extends AbstractAccess {
|
||||
}
|
||||
|
||||
function addKey($key): IAccess {
|
||||
return new ChainAccess($this, $key);
|
||||
return new ChainKeyAccess($this, $key);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user