nur-sery/src/schema/Schema.php

77 lines
2.6 KiB
PHP

<?php
namespace nur\sery\schema;
use nulib\cl;
abstract class Schema {
/**
* créer si besoin une nouvelle instance à partir d'une définition de schéma
*/
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();
}
}
return $md;
}
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);
}
/**
* @var array définition du schéma, à redéfinir le cas échéant dans une classe
* dérivée
*/
const SCHEMA = null;
/** @var array */
protected $definition;
/** 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; }
}