From 39af99ffa4890cca1b5783d24ccd28e568f51222 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 19 Mar 2025 16:56:36 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- .../{ChainAccess.php => ChainKeyAccess.php} | 4 +- src/php/access/PropertyAccess.php | 95 +++++++++++++++++++ src/php/access/ShadowAccess.php | 2 +- 3 files changed, 98 insertions(+), 3 deletions(-) rename src/php/access/{ChainAccess.php => ChainKeyAccess.php} (91%) create mode 100644 src/php/access/PropertyAccess.php diff --git a/src/php/access/ChainAccess.php b/src/php/access/ChainKeyAccess.php similarity index 91% rename from src/php/access/ChainAccess.php rename to src/php/access/ChainKeyAccess.php index d08be7f..f2460f8 100644 --- a/src/php/access/ChainAccess.php +++ b/src/php/access/ChainKeyAccess.php @@ -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)); } } diff --git a/src/php/access/PropertyAccess.php b/src/php/access/PropertyAccess.php new file mode 100644 index 0000000..1550173 --- /dev/null +++ b/src/php/access/PropertyAccess.php @@ -0,0 +1,95 @@ +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); + } +} diff --git a/src/php/access/ShadowAccess.php b/src/php/access/ShadowAccess.php index e4f1211..ab0e33a 100644 --- a/src/php/access/ShadowAccess.php +++ b/src/php/access/ShadowAccess.php @@ -55,6 +55,6 @@ class ShadowAccess extends AbstractAccess { } function addKey($key): IAccess { - return new ChainAccess($this, $key); + return new ChainKeyAccess($this, $key); } }