From fea5cb8b878ce8394a966b722b60a2e36e8d9996 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 30 Apr 2025 16:01:18 +0400 Subject: [PATCH] corrections tarray --- src/schema/Schema.php | 29 +++++++++++++---- src/schema/types/tarray.php | 45 ++++++++++++++++++++----- tests/schema/types/arrayTest.php | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 15 deletions(-) create mode 100644 tests/schema/types/arrayTest.php diff --git a/src/schema/Schema.php b/src/schema/Schema.php index fbb441b..2b96626 100644 --- a/src/schema/Schema.php +++ b/src/schema/Schema.php @@ -127,7 +127,6 @@ abstract class Schema implements ArrayAccess { } } # type - $types = []; $deftype = $definition["type"]; $nullable = $definition["nullable"] ?? false; if ($deftype === null) { @@ -138,7 +137,17 @@ abstract class Schema implements ArrayAccess { if (!is_string($deftype)) throw SchemaException::invalid_type($deftype); $deftype = explode("|", $deftype); } - foreach ($deftype as $type) { + $types = []; + $unionTypes = []; + $index = 0; + foreach ($deftype as $key => $type) { + $args = null; + if ($key === $index) { + $index++; + } else { + $args = $type; + $type = $key; + } if ($type !== null) $type = trim($type); if ($type === null || $type === "null") { $nullable = true; @@ -151,10 +160,18 @@ abstract class Schema implements ArrayAccess { } if ($type === "") throw SchemaException::invalid_type($type); $type = cl::get(ref_types::ALIASES, $type, $type); - $types = array_merge($types, explode("|", $type)); + if ($args === null) { + $unionTypes = array_merge($unionTypes, explode("|", $type)); + } else { + $types = array_merge($types, [$type => $args]); + } + } + if (!$types && !$unionTypes) throw SchemaException::invalid_schema("scalar: type is required"); + foreach ($unionTypes as $type) { + if (!array_key_exists($type, $types)) { + $types[] = $type; + } } - if (!$types) throw SchemaException::invalid_schema("scalar: type is required"); - $types = array_keys(array_fill_keys($types, true)); } $definition["type"] = $types; $definition["nullable"] = $nullable; @@ -224,7 +241,7 @@ abstract class Schema implements ArrayAccess { $types = $definition["type"]; $nullable = $definition["nullable"]; # s'il n'y a qu'une seul type, l'instancier tout de suite - if (is_array($types) && count($types) == 1 && $types[0] !== null) { + if (is_array($types) && count($types) == 1 && cl::first($types) !== null) { foreach ($types as $key => $name) { if ($key === 0) { $args = null; diff --git a/src/schema/types/tarray.php b/src/schema/types/tarray.php index 05cfe7c..d53cddc 100644 --- a/src/schema/types/tarray.php +++ b/src/schema/types/tarray.php @@ -5,19 +5,45 @@ use nulib\cl; use nulib\schema\_scalar\ScalarSchema; use nulib\schema\Result; use nulib\schema\Schema; +use nulib\str; class tarray extends _tstring { const NAME = "array"; - const SPLIT_PATTERN = '/\s+/'; - const FORMAT = " "; + const TRIM = true; + const NORM_NL = true; + + const SEP_MAP = [ + "space" => "spaces", + "lines" => "line", + ]; + const STD_SEPS = [ + "spaces" => [" ", '/\s+/', true], + "line" => ["\n", "\n", false], + ]; + + const DEFAULT_SEP = self::STD_SEPS["spaces"]; + const DEFAULT_PARSE_SEP = self::DEFAULT_SEP[1]; + const DEFAULT_FORMAT_SEP = self::DEFAULT_SEP[0]; public static function get_params_from_definition(?array $definition): ?array { $params = parent::get_params_from_definition($definition); - $splitPattern = $definition["split_pattern"] ?? null; - if ($splitPattern !== null) $params["split_pattern"] = $splitPattern; - $format = $definition["format"] ?? null; - if ($format !== null) $params["format"] = $format; + $sep = $definition["sep"] ?? null; + if ($sep !== null) { + if (!is_array($sep)) { + $sep = cl::get(self::SEP_MAP, $sep, $sep); + $sep = self::STD_SEPS[$sep] ?? [$sep, $sep, false]; + } + $params["parse_sep"] = $sep[1] ?? $sep[0]; + $params["format_sep"] = $sep[0]; + $params["trim"] ??= $sep[2] ?? static::TRIM; + $params["norm_nl"] ??= $sep[3] ?? static::NORM_NL; + } else { + $parseSep = $definition["parse_sep"] ?? null; + if ($parseSep !== null) $params["parse_sep"] = $parseSep; + $formatSep = $definition["format_sep"] ?? null; + if ($formatSep !== null) $params["format_sep"] = $formatSep; + } return $params; } @@ -43,8 +69,9 @@ class tarray extends _tstring { } function parse(string $value) { - $pattern = $this->params["split_pattern"] ?? static::SPLIT_PATTERN; - return preg_split($pattern, $value); + $sep = $this->params["parse_sep"] ?? static::DEFAULT_PARSE_SEP; + if ($sep !== false) $value = str::split($sep, $value); + return $value; } /** @@ -63,7 +90,7 @@ class tarray extends _tstring { function format($value, $format=null): string { if ($value === null) return ""; - $format ??= $this->params["format"] ?? static::FORMAT; + $format ??= $this->params["format"] ?? static::DEFAULT_FORMAT_SEP; return implode($format, $value); } } diff --git a/tests/schema/types/arrayTest.php b/tests/schema/types/arrayTest.php new file mode 100644 index 0000000..95a7e9a --- /dev/null +++ b/tests/schema/types/arrayTest.php @@ -0,0 +1,56 @@ + "spaces", + ]); + self::assertSame(["first", "second"], $value); + + $value = " first second "; + Schema::nw($value, null, [ + "array", + "sep" => "spaces", "trim" => false, + ]); + self::assertSame(["", "first", "second", ""], $value); + + $value = " first second "; + Schema::nw($value, null, [ + "array", + "sep" => "line", + ]); + self::assertSame([" first second "], $value); + + $value = " first second "; + Schema::nw($value, null, [ + "array", + "sep" => "line", "trim" => true, + ]); + self::assertSame(["first second"], $value); + } + + function testxxx() { + $value = " first second "; + Schema::nw($value, null, [ + "array", "sep" => "spaces", + ]); + self::assertSame(["first", "second"], $value); + + # équivalent à... + $value = " first second "; + Schema::nw($value, null, [ + "type" => ["array" => [["sep" => "spaces"]]], + "" => ["scalar"], + ]); + self::assertSame(["first", "second"], $value); + } +}