2023-11-09 10:03:35 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\schema;
|
|
|
|
|
2023-11-27 18:57:07 +04:00
|
|
|
use ArrayAccess;
|
|
|
|
use BadMethodCallException;
|
|
|
|
use LogicException;
|
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-27 18:57:07 +04:00
|
|
|
abstract class Schema implements ArrayAccess {
|
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 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-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
|
|
|
}
|