function isa() pour les vclasses

This commit is contained in:
Jephté Clain 2025-10-22 13:55:13 +04:00
parent 9d6e7f3955
commit 4f0d6d40dc
9 changed files with 46 additions and 0 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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;

View File

@ -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);