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

57 lines
1.3 KiB
PHP

<?php
namespace nur\sery\wip\php\access;
/**
* Class ValueAccess: accès à une valeur unitaire
*/
class ValueAccess extends AbstractAccess {
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->allowNull || $this->dest !== null;
}
function available(): bool {
if (!$this->exists()) return false;
$value = $this->dest;
if ($value === false) return $this->allowFalse;
if ($value === "") return $this->allowEmpty;
return true;
}
function get($default=null) {
$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 {
$this->dest = $value;
}
function del(): void {
$this->dest = null;
}
}