From ab7f7942d6b12d1c63e1a30b94a9ea58e45cd65b Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Sat, 17 Aug 2024 21:46:00 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- wip/php/access/KeyAccess.php | 14 +++++++++++++- wip/php/access/ValueAccess.php | 33 +++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/wip/php/access/KeyAccess.php b/wip/php/access/KeyAccess.php index 3671c36..cd6dcf8 100644 --- a/wip/php/access/KeyAccess.php +++ b/wip/php/access/KeyAccess.php @@ -11,6 +11,7 @@ class KeyAccess extends AbstractAccess { function __construct(&$dest, $key, ?array $params=null) { $this->dest =& $dest; $this->key = $key; + $this->allowNull = $params["allow_null"] ?? true; $this->allowFalse = $params["allow_false"] ?? false; $this->allowEmpty = $params["allow_empty"] ?? true; } @@ -18,9 +19,16 @@ class KeyAccess extends AbstractAccess { /** @var array|ArrayAccess */ protected $dest; + function reset(&$dest): self { + $this->dest =& $dest; + return $this; + } + /** @var int|string */ protected $key; + protected bool $allowNull; + protected bool $allowFalse; protected bool $allowEmpty; @@ -28,7 +36,10 @@ class KeyAccess extends AbstractAccess { function exists(): bool { $key = $this->key; if ($key === null) return false; - return cl::has($this->dest, $key); + $exists = cl::has($this->dest, $key); + if (!$exists) return false; + if ($this->allowNull) return true; + return cl::get($this->dest, $key) !== null; } function available(): bool { @@ -42,6 +53,7 @@ class KeyAccess extends AbstractAccess { function get($default=null) { if ($this->key === null) return $default; $value = cl::get($this->dest, $this->key, $default); + if ($value === null && !$this->allowNull) return $default; if ($value === false && !$this->allowFalse) return $default; if ($value === "" && !$this->allowEmpty) return $default; return $value; diff --git a/wip/php/access/ValueAccess.php b/wip/php/access/ValueAccess.php index 86c733f..910e7c8 100644 --- a/wip/php/access/ValueAccess.php +++ b/wip/php/access/ValueAccess.php @@ -2,27 +2,48 @@ namespace nur\sery\wip\php\access; /** - * Class ValueAccess: accès à une valeur scalaire + * Class ValueAccess: accès à une valeur unitaire */ class ValueAccess extends AbstractAccess { - function __construct(&$dest) { + function __construct(&$dest, ?array $params=null) { $this->dest =& $dest; + $this->allowNull = $params["allow_null"] ?? false; + $this->allowFalse = $params["allow_false"] ?? true; + $this->allowEmpty = $params["allow_empty"] ?? true; } /** @var mixed */ protected $dest; + function reset(&$dest): self { + $this->dest =& $dest; + return $this; + } + + protected bool $allowNull; + + protected bool $allowFalse; + + protected bool $allowEmpty; + function exists(): bool { - return $this->dest !== null; + return $this->allowNull || $this->dest !== null; } function available(): bool { - return $this->exists(); + if (!$this->exists()) return false; + $value = $this->get(); + if ($value === false) return $this->allowFalse; + if ($value === "") return $this->allowEmpty; + return true; } function get($default=null) { - if ($this->dest === null) return $default; - else return $this->dest; + $value = $this->dest; + if ($value === null && !$this->allowNull) return $default; + if ($value === false && !$this->allowFalse) return $default; + if ($value === "" && !$this->allowEmpty) return $default; + return $value; } function set($value): void {