diff --git a/doc/schema.md b/doc/schema.md index b4f2c3d..05f8560 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -169,7 +169,7 @@ const VALUE_SCHEMA = [ "" => "nature du schéma: scalar", "name" => "identifiant de la valeur", # spécifique à VALUE_SCHEMA - "key" => "chemin de clé de la valeur dans le tableau associatif", + "pkey" => "chemin de clé de la valeur dans le tableau associatif", "header" => "nom de l'en-tête s'il faut présenter cette donnée dans un tableau", "composite" => "ce champ fait-il partie d'une valeur composite?", ]; @@ -186,7 +186,7 @@ const VALUE_SCHEMA = [ "desc" => "description de la valeur", "name" => "identifiant de la valeur", # spécifique à VALUE_SCHEMA - "key" => "chemin de clé de la valeur dans le tableau associatif", + "pkey" => "chemin de clé de la valeur dans le tableau associatif", ], ]; ~~~ @@ -202,7 +202,7 @@ const VALUE_SCHEMA = [ "desc" => "description de la valeur", "name" => "identifiant de la valeur", # spécifique à VALUE_SCHEMA - "key" => "chemin de clé de la valeur dans le tableau associatif", + "pkey" => "chemin de clé de la valeur dans le tableau associatif", ], ]; ~~~ diff --git a/src/schema/ScalarSchema.php b/src/schema/ScalarSchema.php index c74c0f9..868ea69 100644 --- a/src/schema/ScalarSchema.php +++ b/src/schema/ScalarSchema.php @@ -5,6 +5,12 @@ use nulib\cl; use nur\sery\schema\input\Input; use nur\sery\schema\ref\ref_schema; use nur\sery\schema\ref\ref_types; +use nur\sery\schema\types\tarray; +use nur\sery\schema\types\tbool; +use nur\sery\schema\types\tcallable; +use nur\sery\schema\types\tcontent; +use nur\sery\schema\types\tpkey; +use nur\sery\schema\types\tstring; use nur\sery\schema\values\IValue; use nur\sery\schema\values\ScalarValue; @@ -26,6 +32,9 @@ use nur\sery\schema\values\ScalarValue; * @property-read mixed $format * @property-read array $nature * @property-read string|int|null $name + * @property-read string|array|null $pkey + * @property-read string|null $header + * @property-read bool|null $composite */ class ScalarSchema extends Schema { /** @var array meta-schema d'un schéma de nature scalaire */ @@ -34,7 +43,7 @@ class ScalarSchema extends Schema { "type", "default", "title", "required", "nullable", "desc", "analyzer_func", "extractor_func", "parser_func", "normalizer_func", "messages", "formatter_func", "format", - "", "name", "key", "header", "composite", + "", "name", "pkey", "header", "composite", ]; /** @@ -68,18 +77,18 @@ class ScalarSchema extends Schema { return $haveIndex0 && $count > 1; } - static function normalize($definition): array { + static function normalize($definition, $definitionKey=null): array { if (!is_array($definition)) $definition = [$definition]; # s'assurer que toutes les clés existent avec leur valeur par défaut $index = 0; - foreach (self::METASCHEMA_KEYS as $key) { - if (!array_key_exists($key, $definition)) { + foreach (self::METASCHEMA_KEYS as $pkey) { + if (!array_key_exists($pkey, $definition)) { if (array_key_exists($index, $definition)) { - $definition[$key] = $definition[$index]; + $definition[$pkey] = $definition[$index]; unset($definition[$index]); $index++; } else { - $definition[$key] = self::METASCHEMA[$key][1]; + $definition[$pkey] = self::METASCHEMA[$pkey][1]; } } } @@ -87,10 +96,10 @@ class ScalarSchema extends Schema { if (cl::have_num_keys($definition)) { $keys = array_keys($definition); $index = 0; - foreach ($keys as $key) { - if (!is_int($key)) continue; - $definition[$index] = $definition[$key]; - unset($definition[$key]); + foreach ($keys as $pkey) { + if (!is_int($pkey)) continue; + $definition[$index] = $definition[$pkey]; + unset($definition[$pkey]); $index++; } } @@ -127,35 +136,36 @@ class ScalarSchema extends Schema { $definition["nullable"] = $nullable; # nature $nature = $definition[""]; - self::ensure_array($nature); + tarray::ensure_array($nature); if (!array_key_exists(0, $nature) || $nature[0] !== "scalar") { throw SchemaException::invalid_schema("expected scalar nature"); } $definition[""] = $nature; - # name, key, header + # name, pkey, header $name = $definition["name"]; - $key = $definition["key"]; + $pkey = $definition["pkey"]; $header = $definition["header"]; - self::ensure_nstring($name); - self::ensure_npkey($key); - self::ensure_nstring($header); - if ($key === null) $key = $name; + if ($name === null) $name = $definitionKey; + tstring::ensure_nstring($name); + tpkey::ensure_npkey($pkey); + tstring::ensure_nstring($header); + if ($pkey === null) $pkey = $name; if ($header === null) $header = $name; $definition["name"] = $name; - $definition["key"] = $key; + $definition["pkey"] = $pkey; $definition["header"] = $header; # autres éléments - self::ensure_nstring($definition["title"]); - self::ensure_bool($definition["required"]); - self::ensure_bool($definition["nullable"]); - self::ensure_ncontent($definition["desc"]); - self::ensure_ncallable($definition["analyzer_func"]); - self::ensure_ncallable($definition["extractor_func"]); - self::ensure_ncallable($definition["parser_func"]); - self::ensure_ncallable($definition["normalizer_func"]); - self::ensure_narray($definition["messages"]); - self::ensure_ncallable($definition["formatter_func"]); - self::ensure_nbool($definition["composite"]); + tstring::ensure_nstring($definition["title"]); + tbool::ensure_bool($definition["required"]); + tbool::ensure_bool($definition["nullable"]); + tcontent::ensure_ncontent($definition["desc"]); + tcallable::ensure_ncallable($definition["analyzer_func"]); + tcallable::ensure_ncallable($definition["extractor_func"]); + tcallable::ensure_ncallable($definition["parser_func"]); + tcallable::ensure_ncallable($definition["normalizer_func"]); + tarray::ensure_narray($definition["messages"]); + tcallable::ensure_ncallable($definition["formatter_func"]); + tbool::ensure_nbool($definition["composite"]); return $definition; } @@ -175,4 +185,16 @@ class ScalarSchema extends Schema { else $input = new Input($value); return new ScalarValue($input, $this, $key); } + + ############################################################################# + # key & properties + + const _PROPERTY_PKEYS = [ + "analyzerFunc" => "analyzer_func", + "extractorFunc" => "extractor_func", + "parserFunc" => "parser_func", + "normalizerFunc" => "normalizer_func", + "formatterFunc" => "formatter_func", + "nature" => ["", 0], + ]; } diff --git a/src/schema/Schema.php b/src/schema/Schema.php index 1697dff..7349e81 100644 --- a/src/schema/Schema.php +++ b/src/schema/Schema.php @@ -1,10 +1,13 @@ definition); + } + function offsetGet($offset) { + if (!array_key_exists($offset, $this->definition)) return null; + else return $this->definition[$offset]; + } + function offsetSet($offset, $value): void { + throw new LogicException("read-only"); + } + function offsetUnset($offset): void { + throw new LogicException("read-only"); + } + + const _PROPERTY_PKEYS = []; + function __get($name) { + $pkey = cl::get(static::_PROPERTY_PKEYS, $name, $name); + return cl::pget($this->definition, $pkey); + } } diff --git a/src/schema/input/FormInput.php b/src/schema/input/FormInput.php index f41f75e..5a48b4d 100644 --- a/src/schema/input/FormInput.php +++ b/src/schema/input/FormInput.php @@ -1,12 +1,15 @@ ["string", null, "nature du schéma", - "key" => 0, + "pkey" => 0, "allowed_values" => ["assoc", "list", "scalar"], ], "title" => ["?string", null, "libellé de la valeur"], @@ -35,7 +35,7 @@ class ref_schema { "" => ["assoc", "schema" => self::NATURE_METASCHEMA], ], "name" => ["?string", null, "identifiant de la valeur"], - "key" => ["?pkey", null, "chemin de clé de la valeur dans un tableau associatif"], + "pkey" => ["?pkey", null, "chemin de clé de la valeur dans un tableau associatif"], "header" => ["?string", null, "nom de l'en-tête s'il faut présenter cette donnée dans un tableau"], "composite" => ["?bool", null, "ce champ fait-il partie d'une valeur composite?"], ]; diff --git a/src/schema/types/tarray.php b/src/schema/types/tarray.php new file mode 100644 index 0000000..1112609 --- /dev/null +++ b/src/schema/types/tarray.php @@ -0,0 +1,14 @@ +