nur-sery/wip/php/access/KeyAccess.php

45 lines
923 B
PHP
Raw Normal View History

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;
/**
* Class KeyAccess: accès à une valeur d'un tableau
*/
class KeyAccess extends AbstractAccess {
2024-08-17 18:11:11 +04:00
function __construct(&$dest, $key) {
$this->dest =& $dest;
2024-08-17 17:03:07 +04:00
$this->key = $key;
}
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 18:11:11 +04:00
function exists(): bool {
return $this->key !== null && cl::has($this->dest, $this->key);
2024-08-17 17:03:07 +04:00
}
2024-08-17 18:19:15 +04:00
function available(): bool {
return $this->exists() && $this->get() !== false;
}
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 18:11:11 +04:00
return cl::get($this->dest, $this->key, $default);
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
}
}