corrections tarray

This commit is contained in:
Jephté Clain 2025-04-30 16:01:18 +04:00
parent 5dd9ebbf65
commit fea5cb8b87
3 changed files with 115 additions and 15 deletions

View File

@ -127,7 +127,6 @@ abstract class Schema implements ArrayAccess {
} }
} }
# type # type
$types = [];
$deftype = $definition["type"]; $deftype = $definition["type"];
$nullable = $definition["nullable"] ?? false; $nullable = $definition["nullable"] ?? false;
if ($deftype === null) { if ($deftype === null) {
@ -138,7 +137,17 @@ abstract class Schema implements ArrayAccess {
if (!is_string($deftype)) throw SchemaException::invalid_type($deftype); if (!is_string($deftype)) throw SchemaException::invalid_type($deftype);
$deftype = explode("|", $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 = trim($type);
if ($type === null || $type === "null") { if ($type === null || $type === "null") {
$nullable = true; $nullable = true;
@ -151,10 +160,18 @@ abstract class Schema implements ArrayAccess {
} }
if ($type === "") throw SchemaException::invalid_type($type); if ($type === "") throw SchemaException::invalid_type($type);
$type = cl::get(ref_types::ALIASES, $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["type"] = $types;
$definition["nullable"] = $nullable; $definition["nullable"] = $nullable;
@ -224,7 +241,7 @@ abstract class Schema implements ArrayAccess {
$types = $definition["type"]; $types = $definition["type"];
$nullable = $definition["nullable"]; $nullable = $definition["nullable"];
# s'il n'y a qu'une seul type, l'instancier tout de suite # 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) { foreach ($types as $key => $name) {
if ($key === 0) { if ($key === 0) {
$args = null; $args = null;

View File

@ -5,19 +5,45 @@ use nulib\cl;
use nulib\schema\_scalar\ScalarSchema; use nulib\schema\_scalar\ScalarSchema;
use nulib\schema\Result; use nulib\schema\Result;
use nulib\schema\Schema; use nulib\schema\Schema;
use nulib\str;
class tarray extends _tstring { class tarray extends _tstring {
const NAME = "array"; const NAME = "array";
const SPLIT_PATTERN = '/\s+/'; const TRIM = true;
const FORMAT = " "; 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 { public static function get_params_from_definition(?array $definition): ?array {
$params = parent::get_params_from_definition($definition); $params = parent::get_params_from_definition($definition);
$splitPattern = $definition["split_pattern"] ?? null; $sep = $definition["sep"] ?? null;
if ($splitPattern !== null) $params["split_pattern"] = $splitPattern; if ($sep !== null) {
$format = $definition["format"] ?? null; if (!is_array($sep)) {
if ($format !== null) $params["format"] = $format; $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; return $params;
} }
@ -43,8 +69,9 @@ class tarray extends _tstring {
} }
function parse(string $value) { function parse(string $value) {
$pattern = $this->params["split_pattern"] ?? static::SPLIT_PATTERN; $sep = $this->params["parse_sep"] ?? static::DEFAULT_PARSE_SEP;
return preg_split($pattern, $value); if ($sep !== false) $value = str::split($sep, $value);
return $value;
} }
/** /**
@ -63,7 +90,7 @@ class tarray extends _tstring {
function format($value, $format=null): string { function format($value, $format=null): string {
if ($value === null) return ""; if ($value === null) return "";
$format ??= $this->params["format"] ?? static::FORMAT; $format ??= $this->params["format"] ?? static::DEFAULT_FORMAT_SEP;
return implode($format, $value); return implode($format, $value);
} }
} }

View File

@ -0,0 +1,56 @@
<?php
namespace nulib\schema\types;
use nulib\schema\Schema;
use nur\t\TestCase;
class arrayTest extends TestCase {
function testSchema() {
$value = " first second ";
Schema::nw($value, null, "array");
self::assertSame(["first", "second"], $value);
$value = " first second ";
Schema::nw($value, null, [
"array", "sep" => "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);
}
}