nur-sery/src/schema/Schema.php

75 lines
2.3 KiB
PHP
Raw Normal View History

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-27 22:39:35 +04:00
use nur\sery\schema\schemas\AssocSchema;
use nur\sery\schema\schemas\ListSchema;
use nur\sery\schema\schemas\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-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-27 22:39:35 +04:00
static function ns(?Schema &$schema, $definition): self {
if ($schema === null) {
2023-11-24 16:50:05 +04:00
if (AssocSchema::isa_definition($definition)) {
2023-11-27 22:39:35 +04:00
$schema = new AssocSchema($definition);
2023-11-24 16:50:05 +04:00
} elseif (ListSchema::isa_definition($definition)) {
2023-11-27 22:39:35 +04:00
$schema = new ListSchema($definition);
2023-11-24 16:50:05 +04:00
} elseif (ScalarSchema::isa_definition($definition)) {
2023-11-27 22:39:35 +04:00
$schema = new ScalarSchema($definition);
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-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-27 22:39:35 +04:00
/**
* Créer si besoin une nouvelle instance de {@link Value} qui référence la
* variable $dest
*/
abstract function nv(?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
}