2023-11-09 10:03:35 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\schema\values;
|
|
|
|
|
2023-11-25 10:04:24 +04:00
|
|
|
use nur\sery\schema\ScalarSchema;
|
2023-11-09 10:03:35 +04:00
|
|
|
use nur\sery\schema\types\IType;
|
|
|
|
|
|
|
|
class ScalarValue implements IValue {
|
|
|
|
use TValue;
|
|
|
|
|
2023-11-25 10:04:24 +04:00
|
|
|
/** @var ScalarSchema schéma de cette valeur */
|
|
|
|
protected $schema;
|
|
|
|
|
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-25 10:04:24 +04:00
|
|
|
function __construct(&$value, ScalarSchema $schema, $key=null, bool $verifix=true) {
|
2023-11-09 10:03:35 +04:00
|
|
|
$this->schema = $schema;
|
|
|
|
$this->reset($value, $key, $verifix);
|
2023-11-25 10:04:24 +04:00
|
|
|
#XXX résoudre les type dans reset()?
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function isScalar(?ScalarValue &$scalar=null): bool { $scalar = $this; return true; }
|
2023-11-25 10:04:24 +04:00
|
|
|
function isList(?ListValue &$list=null): bool { return false; }
|
2023-11-09 10:03:35 +04:00
|
|
|
function isAssoc(?AssocValue &$assoc=null): bool { return false; }
|
|
|
|
|
|
|
|
function get($default=null) {
|
|
|
|
$key = $this->key;
|
|
|
|
if ($key === null) return $this->value;
|
|
|
|
elseif (array_key_exists($key, $this->value)) return $this->value[$key];
|
|
|
|
else return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
function set($value): self {
|
|
|
|
$key = $this->key;
|
|
|
|
if ($key === null) $this->value = $value;
|
|
|
|
else $this->value[$key] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var IType */
|
|
|
|
protected $type;
|
|
|
|
|
|
|
|
function type(): IType {
|
2023-11-25 10:04:24 +04:00
|
|
|
if ($this->type === null) $this->type = $this->schema->getType($this->key);
|
2023-11-09 10:03:35 +04:00
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
function exists(): bool {
|
|
|
|
return $this->type()->exists($this->value, $this->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
$type = $this->type();
|
|
|
|
$key = $this->key;
|
|
|
|
if ($key === null) $modified = $type->verifix($this->value, $throw, $result);
|
|
|
|
else $modified = $type->verifix($this->value[$key], $throw, $result);
|
|
|
|
$this->result = $result;
|
|
|
|
return $modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse($value, bool $throw=true, ?Result &$result=null) {
|
|
|
|
$this->type()->verifix($value, $throw, $result);
|
|
|
|
$this->set($value);
|
|
|
|
$this->result = $result;
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function format(?string $format=null): string {
|
|
|
|
$type = $this->type();
|
|
|
|
$key = $this->key;
|
|
|
|
if ($key === null) return $type->format($this->value, $format);
|
|
|
|
else return $type->format($this->value[$key], $format);
|
|
|
|
}
|
|
|
|
}
|