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-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;
|
|
|
|
$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-09 10:34:36 +04:00
|
|
|
/** @var ?Result 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();
|
|
|
|
return $this;
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|
|
|
|
|
2023-11-27 22:39:35 +04:00
|
|
|
function exists(): bool {
|
2023-11-28 00:20:42 +04:00
|
|
|
return $this->input->exists($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-11-28 00:20:42 +04:00
|
|
|
if ($input->exists($destKey)) return $input->get($destKey);
|
2023-11-09 10:03:35 +04:00
|
|
|
else return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
function set($value): self {
|
2023-11-28 00:20:42 +04:00
|
|
|
$this->input->set($value, $this->destKey);
|
2023-11-09 10:03:35 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var IType */
|
|
|
|
protected $type;
|
|
|
|
|
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-11-27 22:39:35 +04:00
|
|
|
function valid(): bool {
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalized(): 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
|
|
|
}
|
|
|
|
}
|