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

141 lines
3.9 KiB
PHP
Raw Normal View History

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-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 {
if ($key) $message = str_replace("{key}", $key, $message);
else $message = str_replace("{key}: ", "", $message);
}
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 {
$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-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 {
$message = cl::get($schema->messages, "unavailable");
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 {
$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-12-28 19:33:13 +04:00
function setInvalid(ScalarSchema $schema): int {
$this->present = true;
$this->available = true;
$this->null = false;
$this->valid = false;
$message = cl::get($schema->messages, "invalid");
self::replace_key($message, $schema->name);
$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
}