From 4f0d6d40dcbe90eb717cb9fc6c0c366f2825ce54 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 22 Oct 2025 13:55:13 +0400 Subject: [PATCH] function isa() pour les vclasses --- php/src/php/types/varray.php | 5 +++++ php/src/php/types/vbool.php | 6 ++++++ php/src/php/types/vcontent.php | 5 +++++ php/src/php/types/vfloat.php | 5 +++++ php/src/php/types/vfunc.php | 5 +++++ php/src/php/types/vint.php | 5 +++++ php/src/php/types/vkey.php | 5 +++++ php/src/php/types/vpkey.php | 5 +++++ php/src/php/types/vrawstring.php | 5 +++++ 9 files changed, 46 insertions(+) diff --git a/php/src/php/types/varray.php b/php/src/php/types/varray.php index 3a6b416..210590f 100644 --- a/php/src/php/types/varray.php +++ b/php/src/php/types/varray.php @@ -4,6 +4,11 @@ namespace nulib\php\types; use nulib\cl; class varray { + static function isa($value, bool $strict=false) : bool { + if ($strict) return is_array($value); + else return is_scalar($value); + } + static function ensure(&$array): void { if (!is_array($array)) $array = cl::with($array); } diff --git a/php/src/php/types/vbool.php b/php/src/php/types/vbool.php index ab9a1da..3787fef 100644 --- a/php/src/php/types/vbool.php +++ b/php/src/php/types/vbool.php @@ -30,6 +30,12 @@ class vbool { return in_array($value, self::NO_VALUES, true); } + static function isa($value, bool $strict=false) : bool { + if ($strict) return is_bool($value); + elseif (is_string($value)) return self::is_yes($value) || self::is_no($value); + else return is_scalar($value); + } + static function ensure(&$bool): void { if (is_string($bool)) { if (self::is_yes($bool)) $bool = true; diff --git a/php/src/php/types/vcontent.php b/php/src/php/types/vcontent.php index cfb5b77..8596979 100644 --- a/php/src/php/types/vcontent.php +++ b/php/src/php/types/vcontent.php @@ -2,6 +2,11 @@ namespace nulib\php\types; class vcontent { + static function isa($value, bool $strict=false) : bool { + if ($strict) return is_array($value) || is_string($value); + else return is_array($value) || is_scalar($value); + } + static function ensure(&$content): void { if ($content === null || $content === false) $content = []; elseif (!is_string($content) && !is_array($content)) $content = strval($content); diff --git a/php/src/php/types/vfloat.php b/php/src/php/types/vfloat.php index 3233b73..13d5c7c 100644 --- a/php/src/php/types/vfloat.php +++ b/php/src/php/types/vfloat.php @@ -2,6 +2,11 @@ namespace nulib\php\types; class vfloat { + static function isa($value, bool $strict=false) : bool { + if ($strict) return is_float($value); + else return is_numeric($value); + } + static function ensure(&$float): void { if (!is_float($float)) $float = floatval($float); } diff --git a/php/src/php/types/vfunc.php b/php/src/php/types/vfunc.php index ec690bf..bcde965 100644 --- a/php/src/php/types/vfunc.php +++ b/php/src/php/types/vfunc.php @@ -4,6 +4,11 @@ namespace nulib\php\types; use nulib\php\func; class vfunc { + static function isa($value, bool $strict=false) : bool { + if ($strict) return $value instanceof func; + else return func::is_callable($value); + } + static function ensure(&$func): void { $func = func::ensure($func); } diff --git a/php/src/php/types/vint.php b/php/src/php/types/vint.php index 6d5ed4c..74e8b6a 100644 --- a/php/src/php/types/vint.php +++ b/php/src/php/types/vint.php @@ -2,6 +2,11 @@ namespace nulib\php\types; class vint { + static function isa($value, bool $strict=false) : bool { + if ($strict) return is_int($value); + else return is_numeric($value); + } + static function ensure(&$int): void { if (!is_int($int)) $int = intval($int); } diff --git a/php/src/php/types/vkey.php b/php/src/php/types/vkey.php index 87a8402..0769bfb 100644 --- a/php/src/php/types/vkey.php +++ b/php/src/php/types/vkey.php @@ -2,6 +2,11 @@ namespace nulib\php\types; class vkey { + static function isa($value, bool $strict=false) : bool { + if ($strict) return is_int($value) || is_string($value); + else return is_scalar($value); + } + static function ensure(&$key): void { if ($key === null) $key = ""; elseif ($key === false) $key = 0; diff --git a/php/src/php/types/vpkey.php b/php/src/php/types/vpkey.php index 318c7bb..27c8ef6 100644 --- a/php/src/php/types/vpkey.php +++ b/php/src/php/types/vpkey.php @@ -2,6 +2,11 @@ namespace nulib\php\types; class vpkey { + static function isa($value, bool $strict=false) : bool { + if ($strict) return is_array($value) || is_int($value) || is_string($value); + else return is_array($value) || is_scalar($value); + } + static function ensure(&$pkey): void { if ($pkey === null) $pkey = ""; elseif ($pkey === false) $pkey = 0; diff --git a/php/src/php/types/vrawstring.php b/php/src/php/types/vrawstring.php index 463aba3..28f1ff8 100644 --- a/php/src/php/types/vrawstring.php +++ b/php/src/php/types/vrawstring.php @@ -9,6 +9,11 @@ class vrawstring { /** @var bool faut-il normaliser les caractères fin de ligne */ const NORM_NL = false; + static function isa($value, bool $strict=false) : bool { + if ($strict) return is_string($value); + else return is_scalar($value); + } + static function ensure(&$string): void { if (!is_string($string)) $string = strval($string); if (static::TRIM) $string = trim($string);