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

99 lines
2.6 KiB
PHP
Raw Normal View History

2023-11-09 10:03:35 +04:00
<?php
namespace nur\sery\schema\values;
2023-11-27 22:39:35 +04:00
use nur\sery\schema\input\Input;
use nur\sery\schema\Result;
use nur\sery\schema\schemas\ScalarSchema;
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 */
protected $key;
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-27 22:39:35 +04:00
function reset(&$dest, $key=null, bool $verifix=true): Value {
if ($dest instanceof Input) $input = $dest;
else $input = new Input($dest);
$this->input = $input;
$this->key = $key;
$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 {
return $this->input->exists($this->key);
}
2023-11-09 10:03:35 +04:00
function get($default=null) {
$key = $this->key;
2023-11-27 22:39:35 +04:00
$input = $this->input;
if ($input->exists($key)) return $input->get($key);
2023-11-09 10:03:35 +04:00
else return $default;
}
function set($value): self {
2023-11-27 22:39:35 +04:00
$this->input->set($value, $this->key);
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-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;
}
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-09 10:03:35 +04:00
$key = $this->key;
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-09 10:03:35 +04:00
$key = $this->key;
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
}
}