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-25 10:04:24 +04:00
|
|
|
use nur\sery\schema\values\IValue;
|
2023-11-24 22:36:33 +04:00
|
|
|
|
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_string(&$string): void {
|
|
|
|
if (!is_string($string)) $string = strval($string);
|
|
|
|
}
|
2023-11-25 10:04:24 +04:00
|
|
|
protected static function ensure_nstring(&$string): void {
|
|
|
|
if ($string !== null) self::ensure_string($string);
|
2023-11-24 22:36:33 +04:00
|
|
|
}
|
|
|
|
protected static function ensure_bool(&$bool): void {
|
|
|
|
if (!is_bool($bool)) $bool = boolval($bool);
|
|
|
|
}
|
2023-11-25 10:04:24 +04:00
|
|
|
protected static function ensure_nbool(&$bool): void {
|
|
|
|
if ($bool !== null) self::ensure_bool($bool);
|
2023-11-24 22:36:33 +04:00
|
|
|
}
|
2023-11-25 10:04:24 +04:00
|
|
|
protected static function ensure_callable(&$callable): void {
|
|
|
|
if (!is_callable($callable)) throw SchemaException::invalid_callable($callable);
|
|
|
|
}
|
|
|
|
protected static function ensure_ncallable(&$callable): void {
|
|
|
|
if ($callable !== null) self::ensure_callable($callable);
|
2023-11-24 22:36:33 +04:00
|
|
|
}
|
|
|
|
protected static function ensure_array(&$array): void {
|
|
|
|
if (!is_array($array)) $array = cl::with($array);
|
|
|
|
}
|
2023-11-25 10:04:24 +04:00
|
|
|
protected static function ensure_narray(&$array): void {
|
|
|
|
if ($array !== null) self::ensure_array($array);
|
2023-11-24 22:36:33 +04:00
|
|
|
}
|
|
|
|
protected static function ensure_key(&$key): void {
|
|
|
|
if (!is_string($key) && !is_int($key)) $key = strval($key);
|
|
|
|
}
|
2023-11-25 10:04:24 +04:00
|
|
|
protected static function ensure_nkey(&$key): void {
|
|
|
|
if ($key !== null) self::ensure_key($key);
|
2023-11-24 22:36:33 +04:00
|
|
|
}
|
2023-11-25 10:04:24 +04:00
|
|
|
protected static function ensure_pkey(&$pkey): void {
|
|
|
|
if (!is_string($pkey) && !is_int($pkey) && !is_array($pkey)) $pkey = strval($pkey);
|
|
|
|
if (is_array($pkey)) {
|
|
|
|
foreach ($pkey as &$key) {
|
|
|
|
self::ensure_key($key);
|
|
|
|
}; unset($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protected static function ensure_npkey(&$pkey): void {
|
|
|
|
if ($pkey !== null) self::ensure_pkey($pkey);
|
|
|
|
}
|
|
|
|
protected static function ensure_content(&$content): void {
|
|
|
|
if (!is_string($content) && !is_array($content)) $content = strval($content);
|
|
|
|
}
|
|
|
|
protected static function ensure_ncontent(&$content): void {
|
|
|
|
if ($content !== null) self::ensure_content($content);
|
2023-11-24 22:36:33 +04:00
|
|
|
}
|
|
|
|
|
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 */
|
2023-11-25 10:04:24 +04:00
|
|
|
function isAssoc(?AssocSchema &$assoc=null): bool { return false; }
|
2023-11-24 22:36:33 +04:00
|
|
|
/** retourner true si le schéma est de nature liste */
|
2023-11-25 10:04:24 +04:00
|
|
|
function isList(?ListSchema &$list=null): bool { return false; }
|
2023-11-24 22:36:33 +04:00
|
|
|
/** retourner true si le schéma est de nature scalaire */
|
2023-11-25 10:04:24 +04:00
|
|
|
function isScalar(?ScalarSchema &$scalar=null): bool { return false; }
|
|
|
|
|
|
|
|
abstract function getValue(&$value, $key=null): IValue;
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|