diff --git a/src/schema/TODO.md b/src/schema/TODO.md index 580545a..bbd1432 100644 --- a/src/schema/TODO.md +++ b/src/schema/TODO.md @@ -1,5 +1,8 @@ # nulib\schema +* ScalarSchema::from_property() +* AssocSchema === ScalarSchema(["array", "" => ["scalar", "subtype" => "assoc"]]) +* ListSchema === ScalarSchema(["array", "" => ["scalar", "subtype" => "list"]]) * possibilité de spécifier un type via sa classe, e.g ~~~php Schema::ns($schema, [ @@ -15,7 +18,6 @@ ~~~ MyClass ne doit pas implémenter IType, et le type correspondant est créé avec `new tgeneric(MyClass::class)` -* ScalarSchema::from_property() * dans AssocSchema, support `[key_prefix]` qui permet de spécifier un préfixe commun aux champs dans le tableau destination, e.g diff --git a/src/schema/_scalar/ScalarSchema.php b/src/schema/_scalar/ScalarSchema.php index 3d6594d..507d73c 100644 --- a/src/schema/_scalar/ScalarSchema.php +++ b/src/schema/_scalar/ScalarSchema.php @@ -172,12 +172,12 @@ class ScalarSchema extends Schema { if (count($types) == 1 && $types[0] !== null) { foreach ($types as $key => $name) { if ($key === 0) { - $params = null; + $args = null; } else { - $params = $name; + $args = $name; $name = $key; } - $definition["type"] = types::get($nullable, $name, $params, $definition); + $definition["type"] = types::get($nullable, $name, $args, $definition); } } diff --git a/src/schema/_scalar/ScalarWrapper.php b/src/schema/_scalar/ScalarWrapper.php index da320da..110c6f8 100644 --- a/src/schema/_scalar/ScalarWrapper.php +++ b/src/schema/_scalar/ScalarWrapper.php @@ -106,12 +106,12 @@ class ScalarWrapper extends Wrapper { foreach ($schemaTypes as $key => $name) { if ($key === $index) { $index++; - $params = null; + $args = null; } else { - $params = $name; + $args = $name; $name = $key; } - $type = types::get($schema->nullable, $name, $params, $this->schema->getDefinition()); + $type = types::get($schema->nullable, $name, $args, $this->schema->getDefinition()); if ($firstType === null) $firstType = $type; $types[] = $type; if ($type->isAvailable($input, $valueKey)) { diff --git a/src/schema/types.php b/src/schema/types.php index e3489c5..7e3442a 100644 --- a/src/schema/types.php +++ b/src/schema/types.php @@ -31,8 +31,8 @@ class types { return self::$registry; } - static function get(bool $nullable, ?string $name, ?array $params=null, ?array $definition=null): IType { - return self::registry()->get($nullable, $name, $params, $definition); + static function get(bool $nullable, ?string $name, ?array $args=null, ?array $definition=null): IType { + return self::registry()->get($nullable, $name, $args, $definition); } static function rawstring(bool $nullable=true): trawstring { return self::get($nullable, "rawstring"); } diff --git a/src/schema/types/Registry.php b/src/schema/types/Registry.php index 5c08c92..eb16596 100644 --- a/src/schema/types/Registry.php +++ b/src/schema/types/Registry.php @@ -30,12 +30,21 @@ class Registry { /** @var IType[] */ protected $types; - function get(bool $nullable, ?string $name, ?array $params=null, ?array $definition=null): IType { + function get(bool $nullable, ?string $name, ?array $args=null, ?array $definition=null): IType { $name ??= "raw"; $class = self::TYPES[$name]; + if (cl::is_list($args)) { + $key = array_key_last($args); + $params = $args[$key]; + unset($args[$key]); + } else { + $params = $args; + $args = null; + } $params = cl::merge($class::get_params_from_definition($definition), $params); - if ($params !== null) { - return func::with([$class, false, $nullable, $params])->invoke(); + if ($args || $params !== null) { + $args ??= []; + return func::with([$class, false, ...$args, $nullable, $params])->invoke(); } if ($nullable) $name = "?$name"; $type = cl::get($this->types, $name); diff --git a/src/schema/types/_tsimple.php b/src/schema/types/_tsimple.php index ac0aa36..072806e 100644 --- a/src/schema/types/_tsimple.php +++ b/src/schema/types/_tsimple.php @@ -15,6 +15,10 @@ abstract class _tsimple implements IType { return null; } + /** + * $nullable et $params doivent toujours être les derniers arguments du + * constructeur + */ function __construct(bool $nullable, ?array $params=null) { $this->nullable = $nullable; $this->params = $params;