2023-11-09 10:03:35 +04:00
|
|
|
<?php
|
2024-05-23 08:15:28 +04:00
|
|
|
namespace nur\sery\wip\schema;
|
2023-11-09 10:03:35 +04:00
|
|
|
|
2023-11-27 18:57:07 +04:00
|
|
|
use ArrayAccess;
|
2024-04-05 08:31:49 +04:00
|
|
|
use nur\sery\AccessException;
|
|
|
|
use nur\sery\cl;
|
2024-05-23 08:15:28 +04:00
|
|
|
use nur\sery\wip\schema\_assoc\AssocSchema;
|
|
|
|
use nur\sery\wip\schema\_list\ListSchema;
|
|
|
|
use nur\sery\wip\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-12-28 19:33:13 +04:00
|
|
|
*
|
|
|
|
* - si $schema est une instance de schéma, la retourner
|
|
|
|
* - si $schema est un array, c'est une définition, et elle est remplacée par
|
|
|
|
* l'instance de Schema nouvelle créée
|
|
|
|
* - sinon, prendre $definition comme définition
|
2023-11-09 10:03:35 +04:00
|
|
|
*/
|
2023-12-28 19:33:13 +04:00
|
|
|
static function ns(&$schema, $definition=null, $definitionKey=null): self {
|
|
|
|
if (is_array($schema)) {
|
|
|
|
$definition = $schema;
|
|
|
|
$schema = null;
|
|
|
|
}
|
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
|
2023-12-28 19:33:13 +04:00
|
|
|
* variable $dest (si $destKey===null) ou $dest[$destKey] si $destKey n'est
|
|
|
|
* pas null
|
2023-11-28 00:20:42 +04:00
|
|
|
*/
|
2023-12-30 14:52:30 +04:00
|
|
|
static function nv(?Value &$destv=null, &$dest=null, $destKey=null, &$schema=null, $definition=null): Value {
|
2023-11-28 00:20:42 +04:00
|
|
|
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");
|
|
|
|
}
|
2023-12-30 14:52:30 +04:00
|
|
|
return self::ns($schema, $definition)->newValue($destv, $dest, $destKey);
|
2023-11-28 00:20:42 +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; }
|
|
|
|
|
2023-12-30 14:52:30 +04:00
|
|
|
abstract function newValue(?Value &$destv=null, &$dest=null, $destKey=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 {
|
2023-12-28 19:33:13 +04:00
|
|
|
throw AccessException::read_only(null, $offset);
|
2023-11-27 18:57:07 +04:00
|
|
|
}
|
|
|
|
function offsetUnset($offset): void {
|
2023-12-28 19:33:13 +04:00
|
|
|
throw AccessException::read_only(null, $offset);
|
2023-11-27 18:57:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|