nur-sery/src/schema/_scalar/ScalarResult.php

166 lines
4.8 KiB
PHP
Raw Normal View History

2023-11-28 00:20:42 +04:00
<?php
namespace nur\sery\schema\_scalar;
2024-04-05 08:31:49 +04:00
use nur\sery\cl;
2023-12-30 14:52:30 +04:00
use nur\sery\ref\schema\ref_analyze;
use nur\sery\ref\schema\ref_schema;
2023-11-28 00:20:42 +04:00
use nur\sery\schema\Result;
2024-05-22 20:44:20 +04:00
use nur\sery\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
*
2023-12-28 22:48:06 +04:00
* @property bool $resultAvailable le résultat est-il disponible?
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-28 22:48:06 +04:00
const KEYS = ["resultAvailable", "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-28 22:48:06 +04:00
"resultAvailable" => false,
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 getMessage(string $key, ScalarSchema $schema): string {
2023-12-28 22:48:06 +04:00
$message = cl::get($schema->messages, $key);
2023-12-28 21:00:41 +04:00
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-28 22:48:06 +04:00
$this->resultAvailable = true;
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 {
2023-12-28 22:48:06 +04:00
$this->resultAvailable = true;
2023-12-03 22:44:29 +04:00
$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-28 22:48:06 +04:00
$this->resultAvailable = true;
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 22:48:06 +04:00
$this->resultAvailable = true;
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 {
2023-12-28 22:48:06 +04:00
$this->resultAvailable = true;
2023-12-28 19:33:13 +04:00
$this->present = true;
$this->available = true;
$this->null = false;
$this->valid = true;
return ref_analyze::VALID;
}
function setNormalized(): int {
2023-12-28 22:48:06 +04:00
$this->resultAvailable = true;
2023-12-28 19:33:13 +04:00
$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
}