modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-08-17 21:46:00 +04:00
parent b2a39dea31
commit ab7f7942d6
2 changed files with 40 additions and 7 deletions

View File

@ -11,6 +11,7 @@ class KeyAccess extends AbstractAccess {
function __construct(&$dest, $key, ?array $params=null) { function __construct(&$dest, $key, ?array $params=null) {
$this->dest =& $dest; $this->dest =& $dest;
$this->key = $key; $this->key = $key;
$this->allowNull = $params["allow_null"] ?? true;
$this->allowFalse = $params["allow_false"] ?? false; $this->allowFalse = $params["allow_false"] ?? false;
$this->allowEmpty = $params["allow_empty"] ?? true; $this->allowEmpty = $params["allow_empty"] ?? true;
} }
@ -18,9 +19,16 @@ class KeyAccess extends AbstractAccess {
/** @var array|ArrayAccess */ /** @var array|ArrayAccess */
protected $dest; protected $dest;
function reset(&$dest): self {
$this->dest =& $dest;
return $this;
}
/** @var int|string */ /** @var int|string */
protected $key; protected $key;
protected bool $allowNull;
protected bool $allowFalse; protected bool $allowFalse;
protected bool $allowEmpty; protected bool $allowEmpty;
@ -28,7 +36,10 @@ class KeyAccess extends AbstractAccess {
function exists(): bool { function exists(): bool {
$key = $this->key; $key = $this->key;
if ($key === null) return false; 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 { function available(): bool {
@ -42,6 +53,7 @@ class KeyAccess extends AbstractAccess {
function get($default=null) { function get($default=null) {
if ($this->key === null) return $default; if ($this->key === null) return $default;
$value = cl::get($this->dest, $this->key, $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 === false && !$this->allowFalse) return $default;
if ($value === "" && !$this->allowEmpty) return $default; if ($value === "" && !$this->allowEmpty) return $default;
return $value; return $value;

View File

@ -2,27 +2,48 @@
namespace nur\sery\wip\php\access; namespace nur\sery\wip\php\access;
/** /**
* Class ValueAccess: accès à une valeur scalaire * Class ValueAccess: accès à une valeur unitaire
*/ */
class ValueAccess extends AbstractAccess { class ValueAccess extends AbstractAccess {
function __construct(&$dest) { function __construct(&$dest, ?array $params=null) {
$this->dest =& $dest; $this->dest =& $dest;
$this->allowNull = $params["allow_null"] ?? false;
$this->allowFalse = $params["allow_false"] ?? true;
$this->allowEmpty = $params["allow_empty"] ?? true;
} }
/** @var mixed */ /** @var mixed */
protected $dest; protected $dest;
function reset(&$dest): self {
$this->dest =& $dest;
return $this;
}
protected bool $allowNull;
protected bool $allowFalse;
protected bool $allowEmpty;
function exists(): bool { function exists(): bool {
return $this->dest !== null; return $this->allowNull || $this->dest !== null;
} }
function available(): bool { 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) { function get($default=null) {
if ($this->dest === null) return $default; $value = $this->dest;
else return $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 { function set($value): void {