modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-09-15 20:13:51 +04:00
parent 948b94c5cd
commit b0f5de5e9f
11 changed files with 135 additions and 42 deletions

View File

@ -4,11 +4,21 @@ namespace nulib\php\types;
use nulib\cl; use nulib\cl;
class varray { class varray {
static function ensure_array(&$array): void { static function ensure(&$array): void {
if (!is_array($array)) $array = cl::with($array); if (!is_array($array)) $array = cl::with($array);
} }
static function ensure_narray(&$array): void { static function ensuren(&$array): void {
if ($array !== null) varray::ensure_array($array); 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;
} }
} }

View File

@ -32,7 +32,7 @@ class vbool {
return in_array($value, self::NO_VALUES, true); return in_array($value, self::NO_VALUES, true);
} }
static function ensure_bool(&$bool): void { static function ensure(&$bool): void {
if (is_string($bool)) { if (is_string($bool)) {
if (self::is_yes($bool)) $bool = true; if (self::is_yes($bool)) $bool = true;
elseif (self::is_no($bool)) $bool = false; elseif (self::is_no($bool)) $bool = false;
@ -40,7 +40,17 @@ class vbool {
if (!is_bool($bool)) $bool = boolval($bool); if (!is_bool($bool)) $bool = boolval($bool);
} }
static function ensure_nbool(&$bool): void { static function ensuren(&$bool): void {
if ($bool !== null) self::ensure_bool($bool); 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;
} }
} }

View File

@ -2,12 +2,24 @@
namespace nulib\php\types; namespace nulib\php\types;
class vcontent { class vcontent {
static function ensure_content(&$content): void { static function ensure(&$content): void {
if ($content === null || $content === false) $content = []; if ($content === null || $content === false) $content = [];
elseif (!is_string($content) && !is_array($content)) $content = strval($content); elseif (!is_string($content) && !is_array($content)) $content = strval($content);
} }
static function ensure_ncontent(&$content): void { static function ensuren(&$content): void {
if ($content !== null) self::ensure_content($content); 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;
} }
} }

View File

@ -2,11 +2,21 @@
namespace nulib\php\types; namespace nulib\php\types;
class vfloat { class vfloat {
static function ensure_float(&$float): void { static function ensure(&$float): void {
if (!is_float($float)) $float = floatval($float); if (!is_float($float)) $float = floatval($float);
} }
static function ensure_nfloat(&$float): void { static function ensuren(&$float): void {
if ($float !== null) self::ensure_float($float); 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;
} }
} }

View File

@ -4,11 +4,19 @@ namespace nulib\php\types;
use nulib\php\func; use nulib\php\func;
class vfunc { class vfunc {
static function ensure_func(&$func): void { static function ensure(&$func): void {
$func = func::ensure($func); $func = func::ensure($func);
} }
static function ensure_nfunc(&$func): void { static function ensuren(&$func): void {
if ($func !== null) self::ensure_func($func); 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);
} }
} }

View File

@ -2,11 +2,21 @@
namespace nulib\php\types; namespace nulib\php\types;
class vint { class vint {
static function ensure_int(&$int): void { static function ensure(&$int): void {
if (!is_int($int)) $int = intval($int); if (!is_int($int)) $int = intval($int);
} }
static function ensure_nint(&$int): void { static function ensuren(&$int): void {
if ($int !== null) self::ensure_int($int); 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;
} }
} }

View File

@ -2,13 +2,25 @@
namespace nulib\php\types; namespace nulib\php\types;
class vkey { class vkey {
static function ensure_key(&$key): void { static function ensure(&$key): void {
if ($key === null) $key = ""; if ($key === null) $key = "";
elseif ($key === false) $key = 0; elseif ($key === false) $key = 0;
elseif (!is_string($key) && !is_int($key)) $key = strval($key); elseif (!is_string($key) && !is_int($key)) $key = strval($key);
} }
static function ensure_nkey(&$key): void { static function ensuren(&$key): void {
if ($key !== null) self::ensure_key($key); 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;
} }
} }

View File

@ -2,19 +2,31 @@
namespace nulib\php\types; namespace nulib\php\types;
class vpkey { class vpkey {
static function ensure_pkey(&$pkey): void { static function ensure(&$pkey): void {
if ($pkey === null) $pkey = ""; if ($pkey === null) $pkey = "";
elseif ($pkey === false) $pkey = 0; elseif ($pkey === false) $pkey = 0;
elseif (!is_string($pkey) && !is_int($pkey) && !is_array($pkey)) $pkey = strval($pkey); elseif (!is_string($pkey) && !is_int($pkey) && !is_array($pkey)) $pkey = strval($pkey);
if (is_array($pkey)) { if (is_array($pkey)) {
foreach ($pkey as &$key) { foreach ($pkey as &$key) {
vkey::ensure_key($key); vkey::ensure($key);
}; };
unset($key); unset($key);
} }
} }
static function ensure_npkey(&$pkey): void { static function ensuren(&$pkey): void {
if ($pkey !== null) self::ensure_pkey($pkey); 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;
} }
} }

View File

@ -9,13 +9,23 @@ class vrawstring {
/** @var bool faut-il normaliser les caractères fin de ligne */ /** @var bool faut-il normaliser les caractères fin de ligne */
const NORM_NL = false; const NORM_NL = false;
static function ensure_string(&$string): void { static function ensure(&$string): void {
if (!is_string($string)) $string = strval($string); if (!is_string($string)) $string = strval($string);
if (static::TRIM) $string = trim($string); if (static::TRIM) $string = trim($string);
if (static::NORM_NL) $string = str::norm_nl($string); if (static::NORM_NL) $string = str::norm_nl($string);
} }
static function ensure_nstring(&$string): void { static function ensuren(&$string): void {
if ($string !== null) self::ensure_string($string); 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;
} }
} }

View File

@ -3,6 +3,5 @@ namespace nulib\php\types;
class vtext extends vrawstring { class vtext extends vrawstring {
const TRIM = true; const TRIM = true;
const NORM_NL = true; const NORM_NL = true;
} }

View File

@ -182,7 +182,7 @@ abstract class Schema implements ArrayAccess {
$definition["nullable"] = $nullable; $definition["nullable"] = $nullable;
# nature # nature
$nature = $definition[""]; $nature = $definition[""];
varray::ensure_array($nature); varray::ensure($nature);
$natureMetaschema ??= ref_schema::NATURE_METASCHEMA; $natureMetaschema ??= ref_schema::NATURE_METASCHEMA;
foreach (array_keys($natureMetaschema) as $key) { foreach (array_keys($natureMetaschema) as $key) {
if (!array_key_exists($key, $nature)) { if (!array_key_exists($key, $nature)) {
@ -196,7 +196,7 @@ abstract class Schema implements ArrayAccess {
$header = $definition["header"]; $header = $definition["header"];
if ($name === null) $name = $definitionKey; if ($name === null) $name = $definitionKey;
trawstring::ensure_nstring($name); trawstring::ensure_nstring($name);
vpkey::ensure_npkey($pkey); vpkey::ensuren($pkey);
trawstring::ensure_nstring($header); trawstring::ensure_nstring($header);
if ($pkey === null) $pkey = $name; if ($pkey === null) $pkey = $name;
if ($header === null) $header = $name; if ($header === null) $header = $name;
@ -204,18 +204,18 @@ abstract class Schema implements ArrayAccess {
$definition["pkey"] = $pkey; $definition["pkey"] = $pkey;
$definition["header"] = $header; $definition["header"] = $header;
# autres éléments # autres éléments
varray::ensure_narray($definition["schema"]); varray::ensuren($definition["schema"]);
trawstring::ensure_nstring($definition["title"]); trawstring::ensure_nstring($definition["title"]);
vbool::ensure_bool($definition["required"]); vbool::ensure($definition["required"]);
vbool::ensure_bool($definition["nullable"]); vbool::ensure($definition["nullable"]);
vcontent::ensure_ncontent($definition["desc"]); vcontent::ensuren($definition["desc"]);
vfunc::ensure_nfunc($definition["analyzer_func"]); vfunc::ensuren($definition["analyzer_func"]);
vfunc::ensure_nfunc($definition["extractor_func"]); vfunc::ensuren($definition["extractor_func"]);
vfunc::ensure_nfunc($definition["parser_func"]); vfunc::ensuren($definition["parser_func"]);
vfunc::ensure_nfunc($definition["normalizer_func"]); vfunc::ensuren($definition["normalizer_func"]);
varray::ensure_narray($definition["messages"]); varray::ensuren($definition["messages"]);
vfunc::ensure_nfunc($definition["formatter_func"]); vfunc::ensuren($definition["formatter_func"]);
vbool::ensure_nbool($definition["computed"]); vbool::ensuren($definition["computed"]);
switch ($nature[0] ?? null) { switch ($nature[0] ?? null) {
case "assoc": case "assoc":