2023-11-09 10:03:35 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\schema;
|
|
|
|
|
2023-11-27 18:57:07 +04:00
|
|
|
use ArrayAccess;
|
|
|
|
use LogicException;
|
2023-11-24 22:36:33 +04:00
|
|
|
use nulib\cl;
|
2023-11-28 00:20:42 +04:00
|
|
|
use nur\sery\schema\_assoc\AssocSchema;
|
|
|
|
use nur\sery\schema\_list\ListSchema;
|
|
|
|
use nur\sery\schema\_scalar\ScalarSchema;
|
2023-11-24 22:36:33 +04:00
|
|
|
|
2023-11-27 18:57:07 +04:00
|
|
|
abstract class Schema implements ArrayAccess {
|
2023-11-09 10:03:35 +04:00
|
|
|
/**
|
2023-11-28 00:20:42 +04:00
|
|
|
* créer si besoin une nouvelle instance de {@link Schema} à partir d'une
|
|
|
|
* définition de schéma
|
2023-11-09 10:03:35 +04:00
|
|
|
*/
|
2023-11-28 08:20:33 +04:00
|
|
|
static function ns(?Schema &$schema, $definition, $definitionKey=null): self {
|
2023-11-27 22:39:35 +04:00
|
|
|
if ($schema === null) {
|
2023-11-24 16:50:05 +04:00
|
|
|
if (AssocSchema::isa_definition($definition)) {
|
2023-11-28 08:20:33 +04:00
|
|
|
$schema = new AssocSchema($definition, $definitionKey);
|
2023-11-24 16:50:05 +04:00
|
|
|
} elseif (ListSchema::isa_definition($definition)) {
|
2023-11-28 08:20:33 +04:00
|
|
|
$schema = new ListSchema($definition, $definitionKey);
|
2023-11-24 16:50:05 +04:00
|
|
|
} elseif (ScalarSchema::isa_definition($definition)) {
|
2023-11-28 08:20:33 +04:00
|
|
|
$schema = new ScalarSchema($definition, $definitionKey);
|
2023-11-24 16:50:05 +04:00
|
|
|
} else {
|
|
|
|
throw SchemaException::invalid_schema();
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 22:39:35 +04:00
|
|
|
return $schema;
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|
|
|
|
|
2023-11-28 00:20:42 +04:00
|
|
|
/**
|
|
|
|
* Créer si besoin une nouvelle instance de {@link Value} qui référence la
|
|
|
|
* variable $dest
|
|
|
|
*/
|
|
|
|
static function nv(?Value &$value=null, &$dest=null, $key=null, ?Schema &$schema=null, $definition=null): Value {
|
|
|
|
if ($definition === null) {
|
|
|
|
# bien que techniquement, $definition peut être null (il s'agit alors du
|
|
|
|
# schéma d'un scalaire quelconque), on ne l'autorise pas ici
|
|
|
|
throw SchemaException::invalid_schema("definition is required");
|
|
|
|
}
|
|
|
|
return self::ns($schema, $definition)->newValue($value, $dest, $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 */
|
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; }
|
|
|
|
|
2023-11-28 00:20:42 +04:00
|
|
|
abstract function newValue(?Value &$value=null, &$dest=null, $key=null): Value;
|
2023-11-27 18:57:07 +04:00
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
# key & properties
|
|
|
|
|
|
|
|
function offsetExists($offset): bool {
|
|
|
|
return array_key_exists($offset, $this->definition);
|
|
|
|
}
|
|
|
|
function offsetGet($offset) {
|
|
|
|
if (!array_key_exists($offset, $this->definition)) return null;
|
|
|
|
else return $this->definition[$offset];
|
|
|
|
}
|
|
|
|
function offsetSet($offset, $value): void {
|
|
|
|
throw new LogicException("read-only");
|
|
|
|
}
|
|
|
|
function offsetUnset($offset): void {
|
|
|
|
throw new LogicException("read-only");
|
|
|
|
}
|
|
|
|
|
|
|
|
const _PROPERTY_PKEYS = [];
|
|
|
|
function __get($name) {
|
|
|
|
$pkey = cl::get(static::_PROPERTY_PKEYS, $name, $name);
|
|
|
|
return cl::pget($this->definition, $pkey);
|
|
|
|
}
|
2023-11-09 10:03:35 +04:00
|
|
|
}
|