classes outils v*

This commit is contained in:
Jephté Clain 2025-09-15 19:37:18 +04:00
parent db89ff2abe
commit 948b94c5cd
21 changed files with 202 additions and 132 deletions

14
src/php/types/varray.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace nulib\php\types;
use nulib\cl;
class varray {
static function ensure_array(&$array): void {
if (!is_array($array)) $array = cl::with($array);
}
static function ensure_narray(&$array): void {
if ($array !== null) varray::ensure_array($array);
}
}

46
src/php/types/vbool.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace nulib\php\types;
use nulib\schema\types\tbool;
class vbool {
/** liste de valeurs chaines à considérer comme 'OUI' */
public const YES_VALUES = [
"true", "vrai", "yes", "oui",
"t", "v", "y", "o", "1",
];
/** liste de valeurs chaines à considérer comme 'NON' */
public const NO_VALUES = [
"false", "faux", "non", "no",
"f", "n", "0",
];
/** Vérifier si $value est 'OUI' */
static final function is_yes(?string $value): bool {
if ($value === null) return false;
$value = strtolower(trim($value));
if (in_array($value, self::YES_VALUES, true)) return true;
// n'importe quelle valeur numérique
if (is_numeric($value)) return $value != 0;
return false;
}
/** Vérifier si $value est 'NON' */
static final function is_no(?string $value): bool {
if ($value === null) return true;
$value = strtolower(trim($value));
return in_array($value, self::NO_VALUES, true);
}
static function ensure_bool(&$bool): void {
if (is_string($bool)) {
if (self::is_yes($bool)) $bool = true;
elseif (self::is_no($bool)) $bool = false;
}
if (!is_bool($bool)) $bool = boolval($bool);
}
static function ensure_nbool(&$bool): void {
if ($bool !== null) self::ensure_bool($bool);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace nulib\php\types;
class vcontent {
static function ensure_content(&$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);
}
}

12
src/php/types/vfloat.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace nulib\php\types;
class vfloat {
static function ensure_float(&$float): void {
if (!is_float($float)) $float = floatval($float);
}
static function ensure_nfloat(&$float): void {
if ($float !== null) self::ensure_float($float);
}
}

14
src/php/types/vfunc.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace nulib\php\types;
use nulib\php\func;
class vfunc {
static function ensure_func(&$func): void {
$func = func::ensure($func);
}
static function ensure_nfunc(&$func): void {
if ($func !== null) self::ensure_func($func);
}
}

12
src/php/types/vint.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace nulib\php\types;
class vint {
static function ensure_int(&$int): void {
if (!is_int($int)) $int = intval($int);
}
static function ensure_nint(&$int): void {
if ($int !== null) self::ensure_int($int);
}
}

14
src/php/types/vkey.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace nulib\php\types;
class vkey {
static function ensure_key(&$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);
}
}

20
src/php/types/vpkey.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace nulib\php\types;
class vpkey {
static function ensure_pkey(&$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);
};
unset($key);
}
}
static function ensure_npkey(&$pkey): void {
if ($pkey !== null) self::ensure_pkey($pkey);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace nulib\php\types;
use nulib\str;
class vrawstring {
/** @var bool faut-il trimmer la valeur */
const TRIM = false;
/** @var bool faut-il normaliser les caractères fin de ligne */
const NORM_NL = false;
static function ensure_string(&$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);
}
}

View File

@ -0,0 +1,6 @@
<?php
namespace nulib\php\types;
class vstring extends vrawstring {
const TRIM = true;
}

8
src/php/types/vtext.php Normal file
View File

@ -0,0 +1,8 @@
<?php
namespace nulib\php\types;
class vtext extends vrawstring {
const TRIM = true;
const NORM_NL = true;
}

View File

@ -4,6 +4,11 @@ namespace nulib\schema;
use ArrayAccess;
use nulib\AccessException;
use nulib\cl;
use nulib\php\types\varray;
use nulib\php\types\vbool;
use nulib\php\types\vcontent;
use nulib\php\types\vfunc;
use nulib\php\types\vpkey;
use nulib\ref\schema\ref_schema;
use nulib\ref\schema\ref_types;
use nulib\schema\_assoc\AssocSchema;
@ -177,7 +182,7 @@ abstract class Schema implements ArrayAccess {
$definition["nullable"] = $nullable;
# nature
$nature = $definition[""];
tarray::ensure_array($nature);
varray::ensure_array($nature);
$natureMetaschema ??= ref_schema::NATURE_METASCHEMA;
foreach (array_keys($natureMetaschema) as $key) {
if (!array_key_exists($key, $nature)) {
@ -191,7 +196,7 @@ abstract class Schema implements ArrayAccess {
$header = $definition["header"];
if ($name === null) $name = $definitionKey;
trawstring::ensure_nstring($name);
tpkey::ensure_npkey($pkey);
vpkey::ensure_npkey($pkey);
trawstring::ensure_nstring($header);
if ($pkey === null) $pkey = $name;
if ($header === null) $header = $name;
@ -199,18 +204,18 @@ abstract class Schema implements ArrayAccess {
$definition["pkey"] = $pkey;
$definition["header"] = $header;
# autres éléments
tarray::ensure_narray($definition["schema"]);
varray::ensure_narray($definition["schema"]);
trawstring::ensure_nstring($definition["title"]);
tbool::ensure_bool($definition["required"]);
tbool::ensure_bool($definition["nullable"]);
tcontent::ensure_ncontent($definition["desc"]);
tfunc::ensure_nfunc($definition["analyzer_func"]);
tfunc::ensure_nfunc($definition["extractor_func"]);
tfunc::ensure_nfunc($definition["parser_func"]);
tfunc::ensure_nfunc($definition["normalizer_func"]);
tarray::ensure_narray($definition["messages"]);
tfunc::ensure_nfunc($definition["formatter_func"]);
tbool::ensure_nbool($definition["computed"]);
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"]);
switch ($nature[0] ?? null) {
case "assoc":

View File

@ -47,14 +47,6 @@ class tarray extends _tstring {
return $params;
}
static function ensure_array(&$array): void {
if (!is_array($array)) $array = cl::with($array);
}
static function ensure_narray(&$array): void {
if ($array !== null) self::ensure_array($array);
}
function getClass(): string {
return "array";
}

View File

@ -2,6 +2,7 @@
namespace nulib\schema\types;
use nulib\cl;
use nulib\php\types\vbool;
use nulib\schema\_scalar\ScalarSchema;
use nulib\schema\input\Input;
use nulib\schema\Result;
@ -14,47 +15,6 @@ class tbool extends _tformatable {
const ALIASES = ["boolean"];
/** liste de valeurs chaines à considérer comme 'OUI' */
const YES_VALUES = [
"true", "vrai", "yes", "oui",
"t", "v", "y", "o", "1",
];
/** liste de valeurs chaines à considérer comme 'NON' */
const NO_VALUES = [
"false", "faux", "non", "no",
"f", "n", "0",
];
/** Vérifier si $value est 'OUI' */
static final function is_yes(?string $value): bool {
if ($value === null) return false;
$value = strtolower(trim($value));
if (in_array($value, self::YES_VALUES, true)) return true;
// n'importe quelle valeur numérique
if (is_numeric($value)) return $value != 0;
return false;
}
/** Vérifier si $value est 'NON' */
static final function is_no(?string $value): bool {
if ($value === null) return true;
$value = strtolower(trim($value));
return in_array($value, self::NO_VALUES, true);
}
static function ensure_bool(&$bool): void {
if (is_string($bool)) {
if (self::is_yes($bool)) $bool = true;
elseif (self::is_no($bool)) $bool = false;
}
if (!is_bool($bool)) $bool = boolval($bool);
}
static function ensure_nbool(&$bool): void {
if ($bool !== null) self::ensure_bool($bool);
}
function getClass(): string {
return "bool";
}
@ -92,8 +52,8 @@ class tbool extends _tformatable {
}
function parse(string $value) {
if (self::is_yes($value)) return true;
elseif (self::is_no($value)) return false;
if (vbool::is_yes($value)) return true;
elseif (vbool::is_no($value)) return false;
throw new ValueException("une valeur booléenne est attendue");
}

View File

@ -9,15 +9,6 @@ use nulib\schema\Schema;
abstract class tcontent extends _tunion {
const NAME = "content";
static function ensure_content(&$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);
}
function getClass(): string {
return "string|array";
}

View File

@ -11,14 +11,6 @@ class tfloat extends _tformatable {
const ALIASES = ["flt", "double", "dbl"];
static function ensure_float(&$float): void {
if (!is_float($float)) $float = floatval($float);
}
static function ensure_nfloat(&$float): void {
if ($float !== null) self::ensure_float($float);
}
function getClass(): string {
return "float";
}

View File

@ -13,14 +13,6 @@ class tfunc extends _tsimple {
const ALIASES = ["function", "callable"];
static function ensure_func(&$func): void {
$func = func::ensure($func);
}
static function ensure_nfunc(&$func): void {
if ($func !== null) self::ensure_func($func);
}
function getClass(): string {
return func::class;
}

View File

@ -11,14 +11,6 @@ class tint extends _tformatable {
const ALIASES = ["integer"];
static function ensure_int(&$int): void {
if (!is_int($int)) $int = intval($int);
}
static function ensure_nint(&$int): void {
if ($int !== null) self::ensure_int($int);
}
//const INT_PATTERN = '/^[-+]?[0-9]+(?:\.[0-9]*)?$/';
function getClass(): string {

View File

@ -8,16 +8,6 @@ use nulib\schema\Schema;
class tkey extends _tunion {
const NAME = "key";
static function ensure_key(&$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);
}
function getClass(): string {
return "string|int";
}

View File

@ -1,6 +1,7 @@
<?php
namespace nulib\schema\types;
use nulib\php\types\vkey;
use nulib\schema\_scalar\ScalarSchema;
use nulib\schema\Result;
use nulib\schema\Schema;
@ -8,21 +9,6 @@ use nulib\schema\Schema;
class tpkey extends _tunion {
const NAME = "pkey";
static function ensure_pkey(&$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) {
tkey::ensure_key($key);
}; unset($key);
}
}
static function ensure_npkey(&$pkey): void {
if ($pkey !== null) self::ensure_pkey($pkey);
}
function getClass(): string {
return "string|int|array";
}

View File

@ -9,16 +9,6 @@ use nulib\str;
class trawstring extends _tstring {
const NAME = "rawstring";
static function ensure_string(&$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);
}
function getClass(): string {
return "string";
}