From b0f5de5e9fe1550f61b3624292c517fb23a19d07 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Mon, 15 Sep 2025 20:13:51 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/php/types/varray.php | 16 +++++++++++++--- src/php/types/vbool.php | 16 +++++++++++++--- src/php/types/vcontent.php | 18 +++++++++++++++--- src/php/types/vfloat.php | 16 +++++++++++++--- src/php/types/vfunc.php | 14 +++++++++++--- src/php/types/vint.php | 16 +++++++++++++--- src/php/types/vkey.php | 18 +++++++++++++++--- src/php/types/vpkey.php | 20 ++++++++++++++++---- src/php/types/vrawstring.php | 16 +++++++++++++--- src/php/types/vtext.php | 1 - src/schema/Schema.php | 26 +++++++++++++------------- 11 files changed, 135 insertions(+), 42 deletions(-) diff --git a/src/php/types/varray.php b/src/php/types/varray.php index 8f517e4..bc46388 100644 --- a/src/php/types/varray.php +++ b/src/php/types/varray.php @@ -4,11 +4,21 @@ namespace nulib\php\types; use nulib\cl; class varray { - static function ensure_array(&$array): void { + static function ensure(&$array): void { if (!is_array($array)) $array = cl::with($array); } - static function ensure_narray(&$array): void { - if ($array !== null) varray::ensure_array($array); + static function ensuren(&$array): void { + if ($array !== null) varray::ensure($array); + } + + static function with($value): array { + self::ensure($value); + return $value; + } + + static function withn($value): ?array { + self::ensuren($value); + return $value; } } diff --git a/src/php/types/vbool.php b/src/php/types/vbool.php index f4c9167..e057f3d 100644 --- a/src/php/types/vbool.php +++ b/src/php/types/vbool.php @@ -32,7 +32,7 @@ class vbool { return in_array($value, self::NO_VALUES, true); } - static function ensure_bool(&$bool): void { + static function ensure(&$bool): void { if (is_string($bool)) { if (self::is_yes($bool)) $bool = true; elseif (self::is_no($bool)) $bool = false; @@ -40,7 +40,17 @@ class vbool { if (!is_bool($bool)) $bool = boolval($bool); } - static function ensure_nbool(&$bool): void { - if ($bool !== null) self::ensure_bool($bool); + static function ensuren(&$bool): void { + if ($bool !== null) self::ensure($bool); + } + + static function with($value): bool { + self::ensure($value); + return $value; + } + + static function withn($value): ?bool { + self::ensuren($value); + return $value; } } diff --git a/src/php/types/vcontent.php b/src/php/types/vcontent.php index 80de469..cfb5b77 100644 --- a/src/php/types/vcontent.php +++ b/src/php/types/vcontent.php @@ -2,12 +2,24 @@ namespace nulib\php\types; class vcontent { - static function ensure_content(&$content): void { + static function ensure(&$content): void { if ($content === null || $content === false) $content = []; elseif (!is_string($content) && !is_array($content)) $content = strval($content); } - static function ensure_ncontent(&$content): void { - if ($content !== null) self::ensure_content($content); + static function ensuren(&$content): void { + if ($content !== null) self::ensure($content); + } + + /** @return string|array */ + static function with($value) { + self::ensure($value); + return $value; + } + + /** @return string|array|null */ + static function withn($value) { + self::ensuren($value); + return $value; } } diff --git a/src/php/types/vfloat.php b/src/php/types/vfloat.php index b4d53e0..3233b73 100644 --- a/src/php/types/vfloat.php +++ b/src/php/types/vfloat.php @@ -2,11 +2,21 @@ namespace nulib\php\types; class vfloat { - static function ensure_float(&$float): void { + static function ensure(&$float): void { if (!is_float($float)) $float = floatval($float); } - static function ensure_nfloat(&$float): void { - if ($float !== null) self::ensure_float($float); + static function ensuren(&$float): void { + if ($float !== null) self::ensure($float); + } + + static function with($value): float { + self::ensure($value); + return $value; + } + + static function withn($value): ?float { + self::ensuren($value); + return $value; } } diff --git a/src/php/types/vfunc.php b/src/php/types/vfunc.php index 25ea6dc..ec690bf 100644 --- a/src/php/types/vfunc.php +++ b/src/php/types/vfunc.php @@ -4,11 +4,19 @@ namespace nulib\php\types; use nulib\php\func; class vfunc { - static function ensure_func(&$func): void { + static function ensure(&$func): void { $func = func::ensure($func); } - static function ensure_nfunc(&$func): void { - if ($func !== null) self::ensure_func($func); + static function ensuren(&$func): void { + if ($func !== null) $func = func::ensure($func); + } + + static function with($value): func { + return func::with($value); + } + + static function withn($value): ?func { + return func::withn($value); } } diff --git a/src/php/types/vint.php b/src/php/types/vint.php index 7a6a35a..6d5ed4c 100644 --- a/src/php/types/vint.php +++ b/src/php/types/vint.php @@ -2,11 +2,21 @@ namespace nulib\php\types; class vint { - static function ensure_int(&$int): void { + static function ensure(&$int): void { if (!is_int($int)) $int = intval($int); } - static function ensure_nint(&$int): void { - if ($int !== null) self::ensure_int($int); + static function ensuren(&$int): void { + if ($int !== null) self::ensure($int); + } + + static function with($value): int { + self::ensure($value); + return $value; + } + + static function withn($value): ?int { + self::ensuren($value); + return $value; } } diff --git a/src/php/types/vkey.php b/src/php/types/vkey.php index 9c4acc5..87a8402 100644 --- a/src/php/types/vkey.php +++ b/src/php/types/vkey.php @@ -2,13 +2,25 @@ namespace nulib\php\types; class vkey { - static function ensure_key(&$key): void { + static function ensure(&$key): void { if ($key === null) $key = ""; elseif ($key === false) $key = 0; elseif (!is_string($key) && !is_int($key)) $key = strval($key); } - static function ensure_nkey(&$key): void { - if ($key !== null) self::ensure_key($key); + static function ensuren(&$key): void { + if ($key !== null) self::ensure($key); + } + + /** @return string|int */ + static function with($value) { + self::ensure($value); + return $value; + } + + /** @return string|int|null */ + static function withn($value) { + self::ensuren($value); + return $value; } } diff --git a/src/php/types/vpkey.php b/src/php/types/vpkey.php index c0841cb..318c7bb 100644 --- a/src/php/types/vpkey.php +++ b/src/php/types/vpkey.php @@ -2,19 +2,31 @@ namespace nulib\php\types; class vpkey { - static function ensure_pkey(&$pkey): void { + static function ensure(&$pkey): void { if ($pkey === null) $pkey = ""; elseif ($pkey === false) $pkey = 0; elseif (!is_string($pkey) && !is_int($pkey) && !is_array($pkey)) $pkey = strval($pkey); if (is_array($pkey)) { foreach ($pkey as &$key) { - vkey::ensure_key($key); + vkey::ensure($key); }; unset($key); } } - static function ensure_npkey(&$pkey): void { - if ($pkey !== null) self::ensure_pkey($pkey); + static function ensuren(&$pkey): void { + if ($pkey !== null) self::ensure($pkey); + } + + /** @return string|int|array */ + static function with($value) { + self::ensure($value); + return $value; + } + + /** @return string|int|array|null */ + static function withn($value) { + self::ensuren($value); + return $value; } } diff --git a/src/php/types/vrawstring.php b/src/php/types/vrawstring.php index 7650c78..463aba3 100644 --- a/src/php/types/vrawstring.php +++ b/src/php/types/vrawstring.php @@ -9,13 +9,23 @@ class vrawstring { /** @var bool faut-il normaliser les caractères fin de ligne */ const NORM_NL = false; - static function ensure_string(&$string): void { + static function ensure(&$string): void { if (!is_string($string)) $string = strval($string); if (static::TRIM) $string = trim($string); if (static::NORM_NL) $string = str::norm_nl($string); } - static function ensure_nstring(&$string): void { - if ($string !== null) self::ensure_string($string); + static function ensuren(&$string): void { + if ($string !== null) self::ensure($string); + } + + static function with($value): string { + self::ensure($value); + return $value; + } + + static function withn($value): ?string { + self::ensuren($value); + return $value; } } diff --git a/src/php/types/vtext.php b/src/php/types/vtext.php index afe4c9f..147be33 100644 --- a/src/php/types/vtext.php +++ b/src/php/types/vtext.php @@ -3,6 +3,5 @@ namespace nulib\php\types; class vtext extends vrawstring { const TRIM = true; - const NORM_NL = true; } diff --git a/src/schema/Schema.php b/src/schema/Schema.php index 67badaf..1ee163c 100644 --- a/src/schema/Schema.php +++ b/src/schema/Schema.php @@ -182,7 +182,7 @@ abstract class Schema implements ArrayAccess { $definition["nullable"] = $nullable; # nature $nature = $definition[""]; - varray::ensure_array($nature); + varray::ensure($nature); $natureMetaschema ??= ref_schema::NATURE_METASCHEMA; foreach (array_keys($natureMetaschema) as $key) { if (!array_key_exists($key, $nature)) { @@ -196,7 +196,7 @@ abstract class Schema implements ArrayAccess { $header = $definition["header"]; if ($name === null) $name = $definitionKey; trawstring::ensure_nstring($name); - vpkey::ensure_npkey($pkey); + vpkey::ensuren($pkey); trawstring::ensure_nstring($header); if ($pkey === null) $pkey = $name; if ($header === null) $header = $name; @@ -204,18 +204,18 @@ abstract class Schema implements ArrayAccess { $definition["pkey"] = $pkey; $definition["header"] = $header; # autres éléments - varray::ensure_narray($definition["schema"]); + varray::ensuren($definition["schema"]); trawstring::ensure_nstring($definition["title"]); - vbool::ensure_bool($definition["required"]); - vbool::ensure_bool($definition["nullable"]); - vcontent::ensure_ncontent($definition["desc"]); - vfunc::ensure_nfunc($definition["analyzer_func"]); - vfunc::ensure_nfunc($definition["extractor_func"]); - vfunc::ensure_nfunc($definition["parser_func"]); - vfunc::ensure_nfunc($definition["normalizer_func"]); - varray::ensure_narray($definition["messages"]); - vfunc::ensure_nfunc($definition["formatter_func"]); - vbool::ensure_nbool($definition["computed"]); + vbool::ensure($definition["required"]); + vbool::ensure($definition["nullable"]); + vcontent::ensuren($definition["desc"]); + vfunc::ensuren($definition["analyzer_func"]); + vfunc::ensuren($definition["extractor_func"]); + vfunc::ensuren($definition["parser_func"]); + vfunc::ensuren($definition["normalizer_func"]); + varray::ensuren($definition["messages"]); + vfunc::ensuren($definition["formatter_func"]); + vbool::ensuren($definition["computed"]); switch ($nature[0] ?? null) { case "assoc":