diff --git a/src/schema/Value.php b/src/schema/Value.php index c80239b..3d8efd3 100644 --- a/src/schema/Value.php +++ b/src/schema/Value.php @@ -59,7 +59,7 @@ abstract class Value implements ArrayAccess, IteratorAggregate { abstract function set($value): self; /** supprimer la valeur */ - abstract function unset(): void; + abstract function unset(): self; /** formatter la valeur pour affichage */ abstract function format($format=null): string; diff --git a/src/schema/_scalar/ScalarValue.php b/src/schema/_scalar/ScalarValue.php index 84c4355..26d6da0 100644 --- a/src/schema/_scalar/ScalarValue.php +++ b/src/schema/_scalar/ScalarValue.php @@ -10,9 +10,10 @@ use nur\sery\schema\types\IType; use nur\sery\schema\Value; class ScalarValue extends Value { - function __construct(ScalarSchema $schema, &$dest=null, $destKey=null, bool $defaultVerifix=true) { + function __construct(ScalarSchema $schema, &$dest=null, $destKey=null, bool $defaultVerifix=true, bool $defaultThrow=true) { $this->schema = $schema; $this->defaultVerifix = $defaultVerifix; + $this->defaultThrow = $defaultThrow; $this->result = new ScalarResult(); $this->reset($dest, $destKey); } @@ -31,6 +32,9 @@ class ScalarValue extends Value { /** @var bool */ protected $defaultVerifix; + /** @var bool */ + protected $defaultThrow; + /** @var IType type de la valeur après analyse */ protected $type; @@ -44,7 +48,7 @@ class ScalarValue extends Value { $this->destKey = $destKey; $this->type = null; $this->_analyze(); - if ($verifix === null) $verifix = $this->defaultVerifix; + if ($verifix == null) $verifix = $this->defaultVerifix; if ($verifix) $this->verifix(); return $this; } @@ -89,16 +93,12 @@ class ScalarValue extends Value { else return $result->setInvalid($schema); } - /** - * analyser, corriger éventuellement et normaliser la valeur - * - * si la valeur était déjà normalisée, retourner false. - */ - function verifix(bool $throw=true): bool { + function verifix(?bool $throw=null): bool { $type = $this->getType(); $destKey = $this->destKey; $value = $this->input->get($destKey); $modified = $type->verifix($value, $this->result); + if ($throw === null) $throw = $this->defaultThrow; if ($this->result->valid) $this->input->set($value, $destKey); else $this->result->throw($throw); return $modified; @@ -141,11 +141,12 @@ class ScalarValue extends Value { return $this; } - function unset(?bool $verifix=null): void { + function unset(?bool $verifix=null): Value { $this->input->unset($this->destKey); $this->_analyze(); if ($verifix === null) $verifix = $this->defaultVerifix; if ($verifix) $this->verifix(); + return $this; } function format($format=null): string { diff --git a/src/schema/types/IType.php b/src/schema/types/IType.php index 73dc77f..c9a043a 100644 --- a/src/schema/types/IType.php +++ b/src/schema/types/IType.php @@ -8,15 +8,25 @@ use nur\sery\schema\Result; * Interface IType: un type de données */ interface IType { - function canAnalyze(Input $input, $desyKey): bool; + /** ce type peut-il analyser la donnée dans $input[$destKey] ? */ + function canAnalyze(Input $input, $destKey): bool; - function isAvailable(Input $input, $desyKey): bool; + /** la donnée $input[$destKey] est-elle disponible? */ + function isAvailable(Input $input, $destKey): bool; + /** la valeur $value est-elle nulle? */ function isNull($value): bool; + /** la valeur $value est-elle valide et normalisée le cas échéant? */ function isValid($value, ?bool &$normalized=null): bool; - function verifix(&$value, ?Result &$result): bool; + /** + * analyser, corriger éventuellement et normaliser la valeur + * + * si la valeur était déjà normalisée, retourner false. + */ + function verifix(&$value, Result &$result): bool; - function format($value, $format=null); + /** formatter la valeur pour affichage */ + function format($value, $format=null): string; }