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

41 lines
833 B
PHP

<?php
namespace nur\sery\wip\php\access;
use ArrayAccess;
use nur\sery\cl;
/**
* Class KeyAccess: accès à une valeur d'un tableau
*/
class KeyAccess extends AbstractAccess {
function __construct(&$dest, $key) {
$this->dest =& $dest;
$this->key = $key;
}
/** @var array|ArrayAccess */
protected $dest;
/** @var int|string */
protected $key;
function exists(): bool {
return $this->key !== null && cl::has($this->dest, $this->key);
}
function get($default=null) {
if ($this->key === null) return $default;
return cl::get($this->dest, $this->key, $default);
}
function set($value): void {
if ($this->key === null) return;
cl::set($this->dest, $this->key, $value);
}
function del(): void {
if ($this->key === null) return;
cl::del($this->dest, $this->key);
}
}