modifs.mineures sans commentaires
This commit is contained in:
parent
91e6c0dcd2
commit
189c7aba68
@ -269,6 +269,13 @@ abstract class Schema implements ArrayAccess {
|
|||||||
return $this->_definition;
|
return $this->_definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* retourner la liste des clés valides pour l'accès aux valeurs et résultats
|
||||||
|
*/
|
||||||
|
abstract function getKeys(): array;
|
||||||
|
|
||||||
|
abstract function getSchema($key): Schema;
|
||||||
|
|
||||||
/** retourner true si le schéma est de nature tableau associatif */
|
/** retourner true si le schéma est de nature tableau associatif */
|
||||||
function isAssoc(?AssocSchema &$schema=null): bool { return false; }
|
function isAssoc(?AssocSchema &$schema=null): bool { return false; }
|
||||||
/** retourner true si le schéma est de nature liste */
|
/** retourner true si le schéma est de nature liste */
|
||||||
|
@ -1,15 +1,12 @@
|
|||||||
# nulib\schema
|
# nulib\schema
|
||||||
|
|
||||||
* plus de {key} ni {orig} dans messages
|
|
||||||
* les messages standard ne sont utilisés que s'il n'y a pas de message dans
|
|
||||||
l'exception
|
|
||||||
* si instance de UserException, prendre le message "non technique" pour
|
|
||||||
résultat
|
|
||||||
* valeurs composite/computed
|
* valeurs composite/computed
|
||||||
* analyse / vérification de la valeur complète après calcul du résultat, si
|
* analyse / vérification de la valeur complète après calcul du résultat, si
|
||||||
tous les résultats sont bons
|
tous les résultats sont bons
|
||||||
* calcul des valeurs composites/computed par une fonction avant/après l'analyse
|
* calcul des valeurs composites/computed par une fonction avant/après l'analyse
|
||||||
globale si résultat ok
|
globale si résultat ok
|
||||||
|
* fonction getter_func, setter_func, deleter_func pour les propriétés de type
|
||||||
|
computed
|
||||||
* tdate et tdatetime. qu'en est-il des autres classes (delay, etc.)
|
* tdate et tdatetime. qu'en est-il des autres classes (delay, etc.)
|
||||||
* possibilité de spécifier le format de la date à analyser
|
* possibilité de spécifier le format de la date à analyser
|
||||||
* ScalarSchema::from_property()
|
* ScalarSchema::from_property()
|
||||||
|
@ -3,6 +3,7 @@ namespace nur\sery\wip\schema;
|
|||||||
|
|
||||||
use ArrayAccess;
|
use ArrayAccess;
|
||||||
use IteratorAggregate;
|
use IteratorAggregate;
|
||||||
|
use nulib\php\func;
|
||||||
use nur\sery\wip\schema\_assoc\AssocWrapper;
|
use nur\sery\wip\schema\_assoc\AssocWrapper;
|
||||||
use nur\sery\wip\schema\_list\ListWrapper;
|
use nur\sery\wip\schema\_list\ListWrapper;
|
||||||
use nur\sery\wip\schema\_scalar\ScalarWrapper;
|
use nur\sery\wip\schema\_scalar\ScalarWrapper;
|
||||||
@ -67,6 +68,22 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate {
|
|||||||
/** supprimer la valeur */
|
/** supprimer la valeur */
|
||||||
abstract function unset(): self;
|
abstract function unset(): self;
|
||||||
|
|
||||||
|
protected function _format(WrapperContext $context, $format=null): string {
|
||||||
|
$value = $context->input->get($context->valueKey);
|
||||||
|
/** @var func $formatterFunc */
|
||||||
|
$formatterFunc = $context->schema->formatterFunc;
|
||||||
|
if ($formatterFunc !== null) {
|
||||||
|
# la fonction formatter n'a pas forcément accès au format de la définition
|
||||||
|
# le lui fournir ici
|
||||||
|
$format ??= $context->schema->format;
|
||||||
|
return $formatterFunc->invoke([$value, $format, $context, $this]);
|
||||||
|
} else {
|
||||||
|
# on assume que le type a été initialisé avec le format de la définition
|
||||||
|
# le cas échéant
|
||||||
|
return $context->type->format($value, $format);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** formatter la valeur pour affichage */
|
/** formatter la valeur pour affichage */
|
||||||
abstract function format($format=null): string;
|
abstract function format($format=null): string;
|
||||||
|
|
||||||
|
@ -5,21 +5,29 @@ use nur\sery\wip\schema\input\Input;
|
|||||||
use nur\sery\wip\schema\types\IType;
|
use nur\sery\wip\schema\types\IType;
|
||||||
|
|
||||||
class WrapperContext {
|
class WrapperContext {
|
||||||
function __construct(Schema $schema, Wrapper $wrapper, ?Input $input, $valueKey, Result $result) {
|
function __construct(Schema $schema, ?Input $input, $valueKey, ?Result $result, ?array $params) {
|
||||||
|
$this->params = $params;
|
||||||
|
$this->verifix = $params["verifix"] ?? true;
|
||||||
|
$this->throw = $params["throw"] ?? null;
|
||||||
|
|
||||||
$this->schema = $schema;
|
$this->schema = $schema;
|
||||||
$this->wrapper = $wrapper;
|
//$this->wrapper = $wrapper;
|
||||||
if ($input !== null) $this->input = $input;
|
if ($input !== null) $this->input = $input;
|
||||||
$this->valueKey = $valueKey;
|
$this->valueKey = $valueKey;
|
||||||
$this->type = null;
|
$this->type = null;
|
||||||
$this->result = $result;
|
if ($result !== null) $this->result = $result;
|
||||||
$this->origValue = null;
|
$this->origValue = null;
|
||||||
$this->value = null;
|
$this->value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ?array $params;
|
||||||
|
public bool $verifix;
|
||||||
|
public ?bool $throw;
|
||||||
|
|
||||||
/** schéma de la valeur */
|
/** schéma de la valeur */
|
||||||
public Schema $schema;
|
public Schema $schema;
|
||||||
/** instance de Wrapper associé à ce contexte */
|
/** instance de Wrapper associé à ce contexte */
|
||||||
public Wrapper $wrapper;
|
//public Wrapper $wrapper;
|
||||||
/** source et destination de la valeur */
|
/** source et destination de la valeur */
|
||||||
public Input $input;
|
public Input $input;
|
||||||
/** @var string|int|null clé de la valeur dans le tableau destination */
|
/** @var string|int|null clé de la valeur dans le tableau destination */
|
||||||
|
@ -5,41 +5,38 @@ use nulib\ValueException;
|
|||||||
use nur\sery\wip\schema\Result;
|
use nur\sery\wip\schema\Result;
|
||||||
|
|
||||||
class AssocResult extends Result {
|
class AssocResult extends Result {
|
||||||
function __construct(Result $arrayResult, array &$keyResults) {
|
function __construct(AssocWrapperContext $context) {
|
||||||
$this->arrayResult = $arrayResult;
|
$this->context = $context;
|
||||||
$this->keyResults =& $keyResults;
|
|
||||||
$this->result =& $this->arrayResult;
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAssoc(?AssocResult &$result=null): bool { $result = $this; return true;}
|
function isAssoc(?AssocResult &$result=null): bool { $result = $this; return true;}
|
||||||
|
|
||||||
protected Result $arrayResult;
|
protected AssocWrapperContext $context;
|
||||||
|
|
||||||
/** @var Result[] */
|
|
||||||
protected array $keyResults;
|
|
||||||
|
|
||||||
function getKeys(): array {
|
function getKeys(): array {
|
||||||
return array_keys($this->keyResults);
|
return $this->context->keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Result $result;
|
protected Result $result;
|
||||||
|
|
||||||
function select($key): Result {
|
function select($key): AssocResult {
|
||||||
|
$context = $this->context;
|
||||||
if ($key === null) {
|
if ($key === null) {
|
||||||
$this->result =& $this->arrayResult;
|
$this->result = $context->arrayResult;
|
||||||
} elseif (array_key_exists($key, $this->keyResults)) {
|
return $this;
|
||||||
$this->result =& $this->keyResults[$key];
|
|
||||||
} else {
|
|
||||||
throw ValueException::invalid_key($key);
|
|
||||||
}
|
}
|
||||||
|
$wrapper = $context->keyWrappers[$key] ?? null;
|
||||||
|
if ($wrapper === null) throw ValueException::invalid_key($key);
|
||||||
|
$this->result = $wrapper->getResult();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
function reset(): void {
|
function reset(): void {
|
||||||
$this->arrayResult->reset();
|
$context = $this->context;
|
||||||
foreach ($this->keyResults as $result) {
|
$context->arrayResult->reset();
|
||||||
$result->reset();
|
foreach ($context->keyWrappers as $wrapper) {
|
||||||
|
$wrapper->getResult()->reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ namespace nur\sery\wip\schema\_assoc;
|
|||||||
|
|
||||||
use nulib\cl;
|
use nulib\cl;
|
||||||
use nulib\ref\schema\ref_schema;
|
use nulib\ref\schema\ref_schema;
|
||||||
|
use nulib\ValueException;
|
||||||
|
use nur\sery\wip\schema\_scalar\ScalarWrapper;
|
||||||
use nur\sery\wip\schema\Schema;
|
use nur\sery\wip\schema\Schema;
|
||||||
use nur\sery\wip\schema\Wrapper;
|
use nur\sery\wip\schema\Wrapper;
|
||||||
|
|
||||||
@ -50,6 +52,11 @@ class AssocSchema extends Schema {
|
|||||||
self::_ensure_schema_instances($definition);
|
self::_ensure_schema_instances($definition);
|
||||||
}
|
}
|
||||||
$this->definition = $definition;
|
$this->definition = $definition;
|
||||||
|
$keys = [];
|
||||||
|
foreach ($definition["schema"] as $key => $schema) {
|
||||||
|
if (!$schema["computed"]) $keys[] = $key;
|
||||||
|
}
|
||||||
|
$this->keys = $keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAssoc(?AssocSchema &$schema=null): bool {
|
function isAssoc(?AssocSchema &$schema=null): bool {
|
||||||
@ -57,12 +64,28 @@ class AssocSchema extends Schema {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected array $keys;
|
||||||
|
|
||||||
|
function getKeys(): array {
|
||||||
|
return $this->keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSchema($key): Schema {
|
||||||
|
if ($key === null) return $this;
|
||||||
|
$schema = $this->definition["schema"][$key] ?? null;
|
||||||
|
if ($schema === null) throw ValueException::invalid_key($key);
|
||||||
|
return $schema;
|
||||||
|
}
|
||||||
|
|
||||||
protected function newWrapper(): AssocWrapper {
|
protected function newWrapper(): AssocWrapper {
|
||||||
return new AssocWrapper($this);
|
return new AssocWrapper($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getWrapper(&$array=null, $arrayKey=null, ?Wrapper &$wrapper=null): AssocWrapper {
|
function getWrapper(&$array=null, $arrayKey=null, ?Wrapper &$wrapper=null): AssocWrapper {
|
||||||
|
# si pas de valeur ni de wrapper, pas de vérification et donc pas d'exception
|
||||||
|
# cf le code similaire dans ScalarWrapper::__construct()
|
||||||
|
$verifix = $array !== null || $wrapper !== null;
|
||||||
if (!($wrapper instanceof AssocWrapper)) $wrapper = $this->newWrapper();
|
if (!($wrapper instanceof AssocWrapper)) $wrapper = $this->newWrapper();
|
||||||
return $wrapper->reset($array, $arrayKey);
|
return $wrapper->reset($array, $arrayKey, $verifix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,59 +3,43 @@ namespace nur\sery\wip\schema\_assoc;
|
|||||||
|
|
||||||
use nulib\ValueException;
|
use nulib\ValueException;
|
||||||
use nur\sery\wip\schema\_scalar\ScalarResult;
|
use nur\sery\wip\schema\_scalar\ScalarResult;
|
||||||
use nur\sery\wip\schema\_scalar\ScalarWrapper;
|
|
||||||
use nur\sery\wip\schema\input\Input;
|
use nur\sery\wip\schema\input\Input;
|
||||||
use nur\sery\wip\schema\Result;
|
|
||||||
use nur\sery\wip\schema\types\IType;
|
use nur\sery\wip\schema\types\IType;
|
||||||
use nur\sery\wip\schema\Wrapper;
|
use nur\sery\wip\schema\Wrapper;
|
||||||
|
use nur\sery\wip\schema\WrapperContext;
|
||||||
|
|
||||||
class AssocWrapper extends Wrapper {
|
class AssocWrapper extends Wrapper {
|
||||||
function __construct(AssocSchema $schema, &$array=null, $arrayKey=null, ?array $params=null) {
|
function __construct(AssocSchema $schema, &$array=null, $arrayKey=null, ?array $params=null) {
|
||||||
$verifix = $params["verifix"] ?? true;
|
$definitionSchema = $schema->getDefinition()["schema"];
|
||||||
$throw = $params["throw"] ?? null;
|
$keys = $schema->getKeys();
|
||||||
|
$keyTypes = [];
|
||||||
|
$keyWrappers = [];
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
$keyTypes[$key] = null;
|
||||||
|
$keyWrappers[$key] = $schema->getSchema($key)->getWrapper();
|
||||||
|
}
|
||||||
|
$this->context = $context = new AssocWrapperContext($schema, null, null, null, $params);
|
||||||
|
$context->arrayResult = new ScalarResult();
|
||||||
|
$context->keys = $keys;
|
||||||
|
$context->keyTypes = $keyTypes;
|
||||||
|
$context->keyWrappers = $keyWrappers;
|
||||||
|
$context->result = new AssocResult($context);
|
||||||
|
|
||||||
|
$throw = $context->throw;
|
||||||
if ($array !== null && $throw === null) {
|
if ($array !== null && $throw === null) {
|
||||||
# Si $value est null, ne pas lancer d'exception, parce qu'on considère que
|
# Si $value est null, ne pas lancer d'exception, parce qu'on considère que
|
||||||
# c'est une initialisation sans conséquences
|
# c'est une initialisation sans conséquences
|
||||||
$throw = true;
|
$throw = true;
|
||||||
}
|
}
|
||||||
$this->schema = $schema;
|
$context->throw = $throw ?? false;
|
||||||
$this->verifix = $verifix;
|
|
||||||
$this->throw = $throw ?? false;
|
|
||||||
$this->result = new AssocResult();
|
|
||||||
$this->reset($array, $arrayKey);
|
$this->reset($array, $arrayKey);
|
||||||
$this->throw = $throw ?? true;
|
$context->throw = $throw ?? true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAssoc(?AssocWrapper &$wrapper=null): bool { $wrapper = $this; return true; }
|
function isAssoc(?AssocWrapper &$wrapper=null): bool { $wrapper = $this; return true; }
|
||||||
|
|
||||||
protected bool $verifix;
|
/** @var AssocWrapperContext */
|
||||||
|
protected WrapperContext $context;
|
||||||
protected bool $throw;
|
|
||||||
|
|
||||||
/** schéma de ce tableau */
|
|
||||||
protected AssocSchema $schema;
|
|
||||||
|
|
||||||
/** source et destination de la valeur */
|
|
||||||
protected Input $input;
|
|
||||||
|
|
||||||
/** @var string|int|null clé du tableau dans le tableau destination */
|
|
||||||
protected $arrayKey;
|
|
||||||
|
|
||||||
protected IType $arrayType;
|
|
||||||
|
|
||||||
protected ScalarResult $arrayResult;
|
|
||||||
|
|
||||||
/** @var IType[] */
|
|
||||||
protected array $keyTypes;
|
|
||||||
|
|
||||||
/** @var Result[] */
|
|
||||||
protected array $keyResults;
|
|
||||||
|
|
||||||
protected AssocResult $result;
|
|
||||||
|
|
||||||
protected ?array $keys;
|
|
||||||
|
|
||||||
protected ?array $wrappers;
|
|
||||||
|
|
||||||
protected function newInput(&$value): Input {
|
protected function newInput(&$value): Input {
|
||||||
return new Input($value);
|
return new Input($value);
|
||||||
@ -64,73 +48,84 @@ class AssocWrapper extends Wrapper {
|
|||||||
function reset(&$array, $arrayKey=null, ?bool $verifix=null): Wrapper {
|
function reset(&$array, $arrayKey=null, ?bool $verifix=null): Wrapper {
|
||||||
if ($array instanceof Input) $input = $array;
|
if ($array instanceof Input) $input = $array;
|
||||||
else $input = $this->newInput($array);
|
else $input = $this->newInput($array);
|
||||||
$this->input = $input;
|
$context = $this->context;
|
||||||
$this->arrayKey = $arrayKey;
|
$context->input = $input;
|
||||||
|
$context->valueKey = $arrayKey;
|
||||||
$this->analyze();
|
$this->analyze();
|
||||||
if ($verifix ?? $this->verifix) $this->verifix();
|
if ($verifix ?? $context->verifix) $this->verifix();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getKeys(): array {
|
function getKeys(): array {
|
||||||
return $this->keys;
|
return $this->context->keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
function select($key=null): ScalarWrapper {
|
/** @param string|int|null $key */
|
||||||
$wrapper = $this->wrappers[$key] ?? null;
|
function select($key=null): Wrapper {
|
||||||
if ($key !== null) return $wrapper;
|
if ($key === null) return $this;
|
||||||
throw ValueException::invalid_key($key);
|
$wrapper = $this->context->keyWrappers[$key] ?? null;
|
||||||
|
if ($wrapper === null) throw ValueException::invalid_key($key);
|
||||||
|
return $wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param Result[] $results */
|
protected function analyze(): int {
|
||||||
function verifix(?bool $throw=null, ?array &$results=null): bool {
|
return 0; #XXX
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function verifix(?bool $throw=null): bool {
|
||||||
|
return false; #XXX
|
||||||
|
}
|
||||||
|
|
||||||
function getResult(): AssocResult {
|
function getResult(): AssocResult {
|
||||||
return $this->result;
|
/** @var AssocResult $result */
|
||||||
|
$result = $this->context->result;
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPresent(): bool {
|
function isPresent(): bool {
|
||||||
return $this->result->present;
|
return $this->context->result->present;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getType(): IType {
|
function getType(): IType {
|
||||||
return $this->arrayType;
|
return $this->context->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAvailable(): bool {
|
function isAvailable(): bool {
|
||||||
return $this->result->available;
|
return $this->context->result->available;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValid(): bool {
|
function isValid(): bool {
|
||||||
return $this->result->valid;
|
return $this->context->result->valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNormalized(): bool {
|
function isNormalized(): bool {
|
||||||
return $this->result->normalized;
|
return $this->context->result->normalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get($default=null) {
|
function get($default=null) {
|
||||||
if ($this->result->available) return $this->input->get($this->arrayKey);
|
$context = $this->context;
|
||||||
|
if ($context->result->available) return $context->input->get($context->valueKey);
|
||||||
else return $default;
|
else return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
function set($value, ?bool $verifix=null): AssocWrapper {
|
function set($value, ?bool $verifix=null): AssocWrapper {
|
||||||
$this->input->set($value, $this->arrayKey);
|
$context = $this->context;
|
||||||
|
$context->input->set($value, $context->valueKey);
|
||||||
$this->analyze();
|
$this->analyze();
|
||||||
if ($verifix ?? $this->verifix) $this->verifix();
|
if ($verifix ?? $context->verifix) $this->verifix();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
function unset(?bool $verifix=null): AssocWrapper {
|
function unset(?bool $verifix=null): AssocWrapper {
|
||||||
$this->input->unset($this->arrayKey);
|
$context = $this->context;
|
||||||
|
$context->input->unset($context->valueKey);
|
||||||
$this->analyze();
|
$this->analyze();
|
||||||
if ($verifix ?? $this->verifix) $this->verifix();
|
if ($verifix ?? $context->verifix) $this->verifix();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
function format($format = null): string {
|
function format($format = null): string {
|
||||||
// TODO: Implement format() method.
|
return $this->_format($this->context, $format);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensureKeys(): bool {
|
function ensureKeys(): bool {
|
||||||
|
21
src/schema/_assoc/AssocWrapperContext.php
Normal file
21
src/schema/_assoc/AssocWrapperContext.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
namespace nur\sery\wip\schema\_assoc;
|
||||||
|
|
||||||
|
use nur\sery\wip\schema\_scalar\ScalarResult;
|
||||||
|
use nur\sery\wip\schema\types\IType;
|
||||||
|
use nur\sery\wip\schema\Wrapper;
|
||||||
|
use nur\sery\wip\schema\WrapperContext;
|
||||||
|
|
||||||
|
class AssocWrapperContext extends WrapperContext {
|
||||||
|
/** résultat de l'analyse du tableau */
|
||||||
|
public ScalarResult $arrayResult;
|
||||||
|
|
||||||
|
/** liste des clés valides */
|
||||||
|
public array $keys;
|
||||||
|
|
||||||
|
/** @var ?IType[] */
|
||||||
|
public array $keyTypes;
|
||||||
|
|
||||||
|
/** @var Wrapper[] */
|
||||||
|
public array $keyWrappers;
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
namespace nur\sery\wip\schema\_list;
|
namespace nur\sery\wip\schema\_list;
|
||||||
|
|
||||||
use nulib\ref\schema\ref_schema;
|
use nulib\ref\schema\ref_schema;
|
||||||
|
use nulib\ValueException;
|
||||||
use nur\sery\wip\schema\Schema;
|
use nur\sery\wip\schema\Schema;
|
||||||
use nur\sery\wip\schema\Wrapper;
|
use nur\sery\wip\schema\Wrapper;
|
||||||
|
|
||||||
@ -55,12 +56,26 @@ class ListSchema extends Schema {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const KEYS = [null];
|
||||||
|
|
||||||
|
function getKeys(): array {
|
||||||
|
return self::KEYS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSchema($key): Schema {
|
||||||
|
if ($key !== null) throw ValueException::invalid_key($key);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected function newWrapper(): ListWrapper {
|
protected function newWrapper(): ListWrapper {
|
||||||
return new ListWrapper($this);
|
return new ListWrapper($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getWrapper(&$value=null, $valueKey=null, ?Wrapper &$wrapper=null): ListWrapper {
|
function getWrapper(&$value=null, $valueKey=null, ?Wrapper &$wrapper=null): ListWrapper {
|
||||||
|
# si pas de valeur ni de wrapper, pas de vérification et donc pas d'exception
|
||||||
|
# cf le code similaire dans ScalarWrapper::__construct()
|
||||||
|
$verifix = $value !== null || $wrapper !== null;
|
||||||
if (!($wrapper instanceof ListWrapper)) $wrapper = $this->newWrapper();
|
if (!($wrapper instanceof ListWrapper)) $wrapper = $this->newWrapper();
|
||||||
return $wrapper->reset($value, $valueKey);
|
return $wrapper->reset($value, $valueKey, $verifix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ class ScalarResult extends Result {
|
|||||||
function isScalar(?ScalarResult &$result=null): bool { $result = $this; return true; }
|
function isScalar(?ScalarResult &$result=null): bool { $result = $this; return true; }
|
||||||
|
|
||||||
function getKeys(): array {
|
function getKeys(): array {
|
||||||
return [null];
|
return ScalarSchema::KEYS;
|
||||||
}
|
}
|
||||||
|
|
||||||
function select($key): Result {
|
function select($key): Result {
|
||||||
@ -23,8 +23,7 @@ class ScalarResult extends Result {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var array */
|
protected array $result;
|
||||||
protected $result;
|
|
||||||
|
|
||||||
function reset(): void {
|
function reset(): void {
|
||||||
$this->result = array_merge(
|
$this->result = array_merge(
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
namespace nur\sery\wip\schema\_scalar;
|
namespace nur\sery\wip\schema\_scalar;
|
||||||
|
|
||||||
use nulib\ref\schema\ref_schema;
|
use nulib\ref\schema\ref_schema;
|
||||||
|
use nulib\ValueException;
|
||||||
use nur\sery\wip\schema\Schema;
|
use nur\sery\wip\schema\Schema;
|
||||||
use nur\sery\wip\schema\types\IType;
|
use nur\sery\wip\schema\types\IType;
|
||||||
use nur\sery\wip\schema\Wrapper;
|
use nur\sery\wip\schema\Wrapper;
|
||||||
@ -66,6 +67,17 @@ class ScalarSchema extends Schema {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const KEYS = [null];
|
||||||
|
|
||||||
|
function getKeys(): array {
|
||||||
|
return self::KEYS;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSchema($key): Schema {
|
||||||
|
if ($key !== null) throw ValueException::invalid_key($key);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected function newWrapper(): ScalarWrapper {
|
protected function newWrapper(): ScalarWrapper {
|
||||||
return new ScalarWrapper($this);
|
return new ScalarWrapper($this);
|
||||||
}
|
}
|
||||||
|
@ -12,28 +12,23 @@ use nur\sery\wip\schema\Wrapper;
|
|||||||
|
|
||||||
class ScalarWrapper extends Wrapper {
|
class ScalarWrapper extends Wrapper {
|
||||||
function __construct(ScalarSchema $schema, &$value=null, $valueKey=null, ?array $params=null) {
|
function __construct(ScalarSchema $schema, &$value=null, $valueKey=null, ?array $params=null) {
|
||||||
$verifix = $params["verifix"] ?? true;
|
$this->context = $context = new WrapperContext($schema, null, null, new ScalarResult(), $params);
|
||||||
$throw = $params["throw"] ?? null;
|
|
||||||
|
$throw = $context->throw;
|
||||||
if ($value !== null && $throw === null) {
|
if ($value !== null && $throw === null) {
|
||||||
# Si $value est null, ne pas lancer d'exception, parce qu'on considère que
|
# Si $value est null, ne pas lancer d'exception, parce qu'on considère que
|
||||||
# c'est une initialisation sans conséquences
|
# c'est une initialisation sans conséquences
|
||||||
$throw = true;
|
$throw = true;
|
||||||
}
|
}
|
||||||
$this->context = new WrapperContext($schema, $this, null, null, new ScalarResult());
|
$context->throw = $throw ?? false;
|
||||||
$this->verifix = $verifix;
|
|
||||||
$this->throw = $throw ?? false;
|
|
||||||
$this->reset($value, $valueKey);
|
$this->reset($value, $valueKey);
|
||||||
$this->throw = $throw ?? true;
|
$context->throw = $throw ?? true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isScalar(?ScalarWrapper &$wrapper=null): bool { $wrapper = $this; return true; }
|
function isScalar(?ScalarWrapper &$wrapper=null): bool { $wrapper = $this; return true; }
|
||||||
|
|
||||||
protected WrapperContext $context;
|
protected WrapperContext $context;
|
||||||
|
|
||||||
protected bool $verifix;
|
|
||||||
|
|
||||||
protected bool $throw;
|
|
||||||
|
|
||||||
protected function newInput(&$value): Input {
|
protected function newInput(&$value): Input {
|
||||||
return new Input($value);
|
return new Input($value);
|
||||||
}
|
}
|
||||||
@ -41,16 +36,17 @@ class ScalarWrapper extends Wrapper {
|
|||||||
function reset(&$value, $valueKey=null, ?bool $verifix=null): Wrapper {
|
function reset(&$value, $valueKey=null, ?bool $verifix=null): Wrapper {
|
||||||
if ($value instanceof Input) $input = $value;
|
if ($value instanceof Input) $input = $value;
|
||||||
else $input = $this->newInput($value);
|
else $input = $this->newInput($value);
|
||||||
$this->context->input = $input;
|
$context = $this->context;
|
||||||
$this->context->valueKey = $valueKey;
|
$context->input = $input;
|
||||||
$this->context->type = null;
|
$context->valueKey = $valueKey;
|
||||||
|
$context->type = null;
|
||||||
$this->analyze();
|
$this->analyze();
|
||||||
if ($verifix ?? $this->verifix) $this->verifix();
|
if ($verifix ?? $context->verifix) $this->verifix();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getKeys(): array {
|
function getKeys(): array {
|
||||||
return [null];
|
return ScalarSchema::KEYS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param string|int|null $key */
|
/** @param string|int|null $key */
|
||||||
@ -164,7 +160,7 @@ class ScalarWrapper extends Wrapper {
|
|||||||
|
|
||||||
/** @var func $analyzerFunc */
|
/** @var func $analyzerFunc */
|
||||||
$analyzerFunc = $schema->analyzerFunc;
|
$analyzerFunc = $schema->analyzerFunc;
|
||||||
if ($analyzerFunc !== null) $what = $analyzerFunc->invoke([$context]);
|
if ($analyzerFunc !== null) $what = $analyzerFunc->invoke([$context, $this]);
|
||||||
else $what = $this->analyze0();
|
else $what = $this->analyze0();
|
||||||
if ($what !== ref_analyze::STRING) return $what;
|
if ($what !== ref_analyze::STRING) return $what;
|
||||||
|
|
||||||
@ -172,7 +168,7 @@ class ScalarWrapper extends Wrapper {
|
|||||||
try {
|
try {
|
||||||
/** @var func $extractorFunc */
|
/** @var func $extractorFunc */
|
||||||
$extractorFunc = $schema->extractorFunc;
|
$extractorFunc = $schema->extractorFunc;
|
||||||
if ($extractorFunc !== null) $extracted = $extractorFunc->invoke([$value, $context]);
|
if ($extractorFunc !== null) $extracted = $extractorFunc->invoke([$value, $context, $this]);
|
||||||
else $extracted = $context->type->extract($value);
|
else $extracted = $context->type->extract($value);
|
||||||
$context->value = $extracted;
|
$context->value = $extracted;
|
||||||
} catch (ValueException $e) {
|
} catch (ValueException $e) {
|
||||||
@ -183,7 +179,7 @@ class ScalarWrapper extends Wrapper {
|
|||||||
try {
|
try {
|
||||||
/** @var func $parserFunc */
|
/** @var func $parserFunc */
|
||||||
$parserFunc = $schema->parserFunc;
|
$parserFunc = $schema->parserFunc;
|
||||||
if ($parserFunc !== null) $parsed = $parserFunc->invoke([$extracted, $context]);
|
if ($parserFunc !== null) $parsed = $parserFunc->invoke([$extracted, $context, $this]);
|
||||||
else $parsed = $context->type->parse($extracted);
|
else $parsed = $context->type->parse($extracted);
|
||||||
$context->value = $parsed;
|
$context->value = $parsed;
|
||||||
} catch (ValueException $e) {
|
} catch (ValueException $e) {
|
||||||
@ -209,7 +205,6 @@ class ScalarWrapper extends Wrapper {
|
|||||||
/** @var ScalarResult $result */
|
/** @var ScalarResult $result */
|
||||||
$result = $context->result;
|
$result = $context->result;
|
||||||
|
|
||||||
|
|
||||||
$verifix = false;
|
$verifix = false;
|
||||||
$modified = false;
|
$modified = false;
|
||||||
if ($result->resultAvailable) {
|
if ($result->resultAvailable) {
|
||||||
@ -238,16 +233,15 @@ class ScalarWrapper extends Wrapper {
|
|||||||
/** @var func $normalizerFunc */
|
/** @var func $normalizerFunc */
|
||||||
$normalizerFunc = $schema->normalizerFunc;
|
$normalizerFunc = $schema->normalizerFunc;
|
||||||
if ($normalizerFunc !== null) {
|
if ($normalizerFunc !== null) {
|
||||||
$context = new WrapperContext($schema, $this, $input, $valueKey, $result);
|
|
||||||
$orig = $value;
|
$orig = $value;
|
||||||
$value = $normalizerFunc->invoke([$orig, $context]);
|
$value = $normalizerFunc->invoke([$orig, $context, $this]);
|
||||||
$modified = $value !== $orig;
|
$modified = $value !== $orig;
|
||||||
} else {
|
} else {
|
||||||
$modified = $this->type->verifix($value, $result, $schema);
|
$modified = $context->type->verifix($value, $result, $schema);
|
||||||
}
|
}
|
||||||
if ($result->valid) $input->set($value, $valueKey);
|
if ($result->valid) $input->set($value, $valueKey);
|
||||||
}
|
}
|
||||||
if (!$result->valid) $result->throw($throw ?? $this->throw);
|
if (!$result->valid) $result->throw($throw ?? $context->throw);
|
||||||
return $modified;
|
return $modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,7 +281,7 @@ class ScalarWrapper extends Wrapper {
|
|||||||
$context = $this->context;
|
$context = $this->context;
|
||||||
$context->input->set($value, $context->valueKey);
|
$context->input->set($value, $context->valueKey);
|
||||||
$this->analyze();
|
$this->analyze();
|
||||||
if ($verifix ?? $this->verifix) $this->verifix();
|
if ($verifix ?? $context->verifix) $this->verifix();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,24 +289,11 @@ class ScalarWrapper extends Wrapper {
|
|||||||
$context = $this->context;
|
$context = $this->context;
|
||||||
$context->input->unset($context->valueKey);
|
$context->input->unset($context->valueKey);
|
||||||
$this->analyze();
|
$this->analyze();
|
||||||
if ($verifix ?? $this->verifix) $this->verifix();
|
if ($verifix ?? $context->verifix) $this->verifix();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
function format($format=null): string {
|
function format($format=null): string {
|
||||||
$context = $this->context;
|
return $this->_format($this->context, $format);
|
||||||
$value = $context->input->get($context->valueKey);
|
|
||||||
/** @var func $formatterFunc */
|
|
||||||
$formatterFunc = $context->schema->formatterFunc;
|
|
||||||
if ($formatterFunc !== null) {
|
|
||||||
# la fonction formatter n'a pas forcément accès au format de la définition
|
|
||||||
# le lui fournir ici
|
|
||||||
$format ??= $context->schema->format;
|
|
||||||
return $formatterFunc->invoke([$value, $format]);
|
|
||||||
} else {
|
|
||||||
# on assume que le type a été initialisé avec le format de la définition
|
|
||||||
# le cas échéant
|
|
||||||
return $this->type->format($value, $format);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,6 +90,9 @@ class AssocSchemaTest extends TestCase {
|
|||||||
"name" => "c", "pkey" => "c", "header" => "c",
|
"name" => "c", "pkey" => "c", "header" => "c",
|
||||||
],
|
],
|
||||||
]), $schema->getDefinition());
|
]), $schema->getDefinition());
|
||||||
yaml::dump($schema->getDefinition());
|
//yaml::dump($schema->getDefinition());
|
||||||
|
|
||||||
|
$wrapper = $schema->getWrapper();
|
||||||
|
$wrapper->getKeys();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user