2023-11-09 10:03:35 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\schema;
|
|
|
|
|
2023-11-24 22:36:33 +04:00
|
|
|
use nulib\cl;
|
|
|
|
|
2023-11-24 16:50:05 +04:00
|
|
|
abstract class Schema {
|
2023-11-09 10:03:35 +04:00
|
|
|
/**
|
2023-11-24 16:50:05 +04:00
|
|
|
* créer si besoin une nouvelle instance à partir d'une définition de schéma
|
2023-11-09 10:03:35 +04:00
|
|
|
*/
|
2023-11-24 16:50:05 +04:00
|
|
|
static function new(&$md, $definition): self {
|
|
|
|
if ($md === null) {
|
|
|
|
if (AssocSchema::isa_definition($definition)) {
|
|
|
|
$md = new AssocSchema($definition);
|
|
|
|
} elseif (ListSchema::isa_definition($definition)) {
|
|
|
|
$md = new ListSchema($definition);
|
|
|
|
} elseif (ScalarSchema::isa_definition($definition)) {
|
|
|
|
$md = new ScalarSchema($definition);
|
|
|
|
} else {
|
|
|
|
throw SchemaException::invalid_schema();
|
|
|
|
}
|
|
|
|
}
|
2023-11-09 10:03:35 +04:00
|
|
|
return $md;
|
|
|
|
}
|
|
|
|
|
2023-11-24 22:36:33 +04:00
|
|
|
protected static function ensure_nstring(&$string): void {
|
|
|
|
if ($string !== null && !is_string($string)) $string = strval($string);
|
|
|
|
}
|
|
|
|
protected static function ensure_string(&$string): void {
|
|
|
|
if (!is_string($string)) $string = strval($string);
|
|
|
|
}
|
|
|
|
protected static function ensure_nbool(&$bool): void {
|
|
|
|
if ($bool !== null && !is_bool($bool)) $bool = boolval($bool);
|
|
|
|
}
|
|
|
|
protected static function ensure_bool(&$bool): void {
|
|
|
|
if (!is_bool($bool)) $bool = boolval($bool);
|
|
|
|
}
|
|
|
|
protected static function ensure_ncallable(&$callable): void {
|
|
|
|
if ($callable !== null && !is_callable($callable)) {
|
|
|
|
throw SchemaException::invalid_callable($callable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protected static function ensure_narray(&$array): void {
|
|
|
|
if ($array !== null && !is_array($array)) $array = cl::with($array);
|
|
|
|
}
|
|
|
|
protected static function ensure_array(&$array): void {
|
|
|
|
if (!is_array($array)) $array = cl::with($array);
|
|
|
|
}
|
|
|
|
protected static function ensure_nkey(&$key): void {
|
|
|
|
if ($key !== null && !is_string($key) && !is_int($key)) $key = strval($key);
|
|
|
|
}
|
|
|
|
protected static function ensure_key(&$key): void {
|
|
|
|
if (!is_string($key) && !is_int($key)) $key = strval($key);
|
|
|
|
}
|
|
|
|
protected static function ensure_ncontent(&$key): void {
|
|
|
|
if ($key !== null && !is_string($key) && !is_array($key)) $key = strval($key);
|
|
|
|
}
|
|
|
|
protected static function ensure_content(&$key): void {
|
|
|
|
if (!is_string($key) && !is_array($key)) $key = strval($key);
|
|
|
|
}
|
|
|
|
|
2023-11-09 10:03:35 +04:00
|
|
|
/**
|
2023-11-24 16:50:05 +04:00
|
|
|
* @var array définition du schéma, à redéfinir le cas échéant dans une classe
|
|
|
|
* dérivée
|
2023-11-09 10:03:35 +04:00
|
|
|
*/
|
2023-11-24 16:50:05 +04:00
|
|
|
const SCHEMA = null;
|
2023-11-09 10:03:35 +04:00
|
|
|
|
2023-11-24 16:50:05 +04:00
|
|
|
/** @var array */
|
|
|
|
protected $definition;
|
2023-11-24 22:36:33 +04:00
|
|
|
|
|
|
|
/** retourner true si le schéma est de nature tableau associatif */
|
|
|
|
function isAssoc(): bool { return false; }
|
|
|
|
/** retourner true si le schéma est de nature liste */
|
|
|
|
function isList(): bool { return false; }
|
|
|
|
/** retourner true si le schéma est de nature scalaire */
|
|
|
|
function isScalar(): bool { return false; }
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|