2023-11-28 00:20:42 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\schema\_scalar;
|
|
|
|
|
2023-11-28 08:20:33 +04:00
|
|
|
use nulib\cl;
|
|
|
|
use nur\sery\schema\ref\ref_analyze;
|
2023-11-28 00:20:42 +04:00
|
|
|
use nur\sery\schema\Result;
|
2023-11-28 08:20:33 +04:00
|
|
|
use nur\sery\schema\ValueException;
|
2023-11-28 00:20:42 +04:00
|
|
|
|
2023-11-28 08:20:33 +04:00
|
|
|
/**
|
|
|
|
* Class ScalarResult: résultat de l'analyse ou de la normalisation d'une valeur
|
|
|
|
*
|
|
|
|
* @property bool $missing la valeur est-elle inexistante?
|
|
|
|
* @property bool $null si elle existe, la valeur est-nulle?
|
|
|
|
* @property bool $valid si elle existe, la valeur est-elle valide?
|
|
|
|
* @property bool $normalized si la valeur est valide, est-elle normalisée?
|
|
|
|
* @property string|null $orig valeur originale avant analyse avec parse()
|
|
|
|
* @property string|null $message message si la valeur n'est pas valide
|
|
|
|
*/
|
2023-11-28 00:20:42 +04:00
|
|
|
class ScalarResult extends Result {
|
2023-11-28 08:20:33 +04:00
|
|
|
const KEYS = ["missing", "null", "valid", "normalized", "orig", "message"];
|
|
|
|
|
|
|
|
function isScalar(?ScalarResult &$scalar=null): bool { $scalar = $this; return true; }
|
|
|
|
|
|
|
|
function getKeys(): array {
|
|
|
|
return [null];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResult($key=null): Result {
|
|
|
|
if ($key === null) return $this;
|
|
|
|
else throw ValueException::invalid_key($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $result;
|
|
|
|
|
|
|
|
function reset(): void {
|
|
|
|
$this->result = array_merge(
|
|
|
|
array_fill_keys(static::KEYS, null), [
|
|
|
|
"missing" => true,
|
|
|
|
"null" => false,
|
|
|
|
"valid" => false,
|
|
|
|
"normalized" => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function __get(string $name) {
|
|
|
|
return $this->result[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
function __set(string $name, $value): void {
|
|
|
|
$this->result[$name] = $value;
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:53:40 +04:00
|
|
|
protected static function replace_key(string &$message, ?string $key): void {
|
|
|
|
if ($key) $message = str_replace("{key}", $key, $message);
|
|
|
|
else $message = str_replace("{key}: ", "", $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setMissing(ScalarSchema $schema): int {
|
2023-11-28 08:20:33 +04:00
|
|
|
$this->missing = true;
|
|
|
|
if (!$schema->required) {
|
|
|
|
$this->null = true;
|
|
|
|
$this->valid = true;
|
|
|
|
$this->normalized = true;
|
|
|
|
return ref_analyze::NORMALIZED;
|
|
|
|
} else {
|
|
|
|
$message = cl::get($schema->messages, "missing");
|
2023-11-28 08:53:40 +04:00
|
|
|
self::replace_key($message, $schema->name);
|
2023-11-28 08:20:33 +04:00
|
|
|
$this->message = $message;
|
|
|
|
return ref_analyze::MISSING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:53:40 +04:00
|
|
|
function setNull(ScalarSchema $schema): int {
|
2023-11-28 08:20:33 +04:00
|
|
|
$this->missing = true;
|
|
|
|
if ($schema->nullable) {
|
|
|
|
$this->valid = true;
|
|
|
|
$this->normalized = true;
|
|
|
|
return ref_analyze::NORMALIZED;
|
|
|
|
} else {
|
|
|
|
$message = cl::get($schema->messages, "null");
|
2023-11-28 08:53:40 +04:00
|
|
|
self::replace_key($message, $schema->name);
|
2023-11-28 08:20:33 +04:00
|
|
|
$this->message = $message;
|
|
|
|
return ref_analyze::NULL;
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 00:20:42 +04:00
|
|
|
}
|