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

171 lines
4.6 KiB
PHP

<?php
namespace nulib\schema\_scalar;
use nulib\cl;
use nulib\ref\schema\ref_analyze;
use nulib\ref\schema\ref_schema;
use nulib\ValueException;
use nulib\schema\Result;
use nulib\schema\Schema;
use nulib\schema\Wrapper;
use Throwable;
/**
* Class ScalarResult: résultat de l'analyse ou de la normalisation d'une valeur
*/
class ScalarResult extends Result {
function getKeys(): array {
return ScalarSchema::KEYS;
}
function select($key): Result {
if ($key !== null) throw ValueException::invalid_key($key);
return $this;
}
protected array $result;
function reset(): void {
$this->result = array_merge(
array_fill_keys(static::KEYS, null), [
"resultAvailable" => false,
"present" => false,
"available" => false,
"null" => false,
"valid" => false,
"normalized" => false,
]);
}
function __get(string $name) {
return $this->result[$name];
}
function __set(string $name, $value): void {
$this->result[$name] = $value;
}
protected function getMessage(string $key, Schema $schema): string {
$message = cl::get($schema->messages, $key);
if ($message !== null) return $message;
return cl::get(ref_schema::MESSAGES, $key);
}
function setMissing( Schema $schema): int {
$this->resultAvailable = true;
$this->present = false;
$this->available = false;
if (!$schema->required) {
$this->null = false;
$this->valid = true;
$this->normalized = true;
return ref_analyze::NORMALIZED;
} else {
$this->messageKey = $messageKey = "missing";
$this->message = $this->getMessage($messageKey, $schema);
return ref_analyze::MISSING;
}
}
function setUnavailable( Schema $schema): int {
$this->resultAvailable = true;
$this->present = true;
$this->available = false;
if (!$schema->required) {
$this->null = false;
$this->valid = true;
$this->normalized = true;
return ref_analyze::NORMALIZED;
} else {
$this->messageKey = $messageKey = "unavailable";
$this->message = $this->getMessage($messageKey, $schema);
return ref_analyze::UNAVAILABLE;
}
}
function setNull( Schema $schema): int {
$this->resultAvailable = true;
$this->present = true;
$this->available = true;
$this->null = true;
if ($schema->nullable) {
$this->valid = true;
$this->normalized = true;
return ref_analyze::NORMALIZED;
} else {
$this->messageKey = $messageKey = "null";
$this->message = $this->getMessage($messageKey, $schema);
return ref_analyze::NULL;
}
}
function setInvalid($value, Schema $schema, ?Throwable $exception=null): int {
$this->resultAvailable = true;
$this->present = true;
$this->available = true;
$this->null = false;
$this->valid = false;
$this->origValue = $value;
$this->messageKey = $messageKey = "invalid";
$message = $this->getMessage($messageKey, $schema);
if ($exception !== null) {
$tmessage = ValueException::get_message($exception);
if ($tmessage) $message = $tmessage;
}
$this->message = $message;
$this->exception = $exception;
return ref_analyze::INVALID;
}
function addInvalidMessage(Wrapper $wrapper): void {
$this->resultAvailable = true;
$this->present = true;
$this->available = true;
$this->null = false;
$this->valid = false;
$this->messageKey = "invalid";
$result = $wrapper->getResult();
$resultException = $result->exception;
$resultMessage = $result->message;
if ($resultException !== null) {
$tmessage = ValueException::get_message($resultException);
if ($tmessage) {
if ($resultMessage !== null) $resultMessage .= ": ";
$resultMessage .= $tmessage;
}
}
$message = $this->message;
if ($message) $message .= "\n";
$message .= $resultMessage;
$this->message = $message;
}
function setValid($normalizedValue=null): int {
$this->resultAvailable = true;
$this->present = true;
$this->available = true;
$this->null = false;
$this->valid = true;
$this->normalizedValue = $normalizedValue;
return ref_analyze::VALID;
}
function setNormalized(): int {
$this->resultAvailable = true;
$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) {
$exception = $this->exception;
if ($exception !== null) throw $exception;
else throw new ValueException($this->message);
}
}
}