2024-08-17 17:03:07 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\wip\php\access;
|
|
|
|
|
2024-08-17 18:11:11 +04:00
|
|
|
use ArrayAccess;
|
2024-08-17 17:03:07 +04:00
|
|
|
use nur\sery\cl;
|
|
|
|
|
|
|
|
/**
|
2024-08-17 21:34:05 +04:00
|
|
|
* Class KeyAccess: accès à une valeur d'une clé dans un tableau
|
2024-08-17 17:03:07 +04:00
|
|
|
*/
|
|
|
|
class KeyAccess extends AbstractAccess {
|
2024-08-17 21:34:05 +04:00
|
|
|
function __construct(&$dest, $key, ?array $params=null) {
|
2024-08-17 18:11:11 +04:00
|
|
|
$this->dest =& $dest;
|
2024-08-17 17:03:07 +04:00
|
|
|
$this->key = $key;
|
2024-08-17 21:34:05 +04:00
|
|
|
$this->allowFalse = $params["allow_false"] ?? false;
|
|
|
|
$this->allowEmpty = $params["allow_empty"] ?? true;
|
2024-08-17 17:03:07 +04:00
|
|
|
}
|
|
|
|
|
2024-08-17 18:11:11 +04:00
|
|
|
/** @var array|ArrayAccess */
|
|
|
|
protected $dest;
|
|
|
|
|
2024-08-17 17:03:07 +04:00
|
|
|
/** @var int|string */
|
|
|
|
protected $key;
|
|
|
|
|
2024-08-17 21:34:05 +04:00
|
|
|
protected bool $allowFalse;
|
|
|
|
|
|
|
|
protected bool $allowEmpty;
|
|
|
|
|
2024-08-17 18:11:11 +04:00
|
|
|
function exists(): bool {
|
2024-08-17 21:34:05 +04:00
|
|
|
$key = $this->key;
|
|
|
|
if ($key === null) return false;
|
|
|
|
return cl::has($this->dest, $key);
|
2024-08-17 17:03:07 +04:00
|
|
|
}
|
|
|
|
|
2024-08-17 18:19:15 +04:00
|
|
|
function available(): bool {
|
2024-08-17 21:34:05 +04:00
|
|
|
if (!$this->exists()) return false;
|
|
|
|
$value = $this->get();
|
|
|
|
if ($value === false) return $this->allowFalse;
|
|
|
|
if ($value === "") return $this->allowEmpty;
|
|
|
|
return true;
|
2024-08-17 18:19:15 +04:00
|
|
|
}
|
|
|
|
|
2024-08-17 18:11:11 +04:00
|
|
|
function get($default=null) {
|
2024-08-17 17:03:07 +04:00
|
|
|
if ($this->key === null) return $default;
|
2024-08-17 21:34:05 +04:00
|
|
|
$value = cl::get($this->dest, $this->key, $default);
|
|
|
|
if ($value === false && !$this->allowFalse) return $default;
|
|
|
|
if ($value === "" && !$this->allowEmpty) return $default;
|
|
|
|
return $value;
|
2024-08-17 17:03:07 +04:00
|
|
|
}
|
|
|
|
|
2024-08-17 18:11:11 +04:00
|
|
|
function set($value): void {
|
2024-08-17 17:03:07 +04:00
|
|
|
if ($this->key === null) return;
|
2024-08-17 18:11:11 +04:00
|
|
|
cl::set($this->dest, $this->key, $value);
|
2024-08-17 17:03:07 +04:00
|
|
|
}
|
|
|
|
|
2024-08-17 18:11:11 +04:00
|
|
|
function del(): void {
|
2024-08-17 17:03:07 +04:00
|
|
|
if ($this->key === null) return;
|
2024-08-17 18:11:11 +04:00
|
|
|
cl::del($this->dest, $this->key);
|
2024-08-17 17:03:07 +04:00
|
|
|
}
|
|
|
|
}
|