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-04-05 08:31:49 +04:00
|
|
|
use nur\sery\cl;
|
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;
|
2023-11-28 08:20:33 +04:00
|
|
|
$allowEmpty = cl::get($params, "allow_empty");
|
|
|
|
if ($allowEmpty === null) $allowEmpty = static::ALLOW_EMPTY;
|
|
|
|
$this->allowEmpty = $allowEmpty;
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/** tester si la valeur existe sans tenir compte de $allowEmpty */
|
2023-12-03 22:44:29 +04:00
|
|
|
function isPresent($key=null): bool {
|
2023-11-28 08:20:33 +04:00
|
|
|
if ($key === null) return true;
|
|
|
|
$dest = $this->dest;
|
|
|
|
return $dest !== null && array_key_exists($key, $dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool comment considérer une chaine vide: "" si allowEmpty, null sinon
|
|
|
|
*/
|
|
|
|
protected $allowEmpty;
|
|
|
|
|
|
|
|
/** tester si la valeur est disponible en tenant compte de $allowEmpty */
|
2023-12-03 22:44:29 +04:00
|
|
|
function isAvailable($key=null): bool {
|
2023-11-09 10:03:35 +04:00
|
|
|
if ($key === null) return true;
|
2023-11-28 08:20:33 +04:00
|
|
|
$dest = $this->dest;
|
|
|
|
if ($dest === null || !array_key_exists($key, $dest)) return false;
|
|
|
|
return $this->allowEmpty || $dest[$key] !== "";
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function get($key=null) {
|
2023-11-28 08:20:33 +04:00
|
|
|
$dest = $this->dest;
|
|
|
|
if ($key === null) return $dest;
|
|
|
|
if ($dest === null || !array_key_exists($key, $dest)) return null;
|
|
|
|
$value = $dest[$key];
|
|
|
|
if ($value === "" && !$this->allowEmpty) return null;
|
|
|
|
return $value;
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function set($value, $key=null): void {
|
2023-11-27 22:39:35 +04:00
|
|
|
if ($key === null) $this->dest = $value;
|
|
|
|
else $this->dest[$key] = $value;
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|
2023-12-28 19:33:13 +04:00
|
|
|
|
|
|
|
function unset($key=null): void {
|
|
|
|
if ($key === null) $this->dest = null;
|
|
|
|
else unset($this->dest[$key]);
|
|
|
|
}
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|