renommer Value en Wrapper

This commit is contained in:
Jephté Clain 2025-03-06 09:05:10 +04:00
parent 74487a0ab9
commit f8eec57055
21 changed files with 275 additions and 341 deletions

View File

@ -5,24 +5,26 @@ use nur\sery\wip\schema\input\Input;
use nur\sery\wip\schema\types\IType;
class AnalyzerContext {
function __construct(Schema $schema, Input $input, $destKey, Result $result) {
function __construct(Schema $schema, Wrapper $wrapper, Input $input, $destKey, Result $result) {
$this->schema = $schema;
$this->wrapper = $wrapper;
$this->input = $input;
$this->destKey = $destKey;
$this->result = $result;
$this->type = null;
$this->orig = null;
$this->value = null;
$this->dest = null;
$this->destKey = $destKey;
}
public Schema $schema;
public Wrapper $wrapper;
public Input $input;
/** @var int|string|null */
public $destKey;
public Result $result;
public ?IType $type;
/** @var mixed */
public $orig;
/** @var mixed */
public $value;
public $dest;
/** @var int|string|null */
public $destKey;
}

View File

@ -38,17 +38,17 @@ abstract class Schema implements ArrayAccess {
}
/**
* Créer une nouvelle instance de {@link Value} qui référence la
* Créer une nouvelle instance de {@link Wrapper} qui référence la
* variable $dest (si $destKey===null) ou $dest[$destKey] si $destKey n'est
* pas null
*/
static function nv(?Value &$destv=null, &$dest=null, $destKey=null, &$schema=null, $definition=null): Value {
static function nw(&$dest=null, $destKey=null, &$schema=null, $definition=null, ?Wrapper &$destw=null): Wrapper {
if ($definition === null) {
# bien que techniquement, $definition peut être null (il s'agit alors du
# schéma d'un scalaire quelconque), on ne l'autorise pas ici
throw SchemaException::invalid_schema("definition is required");
}
return self::ns($schema, $definition)->newValue($destv, $dest, $destKey);
return self::ns($schema, $definition)->newWrapper($dest, $destKey, $destw);
}
/**
@ -70,7 +70,7 @@ abstract class Schema implements ArrayAccess {
/** retourner true si le schéma est de nature scalaire */
function isScalar(?ScalarSchema &$scalar=null): bool { return false; }
abstract function newValue(?Value &$destv=null, &$dest=null, $destKey=null): Value;
abstract function newWrapper(&$dest=null, $destKey=null, ?Wrapper &$destw=null): Wrapper;
#############################################################################
# key & properties

View File

@ -1,7 +1,5 @@
# nulib\schema
* implémenter support `analyzer_func`, `extractor_func`, `parser_func`,
`normalizer_func`, `formatter_func`
* dans AssocSchema, support `[key_prefix]` qui permet de spécifier un préfixe
commun aux champs dans le tableau destination, e.g
~~~php

View File

@ -3,15 +3,15 @@ namespace nur\sery\wip\schema;
use ArrayAccess;
use IteratorAggregate;
use nur\sery\wip\schema\_assoc\AssocValue;
use nur\sery\wip\schema\_list\ListValue;
use nur\sery\wip\schema\_scalar\ScalarValue;
use nur\sery\wip\schema\_assoc\AssocWrapper;
use nur\sery\wip\schema\_list\ListWrapper;
use nur\sery\wip\schema\_scalar\ScalarWrapper;
use nur\sery\wip\schema\types\IType;
abstract class Value implements ArrayAccess, IteratorAggregate {
function isAssoc(?AssocValue &$assoc=null): bool { return false; }
function isList(?ListValue &$list=null): bool { return false; }
function isScalar(?ScalarValue &$scalar=null): bool { return false; }
abstract class Wrapper implements ArrayAccess, IteratorAggregate {
function isAssoc(?AssocWrapper &$assoc=null): bool { return false; }
function isList(?ListWrapper &$list=null): bool { return false; }
function isScalar(?ScalarWrapper &$scalar=null): bool { return false; }
/** spécifier la valeur destination gérée par cet objet */
abstract function reset(&$dest, $destKey=null, ?bool $verifix=null): self;
@ -23,11 +23,11 @@ abstract class Value implements ArrayAccess, IteratorAggregate {
abstract function getKeys(): array;
/** obtenir un objet pour gérer la valeur spécifiée */
abstract function getValue($key=null): Value;
abstract function getWrapper($key=null): Wrapper;
function getIterator() {
foreach ($this->getKeys() as $key) {
yield $key => $this->getValue($key);
yield $key => $this->getWrapper($key);
}
}
@ -72,14 +72,14 @@ abstract class Value implements ArrayAccess, IteratorAggregate {
}
function offsetGet($offset) {
return $this->getValue($offset);
return $this->getWrapper($offset);
}
function offsetSet($offset, $value): void {
$this->getValue($offset)->set($value);
$this->getWrapper($offset)->set($value);
}
function offsetUnset($offset): void {
$this->getValue($offset)->unset();
$this->getWrapper($offset)->unset();
}
}

View File

@ -4,7 +4,7 @@ namespace nur\sery\wip\schema\_assoc;
use nulib\cl;
use nulib\ref\schema\ref_schema;
use nur\sery\wip\schema\Schema;
use nur\sery\wip\schema\Value;
use nur\sery\wip\schema\Wrapper;
/**
* Class AssocSchema
@ -48,8 +48,8 @@ class AssocSchema extends Schema {
return true;
}
function newValue(?Value &$destv=null, &$dest=null, $destKey=null): AssocValue {
if ($destv instanceof AssocValue) return $destv->reset($dest, $destKey);
else return ($destv = new AssocValue($this, $dest, $destKey));
function newWrapper(&$dest=null, $destKey=null, ?Wrapper &$destw=null): AssocWrapper {
if ($destw instanceof AssocWrapper) return $destw->reset($dest, $destKey);
else return ($destw = new AssocWrapper($this, $dest, $destKey));
}
}

View File

@ -2,10 +2,10 @@
namespace nur\sery\wip\schema\_assoc;
use nur\sery\wip\schema\Result;
use nur\sery\wip\schema\Value;
use nur\sery\wip\schema\Wrapper;
abstract/*XXX*/ class AssocValue extends Value {
function isAssoc(?AssocValue &$assoc=null): bool { $assoc = $this; return true; }
abstract/*XXX*/ class AssocWrapper extends Wrapper {
function isAssoc(?AssocWrapper &$assoc=null): bool { $assoc = $this; return true; }
function ensureKeys(): bool {
}

View File

@ -3,7 +3,7 @@ namespace nur\sery\wip\schema\_list;
use nulib\ref\schema\ref_schema;
use nur\sery\wip\schema\Schema;
use nur\sery\wip\schema\Value;
use nur\sery\wip\schema\Wrapper;
class ListSchema extends Schema {
/** @var array meta-schema d'un schéma de nature liste */
@ -46,8 +46,8 @@ class ListSchema extends Schema {
return true;
}
function newValue(?Value &$destv=null, &$dest=null, $destKey=null): ListValue {
if ($destv instanceof ListValue) return $destv->reset($dest, $destKey);
else return ($destv = new ListValue($this, $dest, $destKey));
function newWrapper(&$dest=null, $destKey=null, ?Wrapper &$destw=null): ListWrapper {
if ($destw instanceof ListWrapper) return $destw->reset($dest, $destKey);
else return ($destw = new ListWrapper($this, $dest, $destKey));
}
}

View File

@ -2,10 +2,10 @@
namespace nur\sery\wip\schema\_list;
use nur\sery\wip\schema\Result;
use nur\sery\wip\schema\Value;
use nur\sery\wip\schema\Wrapper;
abstract/*XXX*/ class ListValue extends Value {
function isList(?ListValue &$list=null): bool { $list = $this; return true; }
abstract/*XXX*/ class ListWrapper extends Wrapper {
function isList(?ListWrapper &$list=null): bool { $list = $this; return true; }
function ensureKeys(): bool {
}

View File

@ -12,7 +12,7 @@ use nur\sery\wip\schema\types\tcallable;
use nur\sery\wip\schema\types\tcontent;
use nur\sery\wip\schema\types\tpkey;
use nur\sery\wip\schema\types\trawstring;
use nur\sery\wip\schema\Value;
use nur\sery\wip\schema\Wrapper;
/**
* Class ScalarSchema
@ -178,9 +178,9 @@ class ScalarSchema extends Schema {
return true;
}
function newValue(?Value &$destv=null, &$dest=null, $destKey=null): ScalarValue {
if ($destv instanceof ScalarValue) return $destv->reset($dest, $destKey);
else return ($destv = new ScalarValue($this, $dest, $destKey));
function newWrapper(&$dest=null, $destKey=null, ?Wrapper &$destw=null): ScalarWrapper {
if ($destw instanceof ScalarWrapper) return $destw->reset($dest, $destKey);
else return ($destw = new ScalarWrapper($this, $dest, $destKey));
}
#############################################################################

View File

@ -8,54 +8,54 @@ use nur\sery\wip\schema\AnalyzerContext;
use nur\sery\wip\schema\input\Input;
use nur\sery\wip\schema\types;
use nur\sery\wip\schema\types\IType;
use nur\sery\wip\schema\Value;
use nur\sery\wip\schema\Wrapper;
class ScalarValue extends Value {
function __construct(ScalarSchema $schema, &$dest=null, $destKey=null, bool $defaultVerifix=true, ?bool $defaultThrow=null) {
class ScalarWrapper extends Wrapper {
function __construct(ScalarSchema $schema, &$dest=null, $destKey=null, ?array $params=null) {
$defaultVerifix = $params["verifix"] ?? true;
$defaultThrow = $params["throw"] ?? null;
if ($dest !== null && $defaultThrow === null) {
# Si $dest est null, ne pas lancer d'exception, parce qu'on considère que
# c'est une initialisation sans conséquences
$defaultThrow = true;
}
$this->schema = $schema;
$this->defaultVerifix = $defaultVerifix;
$this->defaultThrow = $defaultThrow ?? false;
$this->verifix = $defaultVerifix;
$this->throw = $defaultThrow ?? false;
$this->result = new ScalarResult();
$this->reset($dest, $destKey);
$this->defaultThrow = $defaultThrow ?? true;
$this->throw = $defaultThrow ?? true;
}
function isScalar(?ScalarValue &$scalar=null): bool { $scalar = $this; return true; }
function isScalar(?ScalarWrapper &$scalar=null): bool { $scalar = $this; return true; }
/** @var ScalarSchema schéma de cette valeur */
protected $schema;
/** schéma de cette valeur */
protected ScalarSchema $schema;
/** @var Input source et destination de la valeur */
protected $input;
/** source et destination de la valeur */
protected Input $input;
/** @var string|int|null clé de la valeur dans le tableau destination */
protected $destKey;
/** @var bool */
protected $defaultVerifix;
protected bool $verifix;
/** @var bool */
protected $defaultThrow;
protected bool $throw;
/** @var IType type de la valeur après analyse */
protected $type;
/** type de la valeur après analyse */
protected ?IType $type;
/** @var ?ScalarResult résultat de l'analyse de la valeur */
protected $result;
/** résultat de l'analyse de la valeur */
protected ScalarResult $result;
function reset(&$dest, $destKey=null, ?bool $verifix=null): Value {
function reset(&$dest, $destKey=null, ?bool $verifix=null): Wrapper {
if ($dest instanceof Input) $input = $dest;
else $input = new Input($dest);
$this->input = $input;
$this->destKey = $destKey;
$this->type = null;
$this->analyzeExtractParse();
if ($verifix ?? $this->defaultVerifix) $this->verifix();
if ($verifix ?? $this->verifix) $this->verifix();
return $this;
}
@ -63,7 +63,7 @@ class ScalarValue extends Value {
return [null];
}
function getValue($key=null): ScalarValue {
function getWrapper($key=null): ScalarWrapper {
if ($key === null) return $this;
throw ValueException::invalid_key($key);
}
@ -124,7 +124,7 @@ class ScalarValue extends Value {
if (!$type->isAvailable($input, $destKey)) return $result->setUnavailable($schema);
$value = $input->get($destKey);
$context->orig = $context->value = $value;
$context->orig = $context->dest = $value;
if ($type->isNull($value)) {
return $result->setNull($schema);
} elseif (is_string($value)) {
@ -138,12 +138,12 @@ class ScalarValue extends Value {
}
function _extract(AnalyzerContext $context): bool {
return $context->type->extract($context->value);
return $context->type->extract($context->dest);
}
function _parse(AnalyzerContext $context): bool {
try {
$context->value = $context->type->parse($context->value);
$context->dest = $context->type->parse($context->dest);
return true;
} catch (ValueException $e) {
return false;
@ -156,7 +156,7 @@ class ScalarValue extends Value {
$destKey = $this->destKey;
$result = $this->result;
$result->reset();
$context = new AnalyzerContext($schema, $input, $destKey, $result);
$context = new AnalyzerContext($schema, $this, $input, $destKey, $result);
/** @var func $analyzerFunc */
$analyzerFunc = $schema->analyzerFunc;
@ -169,17 +169,17 @@ class ScalarValue extends Value {
if ($extractorFunc !== null) $valid = $extractorFunc->invoke([$context]);
else $valid = $this->_extract($context);
if (!$valid) return $result->setInvalid($context->orig, $schema);
if ($context->type->isNull($context->value)) return $result->setNull($schema);
$extracted = $context->value;
if ($context->type->isNull($context->dest)) return $result->setNull($schema);
$extracted = $context->dest;
/** @var func $parserFunc */
$parserFunc = $schema->parserFunc;
if ($parserFunc !== null) $valid = $parserFunc->invoke([$context]);
else $valid = $this->_parse($context);
if ($valid) {
$normalized = $context->value === $context->orig;
$normalized = $context->dest === $context->orig;
if ($normalized) {
$input->set($context->value, $destKey);
$input->set($context->dest, $destKey);
return $result->setNormalized();
} else {
$input->set($extracted, $destKey);
@ -190,10 +190,17 @@ class ScalarValue extends Value {
}
function verifix(?bool $throw=null): bool {
if ($throw === null) $throw = $this->defaultThrow;
$schema = $this->schema;
$input = $this->input;
$destKey = $this->destKey;
$result = $this->result;
$context = new AnalyzerContext($schema, $this, $input, $destKey, $result);
/** @var func $normalizerFunc */
$normalizerFunc = $schema->normalizerFunc;
if ($normalizerFunc !== null) return $normalizerFunc->invoke([$context]);
if ($throw === null) $throw = $this->throw;
$verifix = false;
$result =& $this->result;
$modified = false;
if ($result->resultAvailable) {
if ($result->null) {
@ -245,19 +252,17 @@ class ScalarValue extends Value {
else return $default;
}
function set($value, ?bool $verifix=null): ScalarValue {
function set($value, ?bool $verifix=null): ScalarWrapper {
$this->input->set($value, $this->destKey);
$this->analyzeExtractParse();
if ($verifix === null) $verifix = $this->defaultVerifix;
if ($verifix) $this->verifix();
if ($verifix ?? $this->verifix) $this->verifix();
return $this;
}
function unset(?bool $verifix=null): ScalarValue {
function unset(?bool $verifix=null): ScalarWrapper {
$this->input->unset($this->destKey);
$this->analyzeExtractParse();
if ($verifix === null) $verifix = $this->defaultVerifix;
if ($verifix) $this->verifix();
if ($verifix ?? $this->verifix) $this->verifix();
return $this;
}

View File

@ -2,6 +2,7 @@
namespace nur\sery\wip\schema\types;
use nulib\cl;
use nulib\ValueException;
use nur\sery\wip\schema\_scalar\ScalarResult;
use nur\sery\wip\schema\_scalar\ScalarSchema;
use nur\sery\wip\schema\Result;
@ -46,14 +47,20 @@ class tarray extends _tstring {
if (is_array($value)) {
$result->setNormalized();
return false;
} elseif (is_string($value)) {
try {
$value = $this->parse($value);
$result->setValid();
return true;
} catch (ValueException $e) {
}
} elseif (is_scalar($value)) {
$value = cl::with($value);
$result->setValid();
return true;
} else {
$result->setInvalid($value, $schema);
return false;
}
$result->setInvalid($value, $schema);
return false;
}
function format($value, $format=null): string {

View File

@ -79,14 +79,20 @@ class tbool extends _tformatable {
if (is_bool($value)) {
$result->setNormalized();
return false;
} elseif (is_string($value)) {
try {
$value = $this->parse($value);
$result->setValid();
return true;
} catch (ValueException $e) {
}
} elseif (is_scalar($value)) {
$value = boolval($value);
$result->setValid();
return true;
} else {
$result->setInvalid($value, $schema);
return false;
}
$result->setInvalid($value, $schema);
return false;
}
const OUINON_FORMAT = ["Oui", "Non", false];

View File

@ -43,10 +43,16 @@ class tcallable extends _tsimple {
$value = func::with($value);
$result->setNormalized();
return true;
} else {
$result->setInvalid($value, $schema);
return false;
} elseif (is_string($value)) {
try {
$value = $this->parse($value);
$result->setValid();
return true;
} catch (ValueException $e) {
}
}
$result->setInvalid($value, $schema);
return false;
}
function format($value, $format=null): string {

View File

@ -40,13 +40,19 @@ class tfloat extends _tformatable {
if (is_float($value)) {
$result->setNormalized();
return false;
} elseif (is_string($value)) {
try {
$value = $this->parse($value);
$result->setValid();
return true;
} catch (ValueException $e) {
}
} elseif (is_scalar($value)) {
$value = floatval($value);
$result->setValid();
return true;
} else {
$result->setInvalid($value, $schema);
return false;
}
$result->setInvalid($value, $schema);
return false;
}
}

View File

@ -42,13 +42,19 @@ class tint extends _tformatable {
if (is_int($value)) {
$result->setNormalized();
return false;
} elseif (is_string($value)) {
try {
$value = $this->parse($value);
$result->setValid();
return true;
} catch (ValueException $e) {
}
} elseif (is_scalar($value)) {
$value = intval($value);
$result->setValid();
return true;
} else {
$result->setInvalid($value, $schema);
return false;
}
$result->setInvalid($value, $schema);
return false;
}
}

View File

@ -1,12 +1,13 @@
<?php
namespace nur\sery\wip\schema\_scalar;
use Exception;
use nulib\tests\TestCase;
use nulib\ValueException;
use nur\sery\wip\schema\input\Input;
class ScalarValueTest extends TestCase {
function checkValue(ScalarValue $value, $dest, bool $present, bool $available, bool $valid, bool $normalized): void {
function checkValue(ScalarWrapper $value, $dest, bool $present, bool $available, bool $valid, bool $normalized): void {
self::assertSame($dest, $value->get(), "value");
self::assertSame($present, $value->isPresent(), "present");
self::assertSame($available, $value->isAvailable(), "available");
@ -14,278 +15,175 @@ class ScalarValueTest extends TestCase {
self::assertSame($normalized, $value->isNormalized(), "normalized");
}
function checkVerifix(ScalarSchema $schema, $src, bool $verifix, $dest, bool $present, bool $available, bool $valid, bool $normalized, ?array $inputParams=null): void {
$value = $schema->newWrapper();
if ($inputParams !== null) $input = new Input($src, $inputParams);
else $input = $src;
$value->reset($input, null, $verifix);
$this->checkValue($value, $dest, $present, $available, $valid, $normalized);
}
function checkException(ScalarSchema $schema, $src, bool $verifix, string $exceptionClass, ?array $inputParams=null) {
$value = $schema->newWrapper();
if ($inputParams !== null) $src = new Input($src, $inputParams);
self::assertException($exceptionClass, function() use ($value, &$src, $verifix) {
$value->reset($src, null, $verifix);
});
}
function testRawstring() {
$schema = new ScalarSchema("rawstring");
$value = $schema->newValue();
$string = null; $value->reset($string, null, false);
$this->checkValue($value, null, true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = null; $value->reset($string);
});
$this->checkVerifix($schema, null, false, null, true, true, false, false);
$this->checkException($schema, null, true, ValueException::class);
$string = ""; $value->reset($string, null, false);
$this->checkValue($value, "", true, true, true, true);
$string = ""; $value->reset($string);
$this->checkValue($value, "", true, true, true, true);
$this->checkVerifix($schema, "", false, "", true, true, true, true);
$this->checkVerifix($schema, "", true, "", true, true, true, true);
$string = " "; $value->reset($string, null, false);
$this->checkValue($value, " ", true, true, true, true);
$string = " "; $value->reset($string);
$this->checkValue($value, " ", true, true, true, true);
$this->checkVerifix($schema, " ", false, " ", true, true, true, true);
$this->checkVerifix($schema, " ", true, " ", true, true, true, true);
$string = "text"; $value->reset($string, null, false);
$this->checkValue($value, "text", true, true, true, true);
$string = "text"; $value->reset($string);
$this->checkValue($value, "text", true, true, true, true);
$this->checkVerifix($schema, "text", false, "text", true, true, true, true);
$this->checkVerifix($schema, "text", true, "text", true, true, true, true);
$string = " text "; $value->reset($string, null, false);
$this->checkValue($value, " text ", true, true, true, true);
$string = " text "; $value->reset($string);
$this->checkValue($value, " text ", true, true, true, true);
$this->checkVerifix($schema, " text ", false, " text ", true, true, true, true);
$this->checkVerifix($schema, " text ", true, " text ", true, true, true, true);
$string = false; $value->reset($string, null, false);
$this->checkValue($value, null, true, false, true, true);
$string = false; $value->reset($string);
$this->checkValue($value, null, true, false, true, true);
$this->checkVerifix($schema, false, false, null, true, false, true, true);
$this->checkVerifix($schema, false, true, null, true, false, true, true);
$string = true; $value->reset($string, null, false);
$this->checkValue($value, true, true, true, true, false);
$string = true; $value->reset($string);
$this->checkValue($value, "1", true, true, true, false);
$this->checkVerifix($schema, true, false, true, true, true, true, false);
$this->checkVerifix($schema, true, true, "1", true, true, true, false);
$string = 42; $value->reset($string, null, false);
$this->checkValue($value, 42, true, true, true, false);
$string = 42; $value->reset($string);
$this->checkValue($value, "42", true, true, true, false);
$this->checkVerifix($schema, 42, false, 42, true, true, true, false);
$this->checkVerifix($schema, 42, true, "42", true, true, true, false);
$string = []; $value->reset($string, null, false);
$this->checkValue($value, [], true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = null;
$value->reset($string);
});
$this->checkVerifix($schema, [], false, [], true, true, false, false);
$this->checkException($schema, [], true, ValueException::class);
## Tester nullable
$schema = new ScalarSchema("?rawstring");
$value = $schema->newValue();
$string = null; $value->reset($string, null, false);
$this->checkValue($value, null, true, true, true, true);
$string = null; $value->reset($string, null);
$this->checkValue($value, null, true, true, true, true);
$this->checkVerifix($schema, null, false, null, true, true, true, true);
$this->checkVerifix($schema, null, true, null, true, true, true, true);
## Tester required
$schema = new ScalarSchema(["rawstring", "required" => true]);
$value = $schema->newValue();
$string = false; $value->reset($string, null, false);
$this->checkValue($value, null, true, false, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = false; $value->reset($string);
});
$this->checkVerifix($schema, false, false, null, true, false, false, false);
$this->checkException($schema, false, true, ValueException::class);
## Tester allow_empty === false
$inputParams = ["allow_empty" => false];
$schema = new ScalarSchema("rawstring");
$value = $schema->newValue();
$string = null; $input = new Input($string, ["allow_empty" => false]);
$value->reset($input, null, false);
$this->checkValue($value, null, true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = null; $input = new Input($string, ["allow_empty" => false]);
$value->reset($input);
});
$this->checkVerifix($schema, null, false, null, true, true, false, false, $inputParams);
$this->checkException($schema, null, true, ValueException::class, $inputParams);
$string = ""; $input = new Input($string, ["allow_empty" => false]);
$value->reset($input, null, false);
$this->checkValue($value, null, true, false, true, true);
$string = ""; $input = new Input($string, ["allow_empty" => false]);
$value->reset($input);
$this->checkValue($value, null, true, false, true, true);
$this->checkVerifix($schema, "", false, null, true, false, true, true, $inputParams);
$this->checkVerifix($schema, "", true, null, true, false, true, true, $inputParams);
$schema = new ScalarSchema("?rawstring");
$value = $schema->newValue();
$string = null; $input = new Input($string, ["allow_empty" => false]);
$value->reset($input, null, false);
$this->checkValue($value, null, true, true, true, true);
$string = null; $input = new Input($string, ["allow_empty" => false]);
$value->reset($input);
$this->checkValue($value, null, true, true, true, true);
$this->checkVerifix($schema, null, false, null, true, true, true, true, $inputParams);
$this->checkVerifix($schema, null, true, null, true, true, true, true, $inputParams);
$string = ""; $input = new Input($string, ["allow_empty" => false]);
$value->reset($input, null, false);
$this->checkValue($value, null, true, false, true, true);
$string = ""; $input = new Input($string, ["allow_empty" => false]);
$value->reset($input);
$this->checkValue($value, null, true, false, true, true);
$this->checkVerifix($schema, "", false, null, true, false, true, true, $inputParams);
$this->checkVerifix($schema, "", true, null, true, false, true, true, $inputParams);
}
function testString() {
$schema = new ScalarSchema("string");
$value = $schema->newValue();
$string = null; $value->reset($string, null, false);
$this->checkValue($value, null, true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = null;
$value->reset($string);
});
$this->checkVerifix($schema, null, false, null, true, true, false, false);
$this->checkException($schema, null, true, ValueException::class);
$string = ""; $value->reset($string, null, false);
$this->checkValue($value, "", true, true, true, true);
$string = ""; $value->reset($string);
$this->checkValue($value, "", true, true, true, true);
$this->checkVerifix($schema, "", false, "", true, true, true, true);
$this->checkVerifix($schema, "", true, "", true, true, true, true);
$string = " "; $value->reset($string, null, false);
$this->checkValue($value, "", true, true, true, false);
$string = " "; $value->reset($string);
$this->checkValue($value, "", true, true, true, true);
$this->checkVerifix($schema, " ", false, "", true, true, true, false);
$this->checkVerifix($schema, " ", true, "", true, true, true, true);
$string = "text"; $value->reset($string, null, false);
$this->checkValue($value, "text", true, true, true, true);
$string = "text"; $value->reset($string);
$this->checkValue($value, "text", true, true, true, true);
$this->checkVerifix($schema, "text", false, "text", true, true, true, true);
$this->checkVerifix($schema, "text", true, "text", true, true, true, true);
$string = " text "; $value->reset($string, null, false);
$this->checkValue($value, "text", true, true, true, false);
$string = " text "; $value->reset($string);
$this->checkValue($value, "text", true, true, true, true);
$this->checkVerifix($schema, " text ", false, "text", true, true, true, false);
$this->checkVerifix($schema, " text ", true, "text", true, true, true, true);
$string = false; $value->reset($string, null, false);
$this->checkValue($value, null, true, false, true, true);
$string = false; $value->reset($string);
$this->checkValue($value, null, true, false, true, true);
$this->checkVerifix($schema, false, false, null, true, false, true, true);
$this->checkVerifix($schema, false, true, null, true, false, true, true);
$string = true; $value->reset($string, null, false);
$this->checkValue($value, true, true, true, true, false);
$string = true; $value->reset($string);
$this->checkValue($value, "1", true, true, true, false);
$this->checkVerifix($schema, true, false, true, true, true, true, false);
$this->checkVerifix($schema, true, true, "1", true, true, true, false);
$string = 42; $value->reset($string, null, false);
$this->checkValue($value, 42, true, true, true, false);
$string = 42; $value->reset($string);
$this->checkValue($value, "42", true, true, true, false);
$this->checkVerifix($schema, 42, false, 42, true, true, true, false);
$this->checkVerifix($schema, 42, true, "42", true, true, true, false);
$string = []; $value->reset($string, null, false);
$this->checkValue($value, [], true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = null;
$value->reset($string);
});
$this->checkVerifix($schema, [], false, [], true, true, false, false);
$this->checkException($schema, [], true, ValueException::class);
## Tester nullable
$schema = new ScalarSchema("?string");
$value = $schema->newValue();
$string = null; $value->reset($string, null, false);
$this->checkValue($value, null, true, true, true, true);
$string = null; $value->reset($string, null);
$this->checkValue($value, null, true, true, true, true);
$this->checkVerifix($schema, null, false, null, true, true, true, true);
$this->checkVerifix($schema, null, true, null, true, true, true, true);
## Tester required
$schema = new ScalarSchema(["string", "required" => true]);
$value = $schema->newValue();
$string = false; $value->reset($string, null, false);
$this->checkValue($value, null, true, false, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = false; $value->reset($string);
});
$this->checkVerifix($schema, false, false, null, true, false, false, false);
$this->checkException($schema, false, true, ValueException::class);
}
function testInt() {
$schema = new ScalarSchema("int");
$value = $schema->newValue();
$int = null; $value->reset($int, null, false);
$this->checkValue($value, null, true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$int = null;
$value->reset($int);
});
$this->checkVerifix($schema, null, false, null, true, true, false, false);
$this->checkException($schema, null, true, ValueException::class);
$int = 42; $value->reset($int, null, false);
$this->checkValue($value, 42, true, true, true, true);
$int = 42; $value->reset($int);
$this->checkValue($value, 42, true, true, true, true);
$this->checkVerifix($schema, 42, false, 42, true, true, true, true);
$this->checkVerifix($schema, 42, true, 42, true, true, true, true);
$int = "42"; $value->reset($int, null, false);
$this->checkValue($value, "42", true, true, true, false);
$int = "42"; $value->reset($int);
$this->checkValue($value, 42, true, true, true, false);
$this->checkVerifix($schema, "42", false, "42", true, true, true, false);
$this->checkVerifix($schema, "42", true, 42, true, true, true, false);
$int = "42.5"; $value->reset($int, null, false);
$this->checkValue($value, "42.5", true, true, true, false);
$int = "42.5"; $value->reset($int);
$this->checkValue($value, 42, true, true, true, false);
$this->checkVerifix($schema, "42.5", false, "42.5", true, true, true, false);
$this->checkVerifix($schema, "42.5", true, 42, true, true, true, false);
$int = "42,5"; $value->reset($int, null, false);
$this->checkValue($value, "42,5", true, true, true, false);
$int = "42,5"; $value->reset($int);
$this->checkValue($value, 42, true, true, true, false);
$this->checkVerifix($schema, "42,5", false, "42,5", true, true, true, false);
$this->checkVerifix($schema, "42,5", true, 42, true, true, true, false);
$int = " 42 "; $value->reset($int, null, false);
$this->checkValue($value, "42", true, true, true, false);
$int = " 42 "; $value->reset($int);
$this->checkValue($value, 42, true, true, true, false);
$this->checkVerifix($schema, " 42 ", false, "42", true, true, true, false);
$this->checkVerifix($schema, " 42 ", true, 42, true, true, true, false);
$int = ""; $value->reset($int, null, false);
$this->checkValue($value, "", true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$int = "";
$value->reset($int);
});
$this->checkVerifix($schema, "", false, "", true, true, false, false);
$this->checkException($schema, "", true, ValueException::class);
$int = " "; $value->reset($int, null, false);
$this->checkValue($value, " ", true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$int = " ";
$value->reset($int);
});
$this->checkVerifix($schema, " ", false, " ", true, true, false, false);
$this->checkException($schema, " ", true, ValueException::class);
$int = "text"; $value->reset($int, null, false);
$this->checkValue($value, "text", true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$int = "text";
$value->reset($int);
});
$this->checkVerifix($schema, "text", false, "text", true, true, false, false);
$this->checkException($schema, "text", true, ValueException::class);
$int = false; $value->reset($int, null, false);
$this->checkValue($value, null, true, false, true, true);
$int = false; $value->reset($int);
$this->checkValue($value, null, true, false, true, true);
$this->checkVerifix($schema, false, false, null, true, false, true, true);
$this->checkVerifix($schema, false, true, null, true, false, true, true);
$int = true; $value->reset($int, null, false);
$this->checkValue($value, true, true, true, true, false);
$int = true; $value->reset($int);
$this->checkValue($value, 1, true, true, true, false);
$this->checkVerifix($schema, true, false, true, true, true, true, false);
$this->checkVerifix($schema, true, true, 1, true, true, true, false);
$int = []; $value->reset($int, null, false);
$this->checkValue($value, [], true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = null;
$value->reset($string);
});
$this->checkVerifix($schema, [], false, [], true, true, false, false);
$this->checkException($schema, [], true, ValueException::class);
## Tester nullable
$schema = new ScalarSchema("?int");
$value = $schema->newValue();
$int = null; $value->reset($int, null, false);
$this->checkValue($value, null, true, true, true, true);
$int = null; $value->reset($int, null);
$this->checkValue($value, null, true, true, true, true);
$this->checkVerifix($schema, null, false, null, true, true, true, true);
$this->checkVerifix($schema, null, true, null, true, true, true, true);
## Tester required
$schema = new ScalarSchema(["int", "required" => true]);
$value = $schema->newValue();
$int = false; $value->reset($int, null, false);
$this->checkValue($value, null, true, false, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = false; $value->reset($string);
});
$this->checkVerifix($schema, false, false, null, true, false, false, false);
$this->checkException($schema, false, true, ValueException::class);
}
}

View File

@ -3,7 +3,7 @@ namespace nur\sery\wip\schema\types;
use Exception;
use nulib\tests\TestCase;
use nur\sery\wip\schema\_scalar\ScalarValue;
use nur\sery\wip\schema\_scalar\ScalarWrapper;
use nur\sery\wip\schema\Schema;
class boolTest extends TestCase {
@ -58,8 +58,8 @@ class boolTest extends TestCase {
}
function testBool() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "bool");
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, "bool", $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -74,8 +74,8 @@ class boolTest extends TestCase {
}
function testNbool() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "?bool");
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, "?bool", $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);

View File

@ -3,7 +3,7 @@ namespace nur\sery\wip\schema\types;
use Exception;
use nulib\tests\TestCase;
use nur\sery\wip\schema\_scalar\ScalarValue;
use nur\sery\wip\schema\_scalar\ScalarWrapper;
use nur\sery\wip\schema\Schema;
class floatTest extends TestCase {
@ -32,8 +32,8 @@ class floatTest extends TestCase {
}
function testFloat() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "float");
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, "float", $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -52,11 +52,11 @@ class floatTest extends TestCase {
}
function testRequiredFloat() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, [
"float", null,
"required" => true,
]);
], $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -74,8 +74,8 @@ class floatTest extends TestCase {
}
function testNfloat() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "?float");
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, "?float", $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -105,11 +105,11 @@ class floatTest extends TestCase {
}
function testRequiredNfloat() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, [
"?float", null,
"required" => true,
]);
], $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);

View File

@ -3,7 +3,7 @@ namespace nur\sery\wip\schema\types;
use Exception;
use nulib\tests\TestCase;
use nur\sery\wip\schema\_scalar\ScalarValue;
use nur\sery\wip\schema\_scalar\ScalarWrapper;
use nur\sery\wip\schema\Schema;
class intTest extends TestCase {
@ -32,8 +32,8 @@ class intTest extends TestCase {
}
function testInt() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "int");
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, "int", $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -52,11 +52,11 @@ class intTest extends TestCase {
}
function testRequiredInt() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, [
"int", null,
"required" => true,
]);
], $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -74,8 +74,8 @@ class intTest extends TestCase {
}
function testNint() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "?int");
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, "?int", $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -105,11 +105,11 @@ class intTest extends TestCase {
}
function testRequiredNint() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, [
"?int", null,
"required" => true,
]);
], $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);

View File

@ -3,7 +3,7 @@ namespace nur\sery\wip\schema\types;
use Exception;
use nulib\tests\TestCase;
use nur\sery\wip\schema\_scalar\ScalarValue;
use nur\sery\wip\schema\_scalar\ScalarWrapper;
use nur\sery\wip\schema\Schema;
class strTest extends TestCase {
@ -13,8 +13,8 @@ class strTest extends TestCase {
self::assertSame("", $dest);
$destv->set(" ");
self::assertSame(" ", $destv->get());
self::assertSame(" ", $dest);
self::assertSame("", $destv->get());
self::assertSame("", $dest);
$destv->set("a");
self::assertSame("a", $destv->get());
@ -24,7 +24,7 @@ class strTest extends TestCase {
self::assertSame("12", $destv->get());
$destv->set(" 12 ");
self::assertSame(" 12 ", $destv->get());
self::assertSame("12", $destv->get());
$destv->set(12);
self::assertSame("12", $destv->get());
@ -40,8 +40,8 @@ class strTest extends TestCase {
}
function testStr() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "string");
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, "string", $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -58,11 +58,11 @@ class strTest extends TestCase {
}
function testRequiredStr() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, [
"string", null,
"required" => true,
]);
], $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -78,8 +78,8 @@ class strTest extends TestCase {
}
function testNstr() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "?string");
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, "?string", $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
@ -99,11 +99,11 @@ class strTest extends TestCase {
}
function testRequiredNstr() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
/** @var ScalarWrapper $destv */
Schema::nw($dest, null, $schema, [
"?string", null,
"required" => true,
]);
], $destv);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);

View File

@ -2,7 +2,7 @@
namespace nur\sery\wip\schema\types;
use nulib\tests\TestCase;
use nur\sery\wip\schema\_scalar\ScalarValue;
use nur\sery\wip\schema\_scalar\ScalarWrapper;
use nur\sery\wip\schema\Schema;
class unionTest extends TestCase {
@ -10,8 +10,8 @@ class unionTest extends TestCase {
## l'ordre des types doit être respecté
# string puis int
/** @var ScalarValue $siv */
Schema::nv($siv, $si, null, $sis, "string|int");
/** @var ScalarWrapper $siv */
Schema::nw($si, null, $sis, "string|int", $siv);
$siv->set("12");
self::assertSame("12", $si);
@ -19,7 +19,7 @@ class unionTest extends TestCase {
self::assertSame(12, $si);
# int puis string
Schema::nv($isv, $is, null, $iss, "int|string");
Schema::nw($is, null, $iss, "int|string", $isv);
$isv->set("12");
self::assertSame("12", $is);