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

128 lines
3.4 KiB
PHP
Raw Normal View History

2023-11-09 10:03:35 +04:00
<?php
2023-11-28 00:20:42 +04:00
namespace nur\sery\schema\_scalar;
2023-11-09 10:03:35 +04:00
2023-12-28 12:33:17 +04:00
use nulib\ValueException;
2023-11-27 22:39:35 +04:00
use nur\sery\schema\input\Input;
use nur\sery\schema\Result;
2023-11-09 10:03:35 +04:00
use nur\sery\schema\types\IType;
2023-11-27 22:39:35 +04:00
use nur\sery\schema\Value;
2023-11-09 10:03:35 +04:00
2023-11-27 22:39:35 +04:00
class ScalarValue extends Value {
function __construct(ScalarSchema $schema, &$dest=null, $key=null, bool $verifix=true) {
$this->schema = $schema;
2023-11-28 08:20:33 +04:00
$this->result = new ScalarResult();
2023-11-27 22:39:35 +04:00
$this->reset($dest, $key, $verifix);
}
function isScalar(?ScalarValue &$scalar=null): bool { $scalar = $this; return true; }
2023-11-09 10:03:35 +04:00
2023-11-25 10:04:24 +04:00
/** @var ScalarSchema schéma de cette valeur */
protected $schema;
2023-11-27 22:39:35 +04:00
/** @var Input source et destination de la valeur */
protected $input;
/** @var string|int clé de la valeur dans le tableau destination */
2023-11-28 00:20:42 +04:00
protected $destKey;
2023-11-27 22:39:35 +04:00
2023-11-28 08:20:33 +04:00
/** @var IType */
protected $type;
/** @var ?ScalarResult résultat de l'analyse de la valeur */
2023-11-25 10:04:24 +04:00
protected $result;
2023-11-09 10:34:36 +04:00
2023-11-28 00:20:42 +04:00
function reset(&$dest, $destKey=null, bool $verifix=true): Value {
2023-11-27 22:39:35 +04:00
if ($dest instanceof Input) $input = $dest;
else $input = new Input($dest);
$this->input = $input;
2023-11-28 00:20:42 +04:00
$this->destKey = $destKey;
2023-11-27 22:39:35 +04:00
$this->result = null;
#XXX résoudre les types ici?
if ($verifix) $this->verifix();
2023-11-28 08:20:33 +04:00
else $this->_analyze();
2023-11-27 22:39:35 +04:00
return $this;
2023-11-09 10:03:35 +04:00
}
2023-11-28 08:20:33 +04:00
function getKeys(): array {
return [null];
}
function getValue($key=null): Value {
if ($key === null) return $this;
throw ValueException::invalid_key($key);
}
/** analyser la valeur et résoudre son type */
function _analyze(): int {
$schema = $this->schema;
$input = $this->input;
$destKey = $this->destKey;
$result = $this->result;
$result->reset();
#XXX résoudre le type
2023-12-03 22:44:29 +04:00
if (!$input->isAvailable()) return $result->setUnavailable($schema);
2023-11-28 08:53:40 +04:00
$value = $input->get($destKey);
if ($value === null) return $result->setNull($schema);
2023-11-28 08:20:33 +04:00
}
2023-12-03 22:44:29 +04:00
function isPresent(): bool {
return $this->input->isPresent($this->destKey);
2023-11-28 08:20:33 +04:00
}
2023-12-03 22:44:29 +04:00
function isAvailable(): bool {
return $this->input->isAvailable($this->destKey);
2023-11-27 22:39:35 +04:00
}
2023-11-09 10:03:35 +04:00
function get($default=null) {
2023-11-28 00:20:42 +04:00
$destKey = $this->destKey;
2023-11-27 22:39:35 +04:00
$input = $this->input;
2023-12-03 22:44:29 +04:00
if ($input->isAvailable($destKey)) return $input->get($destKey);
2023-11-09 10:03:35 +04:00
else return $default;
}
2023-11-28 08:20:33 +04:00
function set($value, bool $verifix=true): Value {
2023-11-28 00:20:42 +04:00
$this->input->set($value, $this->destKey);
2023-11-28 08:20:33 +04:00
if ($verifix) $this->verifix();
2023-11-09 10:03:35 +04:00
return $this;
}
2023-11-27 22:39:35 +04:00
function getType(): IType {
2023-11-28 00:20:42 +04:00
if ($this->type === null) $this->type = $this->schema->getType($this->destKey);
2023-11-09 10:03:35 +04:00
return $this->type;
}
2023-12-03 22:44:29 +04:00
function isValid(): bool {
2023-11-27 22:39:35 +04:00
}
2023-12-03 22:44:29 +04:00
function isNormalized(): bool {
2023-11-09 10:03:35 +04:00
}
/**
* analyser, corriger éventuellement et normaliser la valeur
*
* si la valeur était déjà normalisée, retourner false.
*/
function verifix(bool $throw=true, ?Result &$result=null): bool {
2023-11-27 22:39:35 +04:00
$type = $this->getType();
2023-11-28 00:20:42 +04:00
$key = $this->destKey;
2023-11-27 22:39:35 +04:00
if ($key === null) $modified = $type->verifix($this->input, $throw, $result);
else $modified = $type->verifix($this->input[$key], $throw, $result);
2023-11-09 10:03:35 +04:00
$this->result = $result;
return $modified;
}
function parse($value, bool $throw=true, ?Result &$result=null) {
2023-11-27 22:39:35 +04:00
$this->getType()->verifix($value, $throw, $result);
2023-11-09 10:03:35 +04:00
$this->set($value);
$this->result = $result;
return $value;
}
function format(?string $format=null): string {
2023-11-27 22:39:35 +04:00
$type = $this->getType();
2023-11-28 00:20:42 +04:00
$key = $this->destKey;
2023-11-27 22:39:35 +04:00
if ($key === null) return $type->format($this->input, $format);
else return $type->format($this->input[$key], $format);
2023-11-09 10:03:35 +04:00
}
}