suite vschema
This commit is contained in:
parent
ce21bbc451
commit
81f666f90e
@ -31,8 +31,13 @@ class Time {
|
||||
else return new static($time);
|
||||
}
|
||||
|
||||
static function ensure(&$time): void {
|
||||
$time = static::withn($time);
|
||||
static function isa_time($time): bool {
|
||||
if ($time === null) return false;
|
||||
if ($time instanceof Time) return true;
|
||||
if (is_int($time)) return true;
|
||||
if (is_string($time)) return preg_match(self::PATTERN, $time);
|
||||
#XXX supporter tous les formats
|
||||
return false;
|
||||
}
|
||||
|
||||
static function seconds_now(): int {
|
||||
|
||||
@ -4,6 +4,11 @@ namespace nulib\php\types;
|
||||
use nulib\php\time\Date;
|
||||
|
||||
class vdate {
|
||||
static function isa($value, bool $strict=false) : bool {
|
||||
if ($strict) return $value instanceof Date;
|
||||
else return Date::isa_date($value);
|
||||
}
|
||||
|
||||
static function ensure(&$date): void {
|
||||
$date = Date::with($date);
|
||||
}
|
||||
|
||||
@ -4,6 +4,11 @@ namespace nulib\php\types;
|
||||
use nulib\php\time\DateTime;
|
||||
|
||||
class vdatetime {
|
||||
static function isa($value, bool $strict=false) : bool {
|
||||
if ($strict) return $value instanceof DateTime;
|
||||
else return DateTime::isa_datetime($value);
|
||||
}
|
||||
|
||||
static function ensure(&$datetime): void {
|
||||
$datetime = DateTime::with($datetime);
|
||||
}
|
||||
|
||||
@ -2,6 +2,10 @@
|
||||
namespace nulib\php\types;
|
||||
|
||||
class vmixed {
|
||||
static function isa($value, bool $strict=false) : bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
static function ensure(&$mixed): void {
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,10 @@
|
||||
namespace nulib\php\types;
|
||||
|
||||
class vraw {
|
||||
static function isa($value, bool $strict=false) : bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
static function ensure(&$raw): void {
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
<?php
|
||||
namespace nulib\php\types;
|
||||
|
||||
use nulib\exceptions;
|
||||
use nulib\str;
|
||||
|
||||
/**
|
||||
* Class vschema: gestionsimplifiée de schémas associatifs
|
||||
*
|
||||
@ -8,7 +11,7 @@ namespace nulib\php\types;
|
||||
*/
|
||||
class vschema {
|
||||
/** @var array types valides */
|
||||
const TYPES = [
|
||||
const VCLASSES = [
|
||||
"rawstring" => vrawstring::class,
|
||||
"string" => vstring::class,
|
||||
"text" => vtext::class,
|
||||
@ -27,9 +30,62 @@ class vschema {
|
||||
"time" => vtime::class,
|
||||
];
|
||||
|
||||
/** indiquer si $value est conforme au schéma */
|
||||
static function check($value, array $schema): bool {
|
||||
private static function get_types($schema): array {
|
||||
if (is_array($schema)) {
|
||||
$types = $schema["type"] ?? $schema[0] ?? null;
|
||||
} elseif (is_string($schema)) {
|
||||
$types = $schema;
|
||||
} else {
|
||||
throw exceptions::invalid_value($schema, "schema");
|
||||
}
|
||||
if (is_string($types)) {
|
||||
$types = explode(",", $types);
|
||||
} elseif (!is_array($types)) {
|
||||
throw exceptions::invalid_value($types, "types");
|
||||
}
|
||||
return $types;
|
||||
}
|
||||
|
||||
private static function get_vclass(array $types, ?bool &$nullable): ?string {
|
||||
foreach ($types as $type) {
|
||||
$vclass = self::VCLASSES[$type] ?? null;
|
||||
if ($vclass !== null) {
|
||||
$nullable = str::del_prefix($type, "?");
|
||||
return $vclass;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** indiquer si $value est conforme au schéma */
|
||||
static function check_scalar($value, $schema, bool $strict=false): bool {
|
||||
$types = self::get_types($schema);
|
||||
$vclass = self::get_vclass($types, $nullable);
|
||||
# ce doit être un type supporté
|
||||
if ($vclass === null) return false;
|
||||
if ($value === null) return $nullable;
|
||||
return $vclass::isa($value, $strict);
|
||||
}
|
||||
|
||||
static function ensure_scalar(&$value, $schema): bool {
|
||||
$types = self::get_types($schema);
|
||||
$vclass = self::get_vclass($types, $nullable);
|
||||
# ce doit être un type supporté
|
||||
if ($vclass === null) return false;
|
||||
if ($nullable) $value = $vclass::withn($value);
|
||||
else $value = $vclass::with($value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** indiquer si $value est conforme au schéma */
|
||||
static function check_assoc(?array $array, array $schema): bool {
|
||||
foreach ($schema as $key => $kschema) {
|
||||
$required = vbool::with($kschema["required"] ?? false);
|
||||
$exists = array_key_exists($key, $array);
|
||||
if (!$exists && $required) return false;
|
||||
if (!self::check_scalar($array[$key], $kschema)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,7 +93,7 @@ class vschema {
|
||||
* - les clés ne sont pas créées si elles n'existent pas
|
||||
*
|
||||
*/
|
||||
static function ensure(&$value, array $schema): void {
|
||||
static function ensure_assoc(&$value, array $schema): void {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,15 @@
|
||||
<?php
|
||||
namespace nulib\php\types;
|
||||
|
||||
use nulib\php\time\DateTime;
|
||||
use nulib\php\time\Time;
|
||||
|
||||
class vtime {
|
||||
static function isa($value, bool $strict=false) : bool {
|
||||
if ($strict) return $value instanceof Time;
|
||||
else return Time::isa_time($value);
|
||||
}
|
||||
|
||||
static function ensure(&$time): void {
|
||||
$time = Time::with($time);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user