nur-sery/wip/schema/input/Input.php

67 lines
1.6 KiB
PHP
Raw Normal View History

2023-11-09 10:03:35 +04:00
<?php
2024-05-23 08:15:28 +04:00
namespace nur\sery\wip\schema\input;
2023-11-09 10:03:35 +04:00
2024-08-17 22:29:55 +04:00
use nur\sery\wip\php\access\IAccess;
use nur\sery\wip\php\access\KeyAccess;
use nur\sery\wip\php\access\ValueAccess;
2023-11-28 08:20:33 +04:00
2023-11-09 10:03:35 +04:00
/**
* Class Input: accès à une valeur
*
* cette implémentation lit depuis et écrit dans une référence
*/
class Input {
2023-11-28 08:20:33 +04:00
const ALLOW_EMPTY = true;
function __construct(&$dest=null, ?array $params=null) {
2023-11-27 22:39:35 +04:00
$this->dest =& $dest;
2024-08-17 22:29:55 +04:00
$this->allowEmpty = $params["allow_empty"] ?? static::ALLOW_EMPTY;
2023-11-09 10:03:35 +04:00
}
2024-08-17 22:29:55 +04:00
/** @var mixed */
2023-11-27 22:39:35 +04:00
protected $dest;
2023-11-09 10:03:35 +04:00
2023-11-28 08:20:33 +04:00
/**
* @var bool comment considérer une chaine vide: "" si allowEmpty, null sinon
*/
protected $allowEmpty;
2024-08-17 22:29:55 +04:00
protected ?ValueAccess $valueAccess = null;
protected ?array $keyAccess = null;
protected function access($key): IAccess {
if ($key === null) {
return $this->valueAccess ??= new ValueAccess($this->dest, [
"allow_null" => true,
"allow_empty" => $this->allowEmpty,
]);
} else {
return $this->keyAccess[$key] ??= new KeyAccess($this->dest, $key, [
"allow_empty" => $this->allowEmpty,
]);
}
}
/** tester si la valeur existe sans tenir compte de $allowEmpty */
function isPresent($key=null): bool {
return $this->access($key)->exists();
}
2023-11-28 08:20:33 +04:00
/** tester si la valeur est disponible en tenant compte de $allowEmpty */
2023-12-03 22:44:29 +04:00
function isAvailable($key=null): bool {
2024-08-17 22:29:55 +04:00
return $this->access($key)->available();
2023-11-09 10:03:35 +04:00
}
function get($key=null) {
2024-08-17 22:29:55 +04:00
return $this->access($key)->get();
2023-11-09 10:03:35 +04:00
}
function set($value, $key=null): void {
2024-08-17 22:29:55 +04:00
$this->access($key)->set($value);
2023-11-09 10:03:35 +04:00
}
2023-12-28 19:33:13 +04:00
function unset($key=null): void {
2024-08-17 22:29:55 +04:00
$this->access($key)->del();
2023-12-28 19:33:13 +04:00
}
2023-11-09 10:03:35 +04:00
}