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

57 lines
1.3 KiB
PHP
Raw Normal View History

2024-08-17 17:03:07 +04:00
<?php
namespace nur\sery\wip\php\access;
/**
2024-08-17 21:46:00 +04:00
* Class ValueAccess: accès à une valeur unitaire
2024-08-17 17:03:07 +04:00
*/
class ValueAccess extends AbstractAccess {
2024-08-17 21:46:00 +04:00
function __construct(&$dest, ?array $params=null) {
2024-08-17 18:11:11 +04:00
$this->dest =& $dest;
2024-08-17 21:46:00 +04:00
$this->allowNull = $params["allow_null"] ?? false;
$this->allowFalse = $params["allow_false"] ?? true;
$this->allowEmpty = $params["allow_empty"] ?? true;
2024-08-17 17:03:07 +04:00
}
2024-08-17 18:11:11 +04:00
/** @var mixed */
protected $dest;
2024-08-17 21:46:00 +04:00
function reset(&$dest): self {
$this->dest =& $dest;
return $this;
}
protected bool $allowNull;
protected bool $allowFalse;
protected bool $allowEmpty;
2024-08-17 18:11:11 +04:00
function exists(): bool {
2024-08-17 21:46:00 +04:00
return $this->allowNull || $this->dest !== null;
2024-08-17 18:11:11 +04:00
}
function available(): bool {
2024-08-17 21:46:00 +04:00
if (!$this->exists()) return false;
2024-08-17 22:29:55 +04:00
$value = $this->dest;
2024-08-17 21:46:00 +04:00
if ($value === false) return $this->allowFalse;
if ($value === "") return $this->allowEmpty;
return true;
2024-08-17 17:03:07 +04:00
}
2024-08-17 18:11:11 +04:00
function get($default=null) {
2024-08-17 21:46:00 +04:00
$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;
2024-08-17 17:03:07 +04:00
}
2024-08-17 18:11:11 +04:00
function set($value): void {
$this->dest = $value;
2024-08-17 17:03:07 +04:00
}
2024-08-17 18:11:11 +04:00
function del(): void {
$this->dest = null;
2024-08-17 17:03:07 +04:00
}
}