From 3028fe12f7dcf3a0b8130a94f08d5e8f87fa5f4a Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Mon, 27 Nov 2023 22:39:35 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- TODO.md | 1 - src/schema/{values => }/Result.php | 2 +- src/schema/Schema.php | 24 ++-- src/schema/Value.php | 27 +++++ src/schema/input/Input.php | 24 ++-- src/schema/{ => schemas}/AssocSchema.php | 15 ++- src/schema/{ => schemas}/ListSchema.php | 15 ++- src/schema/{ => schemas}/ScalarSchema.php | 13 ++- src/schema/types.php | 37 ++++++ src/schema/types/IType.php | 2 +- src/schema/types/Registry.php | 26 +++++ src/schema/types/tarray.php | 2 +- src/schema/types/tbool.php | 2 +- src/schema/types/tcallable.php | 2 +- src/schema/types/tcontent.php | 2 +- src/schema/types/tfloat.php | 5 + src/schema/types/tint.php | 5 + src/schema/types/tkey.php | 2 +- src/schema/types/tpkey.php | 2 +- src/schema/types/tstring.php | 2 +- src/schema/values/AssocValue.php | 7 +- src/schema/values/IValue.php | 29 ----- src/schema/values/ListValue.php | 7 +- src/schema/values/ScalarValue.php | 69 +++++++----- src/schema/values/TValue.php | 17 --- tests/schema/schemaTest.php | 106 ++++++++++++++++++ .../schema/{ => schemas}/ScalarSchemaTest.php | 6 +- 27 files changed, 330 insertions(+), 121 deletions(-) rename src/schema/{values => }/Result.php (96%) create mode 100644 src/schema/Value.php rename src/schema/{ => schemas}/AssocSchema.php (71%) rename src/schema/{ => schemas}/ListSchema.php (74%) rename src/schema/{ => schemas}/ScalarSchema.php (95%) create mode 100644 src/schema/types.php create mode 100644 src/schema/types/Registry.php create mode 100644 src/schema/types/tfloat.php create mode 100644 src/schema/types/tint.php delete mode 100644 src/schema/values/IValue.php delete mode 100644 src/schema/values/TValue.php create mode 100644 tests/schema/schemaTest.php rename tests/schema/{ => schemas}/ScalarSchemaTest.php (94%) diff --git a/TODO.md b/TODO.md index 1f00665..c3db9ef 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,6 @@ ## schema -* [ ] properties dans ScalarSchema, avec aliases pour notamment "nature" => "" * [ ] remplacer IValue et TValue par une classe abstraite Value -*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary \ No newline at end of file diff --git a/src/schema/values/Result.php b/src/schema/Result.php similarity index 96% rename from src/schema/values/Result.php rename to src/schema/Result.php index 87cafe6..3d299a8 100644 --- a/src/schema/values/Result.php +++ b/src/schema/Result.php @@ -1,5 +1,5 @@ value =& $value; + function __construct(&$dest=null) { + $this->dest =& $dest; } - protected $value; + protected $dest; + /** tester si la valeur existe */ function exists($key=null): bool { if ($key === null) return true; - return $this->value !== null && array_key_exists($key, $this->value); + return $this->dest !== null && array_key_exists($key, $this->dest); } function get($key=null) { - if ($key === null) return $this->value; - elseif ($this->value === null) return null; - elseif (!array_key_exists($key, $this->value)) return null; - else return $this->value[$key]; + if ($key === null) return $this->dest; + elseif ($this->dest === null) return null; + elseif (!array_key_exists($key, $this->dest)) return null; + else return $this->dest[$key]; } function set($value, $key=null): void { - if ($key === null) $this->value = $value; - else $this->value[$key] = $value; + if ($key === null) $this->dest = $value; + else $this->dest[$key] = $value; } } diff --git a/src/schema/AssocSchema.php b/src/schema/schemas/AssocSchema.php similarity index 71% rename from src/schema/AssocSchema.php rename to src/schema/schemas/AssocSchema.php index f34afd5..f35eca6 100644 --- a/src/schema/AssocSchema.php +++ b/src/schema/schemas/AssocSchema.php @@ -1,7 +1,12 @@ reset($input, $key); + } } diff --git a/src/schema/ListSchema.php b/src/schema/schemas/ListSchema.php similarity index 74% rename from src/schema/ListSchema.php rename to src/schema/schemas/ListSchema.php index f4a6bf7..04c4472 100644 --- a/src/schema/ListSchema.php +++ b/src/schema/schemas/ListSchema.php @@ -1,5 +1,10 @@ reset($input, $key); + } } diff --git a/src/schema/ScalarSchema.php b/src/schema/schemas/ScalarSchema.php similarity index 95% rename from src/schema/ScalarSchema.php rename to src/schema/schemas/ScalarSchema.php index 868ea69..1902201 100644 --- a/src/schema/ScalarSchema.php +++ b/src/schema/schemas/ScalarSchema.php @@ -1,17 +1,19 @@ reset($dest, $key); } ############################################################################# diff --git a/src/schema/types.php b/src/schema/types.php new file mode 100644 index 0000000..a89d615 --- /dev/null +++ b/src/schema/types.php @@ -0,0 +1,37 @@ +get($name); + } + + static function string(): tstring { return self::get("string"); } + static function bool(): tbool { return self::get("bool"); } + static function int(): tint { return self::get("int"); } + static function float(): tfloat { return self::get("float"); } + static function array(): tarray { return self::get("array"); } + static function callable(): tcallable { return self::get("callable"); } +} diff --git a/src/schema/types/IType.php b/src/schema/types/IType.php index 6820ee3..1d544a2 100644 --- a/src/schema/types/IType.php +++ b/src/schema/types/IType.php @@ -1,7 +1,7 @@ tstring::class, + "bool" => tbool::class, "boolean" => tbool::class, + "int" => tint::class, "integer" => tint::class, + "float" => tfloat::class, "flt" => tfloat::class, + "double" => tfloat::class, "dbl" => tfloat::class, + "array" => tarray::class, + "callable" => tcallable::class, + ]; + + function __construct() { + $this->types = []; + } + + /** @var IType[] */ + protected $types; + + function get(string $name): IType { + #XXX les instancier + return $this->types[$name]; + } +} diff --git a/src/schema/types/tarray.php b/src/schema/types/tarray.php index 1112609..b6f1c10 100644 --- a/src/schema/types/tarray.php +++ b/src/schema/types/tarray.php @@ -3,7 +3,7 @@ namespace nur\sery\schema\types; use nulib\cl; -class tarray { +class tarray implements IType { static function ensure_array(&$array): void { if (!is_array($array)) $array = cl::with($array); } diff --git a/src/schema/types/tbool.php b/src/schema/types/tbool.php index 84e0313..83cb0ba 100644 --- a/src/schema/types/tbool.php +++ b/src/schema/types/tbool.php @@ -1,7 +1,7 @@ schema = $schema; + $this->reset($dest, $key, $verifix); + } + + function isScalar(?ScalarValue &$scalar=null): bool { $scalar = $this; return true; } /** @var ScalarSchema schéma de cette valeur */ protected $schema; + /** @var Input source et destination de la valeur */ + protected $input; + + /** @var string|int clé de la valeur dans le tableau destination */ + protected $key; + /** @var ?Result résultat de l'analyse de la valeur */ protected $result; - function __construct(&$value, ScalarSchema $schema, $key=null, bool $verifix=true) { - $this->schema = $schema; - $this->reset($value, $key, $verifix); - #XXX résoudre les type dans reset()? + 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; } - function isScalar(?ScalarValue &$scalar=null): bool { $scalar = $this; return true; } - function isList(?ListValue &$list=null): bool { return false; } - function isAssoc(?AssocValue &$assoc=null): bool { return false; } + function exists(): bool { + return $this->input->exists($this->key); + } function get($default=null) { $key = $this->key; - if ($key === null) return $this->value; - elseif (array_key_exists($key, $this->value)) return $this->value[$key]; + $input = $this->input; + if ($input->exists($key)) return $input->get($key); else return $default; } function set($value): self { - $key = $this->key; - if ($key === null) $this->value = $value; - else $this->value[$key] = $value; + $this->input->set($value, $this->key); return $this; } /** @var IType */ protected $type; - function type(): IType { + function getType(): IType { if ($this->type === null) $this->type = $this->schema->getType($this->key); return $this->type; } - function exists(): bool { - return $this->type()->exists($this->value, $this->key); + function valid(): bool { + } + + function normalized(): bool { } /** @@ -55,25 +74,25 @@ class ScalarValue implements IValue { * si la valeur était déjà normalisée, retourner false. */ function verifix(bool $throw=true, ?Result &$result=null): bool { - $type = $this->type(); + $type = $this->getType(); $key = $this->key; - if ($key === null) $modified = $type->verifix($this->value, $throw, $result); - else $modified = $type->verifix($this->value[$key], $throw, $result); + if ($key === null) $modified = $type->verifix($this->input, $throw, $result); + else $modified = $type->verifix($this->input[$key], $throw, $result); $this->result = $result; return $modified; } function parse($value, bool $throw=true, ?Result &$result=null) { - $this->type()->verifix($value, $throw, $result); + $this->getType()->verifix($value, $throw, $result); $this->set($value); $this->result = $result; return $value; } function format(?string $format=null): string { - $type = $this->type(); + $type = $this->getType(); $key = $this->key; - if ($key === null) return $type->format($this->value, $format); - else return $type->format($this->value[$key], $format); + if ($key === null) return $type->format($this->input, $format); + else return $type->format($this->input[$key], $format); } } diff --git a/src/schema/values/TValue.php b/src/schema/values/TValue.php deleted file mode 100644 index 75d6953..0000000 --- a/src/schema/values/TValue.php +++ /dev/null @@ -1,17 +0,0 @@ -value =& $value; - $this->key = $key; - $this->result = null; - if ($verifix) $this->verifix(); - } -} diff --git a/tests/schema/schemaTest.php b/tests/schema/schemaTest.php new file mode 100644 index 0000000..6e83c85 --- /dev/null +++ b/tests/schema/schemaTest.php @@ -0,0 +1,106 @@ +nv($intv, $int); + $f = function($value) use($intv) { + return function() use($intv, $value) { + $intv->set($value); + }; + }; + + self::assertException(Exception::class, $f(null)); + self::assertException(Exception::class, $f("")); + self::assertException(Exception::class, $f(" ")); + + $intv->set(12); + self::assertSame(12, $intv->get()); + self::assertSame(12, $int); + self::assertSame("12", $intv->format()); + self::assertSame("0012", $intv->format("%4u")); + + $intv->set("12"); + self::assertSame(12, $intv->get()); + + $intv->set(" 12 "); + self::assertSame(12, $intv->get()); + + self::assertException(Exception::class, $f(true)); + self::assertException(Exception::class, $f(false)); + self::assertException(Exception::class, $f("a")); + self::assertException(Exception::class, $f([])); + self::assertException(Exception::class, $f(["a"])); + } + + function testNint() { + Schema::ns($schema, ["?int"]); + /** @var ScalarValue $intv */ + $schema->nv($intv, $int); + $f = function($value) use($intv) { + return function() use($intv, $value) { + $intv->set($value); + }; + }; + + $intv->set(null); + self::assertNull($intv->get()); + self::assertNull($int); + self::assertSame("", $intv->format()); + + $intv->set(""); + self::assertNull($intv->get()); + self::assertNull($int); + self::assertSame("", $intv->format()); + + $intv->set(" "); + self::assertNull($intv->get()); + self::assertNull($int); + self::assertSame("", $intv->format()); + + $intv->set(12); + self::assertSame(12, $intv->get()); + self::assertSame(12, $int); + self::assertSame("12", $intv->format()); + self::assertSame("0012", $intv->format("%4u")); + + $intv->set("12"); + self::assertSame(12, $int); + + $intv->set(" 12 "); + self::assertSame(12, $int); + + self::assertException(Exception::class, $f(true)); + self::assertException(Exception::class, $f(false)); + self::assertException(Exception::class, $f("a")); + self::assertException(Exception::class, $f([])); + self::assertException(Exception::class, $f(["a"])); + } + + function testUnionTypes() { + # l'ordre des types est respecté + Schema::ns($schema, ["string|int"]); + /** @var ScalarValue $siv */ + $schema->nv($siv, $si); + + $siv->set("12"); + self::assertSame("12", $si); + $siv->set(12); + self::assertSame(12, $si); + + Schema::ns($schema, ["int|string"]); + /** @var ScalarValue $siv */ + $schema->nv($siv, $si); + + $siv->set("12"); + self::assertSame(12, $si); + $siv->set(12); + self::assertSame(12, $si); + } +} diff --git a/tests/schema/ScalarSchemaTest.php b/tests/schema/schemas/ScalarSchemaTest.php similarity index 94% rename from tests/schema/ScalarSchemaTest.php rename to tests/schema/schemas/ScalarSchemaTest.php index f8e15e3..fcce800 100644 --- a/tests/schema/ScalarSchemaTest.php +++ b/tests/schema/schemas/ScalarSchemaTest.php @@ -1,7 +1,9 @@ null, "" => ["scalar"], "name" => null, - "key" => null, + "pkey" => null, "header" => null, "composite" => null, ];