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;
|
2023-12-28 12:33:17 +04:00
|
|
|
use nulib\ValueException;
|
2023-11-28 08:20:33 +04:00
|
|
|
use nur\sery\schema\ref\ref_analyze;
|
2023-12-28 21:00:41 +04:00
|
|
|
use nur\sery\schema\ref\ref_schema;
|
2023-11-28 00:20:42 +04:00
|
|
|
use nur\sery\schema\Result;
|
|
|
|
|
2023-11-28 08:20:33 +04:00
|
|
|
/**
|
|
|
|
* Class ScalarResult: résultat de l'analyse ou de la normalisation d'une valeur
|
|
|
|
*
|
2023-12-03 22:44:29 +04:00
|
|
|
* @property bool $present la valeur existe-t-elle?
|
|
|
|
* @property bool $available si la valeur existe, est-elle disponible?
|
2023-12-28 19:33:13 +04:00
|
|
|
* @property bool $null si la valeur est disponible, est-elle nulle?
|
|
|
|
* @property bool $valid si la valeur est disponible, est-elle valide?
|
2023-11-28 08:20:33 +04:00
|
|
|
* @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-12-03 22:44:29 +04:00
|
|
|
const KEYS = ["present", "available", "null", "valid", "normalized", "orig", "message"];
|
2023-11-28 08:20:33 +04:00
|
|
|
|
|
|
|
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), [
|
2023-12-03 22:44:29 +04:00
|
|
|
"present" => false,
|
|
|
|
"available" => false,
|
2023-11-28 08:20:33 +04:00
|
|
|
"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 {
|
2023-12-28 21:12:47 +04:00
|
|
|
if ($key) {
|
|
|
|
$message = str_replace("{key}", $key, $message);
|
|
|
|
} else {
|
|
|
|
$message = str_replace("{key}: ", "", $message);
|
|
|
|
$message = str_replace("cette valeur", "la valeur", $message);
|
|
|
|
}
|
2023-11-28 08:53:40 +04:00
|
|
|
}
|
|
|
|
|
2023-12-28 21:00:41 +04:00
|
|
|
protected static function replace_orig(string &$message, $orig): void {
|
|
|
|
$message = str_replace("{orig}", strval($orig), $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMessages(ScalarSchema $schema): ?array {
|
|
|
|
return $schema->messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMessage(string $key, ScalarSchema $schema): string {
|
|
|
|
$messages = $this->getMessages($schema);
|
|
|
|
$message = cl::get($messages, $key);
|
|
|
|
if ($message !== null) return $message;
|
|
|
|
return cl::get(ref_schema::MESSAGES, $key);
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:53:40 +04:00
|
|
|
function setMissing(ScalarSchema $schema): int {
|
2023-12-03 22:44:29 +04:00
|
|
|
$this->present = false;
|
|
|
|
$this->available = false;
|
2023-11-28 08:20:33 +04:00
|
|
|
if (!$schema->required) {
|
2023-12-03 22:44:29 +04:00
|
|
|
$this->null = false;
|
2023-11-28 08:20:33 +04:00
|
|
|
$this->valid = true;
|
|
|
|
$this->normalized = true;
|
|
|
|
return ref_analyze::NORMALIZED;
|
|
|
|
} else {
|
2023-12-28 21:00:41 +04:00
|
|
|
$message = $this->getMessage("missing", $schema);
|
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-12-03 22:44:29 +04:00
|
|
|
function setUnavailable(ScalarSchema $schema): int {
|
|
|
|
$this->present = true;
|
|
|
|
$this->available = false;
|
|
|
|
if (!$schema->required) {
|
|
|
|
$this->null = false;
|
|
|
|
$this->valid = true;
|
|
|
|
$this->normalized = true;
|
|
|
|
return ref_analyze::NORMALIZED;
|
|
|
|
} else {
|
2023-12-28 21:00:41 +04:00
|
|
|
$message = $this->getMessage("unavailable", $schema);
|
2023-12-03 22:44:29 +04:00
|
|
|
self::replace_key($message, $schema->name);
|
|
|
|
$this->message = $message;
|
|
|
|
return ref_analyze::UNAVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:53:40 +04:00
|
|
|
function setNull(ScalarSchema $schema): int {
|
2023-12-03 22:44:29 +04:00
|
|
|
$this->present = true;
|
|
|
|
$this->available = true;
|
|
|
|
$this->null = true;
|
2023-11-28 08:20:33 +04:00
|
|
|
if ($schema->nullable) {
|
|
|
|
$this->valid = true;
|
|
|
|
$this->normalized = true;
|
|
|
|
return ref_analyze::NORMALIZED;
|
|
|
|
} else {
|
2023-12-28 21:00:41 +04:00
|
|
|
$message = $this->getMessage("null", $schema);
|
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-12-28 19:33:13 +04:00
|
|
|
|
2023-12-28 21:00:41 +04:00
|
|
|
function setInvalid($value, ScalarSchema $schema): int {
|
2023-12-28 19:33:13 +04:00
|
|
|
$this->present = true;
|
|
|
|
$this->available = true;
|
|
|
|
$this->null = false;
|
|
|
|
$this->valid = false;
|
2023-12-28 21:00:41 +04:00
|
|
|
$this->orig = $value;
|
|
|
|
$message = $this->getMessage("invalid", $schema);
|
2023-12-28 19:33:13 +04:00
|
|
|
self::replace_key($message, $schema->name);
|
2023-12-28 21:00:41 +04:00
|
|
|
self::replace_orig($message, $schema->orig);
|
2023-12-28 19:33:13 +04:00
|
|
|
$this->message = $message;
|
|
|
|
return ref_analyze::INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setValid(): int {
|
|
|
|
$this->present = true;
|
|
|
|
$this->available = true;
|
|
|
|
$this->null = false;
|
|
|
|
$this->valid = true;
|
|
|
|
return ref_analyze::VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setNormalized(): int {
|
|
|
|
$this->present = true;
|
|
|
|
$this->available = true;
|
|
|
|
$this->null = false;
|
|
|
|
$this->valid = true;
|
|
|
|
$this->normalized = true;
|
|
|
|
return ref_analyze::NORMALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
function throw(bool $throw): void {
|
|
|
|
if ($throw) throw new ValueException($this->message);
|
|
|
|
}
|
2023-11-28 00:20:42 +04:00
|
|
|
}
|