déplacer les fichiers dans nulib-base

This commit is contained in:
Jephté Clain 2025-10-22 17:44:07 +04:00
parent 2e4fe05467
commit 2aeacade5c
2 changed files with 1 additions and 100 deletions

View File

@ -1,99 +0,0 @@
<?php
namespace nulib\php\types;
use nulib\exceptions;
use nulib\str;
/**
* Class vschema: gestionsimplifiée de schémas associatifs
*
* seuls les types simples sont reconnus
*/
class vschema {
/** @var array types valides */
const VCLASSES = [
"rawstring" => vrawstring::class,
"string" => vstring::class,
"text" => vtext::class,
"bool" => vbool::class,
"int" => vint::class,
"float" => vfloat::class,
"array" => varray::class,
"func" => vfunc::class,
"raw" => vraw::class,
"mixed" => vmixed::class,
"key" => vkey::class,
"pkey" => vpkey::class,
"content" => vcontent::class,
"datetime" => vdatetime::class,
"date" => vdate::class,
"time" => vtime::class,
];
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;
}
/**
* s'assurer que $value est conforme au schéma
* - les clés ne sont pas créées si elles n'existent pas
*
*/
static function ensure_assoc(&$value, array $schema): void {
}
}

View File

@ -42,7 +42,7 @@ use nulib\schema\types\trawstring;
*/
abstract class Schema implements ArrayAccess {
/**
* créer le cas échéant une nouvelle instance de {@link Schema} à partir d'une
* créer le cas échéant une nouvelle instance de cette classe à partir d'une
* définition de schéma
*
* - si $schema est une instance de schéma, la retourner