modifs.mineures sans commentaires
This commit is contained in:
parent
f39589af1b
commit
ea1b774d0f
|
@ -169,7 +169,7 @@ const VALUE_SCHEMA = [
|
|||
"" => "nature du schéma: scalar",
|
||||
"name" => "identifiant de la valeur",
|
||||
# spécifique à VALUE_SCHEMA
|
||||
"key" => "chemin de clé de la valeur dans le tableau associatif",
|
||||
"pkey" => "chemin de clé de la valeur dans le tableau associatif",
|
||||
"header" => "nom de l'en-tête s'il faut présenter cette donnée dans un tableau",
|
||||
"composite" => "ce champ fait-il partie d'une valeur composite?",
|
||||
];
|
||||
|
@ -186,7 +186,7 @@ const VALUE_SCHEMA = [
|
|||
"desc" => "description de la valeur",
|
||||
"name" => "identifiant de la valeur",
|
||||
# spécifique à VALUE_SCHEMA
|
||||
"key" => "chemin de clé de la valeur dans le tableau associatif",
|
||||
"pkey" => "chemin de clé de la valeur dans le tableau associatif",
|
||||
],
|
||||
];
|
||||
~~~
|
||||
|
@ -202,7 +202,7 @@ const VALUE_SCHEMA = [
|
|||
"desc" => "description de la valeur",
|
||||
"name" => "identifiant de la valeur",
|
||||
# spécifique à VALUE_SCHEMA
|
||||
"key" => "chemin de clé de la valeur dans le tableau associatif",
|
||||
"pkey" => "chemin de clé de la valeur dans le tableau associatif",
|
||||
],
|
||||
];
|
||||
~~~
|
||||
|
|
|
@ -5,6 +5,12 @@ use nulib\cl;
|
|||
use nur\sery\schema\input\Input;
|
||||
use nur\sery\schema\ref\ref_schema;
|
||||
use nur\sery\schema\ref\ref_types;
|
||||
use nur\sery\schema\types\tarray;
|
||||
use nur\sery\schema\types\tbool;
|
||||
use nur\sery\schema\types\tcallable;
|
||||
use nur\sery\schema\types\tcontent;
|
||||
use nur\sery\schema\types\tpkey;
|
||||
use nur\sery\schema\types\tstring;
|
||||
use nur\sery\schema\values\IValue;
|
||||
use nur\sery\schema\values\ScalarValue;
|
||||
|
||||
|
@ -26,6 +32,9 @@ use nur\sery\schema\values\ScalarValue;
|
|||
* @property-read mixed $format
|
||||
* @property-read array $nature
|
||||
* @property-read string|int|null $name
|
||||
* @property-read string|array|null $pkey
|
||||
* @property-read string|null $header
|
||||
* @property-read bool|null $composite
|
||||
*/
|
||||
class ScalarSchema extends Schema {
|
||||
/** @var array meta-schema d'un schéma de nature scalaire */
|
||||
|
@ -34,7 +43,7 @@ class ScalarSchema extends Schema {
|
|||
"type", "default", "title", "required", "nullable", "desc",
|
||||
"analyzer_func", "extractor_func", "parser_func", "normalizer_func", "messages",
|
||||
"formatter_func", "format",
|
||||
"", "name", "key", "header", "composite",
|
||||
"", "name", "pkey", "header", "composite",
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -68,18 +77,18 @@ class ScalarSchema extends Schema {
|
|||
return $haveIndex0 && $count > 1;
|
||||
}
|
||||
|
||||
static function normalize($definition): array {
|
||||
static function normalize($definition, $definitionKey=null): array {
|
||||
if (!is_array($definition)) $definition = [$definition];
|
||||
# s'assurer que toutes les clés existent avec leur valeur par défaut
|
||||
$index = 0;
|
||||
foreach (self::METASCHEMA_KEYS as $key) {
|
||||
if (!array_key_exists($key, $definition)) {
|
||||
foreach (self::METASCHEMA_KEYS as $pkey) {
|
||||
if (!array_key_exists($pkey, $definition)) {
|
||||
if (array_key_exists($index, $definition)) {
|
||||
$definition[$key] = $definition[$index];
|
||||
$definition[$pkey] = $definition[$index];
|
||||
unset($definition[$index]);
|
||||
$index++;
|
||||
} else {
|
||||
$definition[$key] = self::METASCHEMA[$key][1];
|
||||
$definition[$pkey] = self::METASCHEMA[$pkey][1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,10 +96,10 @@ class ScalarSchema extends Schema {
|
|||
if (cl::have_num_keys($definition)) {
|
||||
$keys = array_keys($definition);
|
||||
$index = 0;
|
||||
foreach ($keys as $key) {
|
||||
if (!is_int($key)) continue;
|
||||
$definition[$index] = $definition[$key];
|
||||
unset($definition[$key]);
|
||||
foreach ($keys as $pkey) {
|
||||
if (!is_int($pkey)) continue;
|
||||
$definition[$index] = $definition[$pkey];
|
||||
unset($definition[$pkey]);
|
||||
$index++;
|
||||
}
|
||||
}
|
||||
|
@ -127,35 +136,36 @@ class ScalarSchema extends Schema {
|
|||
$definition["nullable"] = $nullable;
|
||||
# nature
|
||||
$nature = $definition[""];
|
||||
self::ensure_array($nature);
|
||||
tarray::ensure_array($nature);
|
||||
if (!array_key_exists(0, $nature) || $nature[0] !== "scalar") {
|
||||
throw SchemaException::invalid_schema("expected scalar nature");
|
||||
}
|
||||
$definition[""] = $nature;
|
||||
# name, key, header
|
||||
# name, pkey, header
|
||||
$name = $definition["name"];
|
||||
$key = $definition["key"];
|
||||
$pkey = $definition["pkey"];
|
||||
$header = $definition["header"];
|
||||
self::ensure_nstring($name);
|
||||
self::ensure_npkey($key);
|
||||
self::ensure_nstring($header);
|
||||
if ($key === null) $key = $name;
|
||||
if ($name === null) $name = $definitionKey;
|
||||
tstring::ensure_nstring($name);
|
||||
tpkey::ensure_npkey($pkey);
|
||||
tstring::ensure_nstring($header);
|
||||
if ($pkey === null) $pkey = $name;
|
||||
if ($header === null) $header = $name;
|
||||
$definition["name"] = $name;
|
||||
$definition["key"] = $key;
|
||||
$definition["pkey"] = $pkey;
|
||||
$definition["header"] = $header;
|
||||
# autres éléments
|
||||
self::ensure_nstring($definition["title"]);
|
||||
self::ensure_bool($definition["required"]);
|
||||
self::ensure_bool($definition["nullable"]);
|
||||
self::ensure_ncontent($definition["desc"]);
|
||||
self::ensure_ncallable($definition["analyzer_func"]);
|
||||
self::ensure_ncallable($definition["extractor_func"]);
|
||||
self::ensure_ncallable($definition["parser_func"]);
|
||||
self::ensure_ncallable($definition["normalizer_func"]);
|
||||
self::ensure_narray($definition["messages"]);
|
||||
self::ensure_ncallable($definition["formatter_func"]);
|
||||
self::ensure_nbool($definition["composite"]);
|
||||
tstring::ensure_nstring($definition["title"]);
|
||||
tbool::ensure_bool($definition["required"]);
|
||||
tbool::ensure_bool($definition["nullable"]);
|
||||
tcontent::ensure_ncontent($definition["desc"]);
|
||||
tcallable::ensure_ncallable($definition["analyzer_func"]);
|
||||
tcallable::ensure_ncallable($definition["extractor_func"]);
|
||||
tcallable::ensure_ncallable($definition["parser_func"]);
|
||||
tcallable::ensure_ncallable($definition["normalizer_func"]);
|
||||
tarray::ensure_narray($definition["messages"]);
|
||||
tcallable::ensure_ncallable($definition["formatter_func"]);
|
||||
tbool::ensure_nbool($definition["composite"]);
|
||||
return $definition;
|
||||
}
|
||||
|
||||
|
@ -175,4 +185,16 @@ class ScalarSchema extends Schema {
|
|||
else $input = new Input($value);
|
||||
return new ScalarValue($input, $this, $key);
|
||||
}
|
||||
|
||||
#############################################################################
|
||||
# key & properties
|
||||
|
||||
const _PROPERTY_PKEYS = [
|
||||
"analyzerFunc" => "analyzer_func",
|
||||
"extractorFunc" => "extractor_func",
|
||||
"parserFunc" => "parser_func",
|
||||
"normalizerFunc" => "normalizer_func",
|
||||
"formatterFunc" => "formatter_func",
|
||||
"nature" => ["", 0],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
<?php
|
||||
namespace nur\sery\schema;
|
||||
|
||||
use ArrayAccess;
|
||||
use BadMethodCallException;
|
||||
use LogicException;
|
||||
use nulib\cl;
|
||||
use nur\sery\schema\values\IValue;
|
||||
|
||||
abstract class Schema {
|
||||
abstract class Schema implements ArrayAccess {
|
||||
/**
|
||||
* créer si besoin une nouvelle instance à partir d'une définition de schéma
|
||||
*/
|
||||
|
@ -23,54 +26,6 @@ abstract class Schema {
|
|||
return $md;
|
||||
}
|
||||
|
||||
protected static function ensure_string(&$string): void {
|
||||
if (!is_string($string)) $string = strval($string);
|
||||
}
|
||||
protected static function ensure_nstring(&$string): void {
|
||||
if ($string !== null) self::ensure_string($string);
|
||||
}
|
||||
protected static function ensure_bool(&$bool): void {
|
||||
if (!is_bool($bool)) $bool = boolval($bool);
|
||||
}
|
||||
protected static function ensure_nbool(&$bool): void {
|
||||
if ($bool !== null) self::ensure_bool($bool);
|
||||
}
|
||||
protected static function ensure_callable(&$callable): void {
|
||||
if (!is_callable($callable)) throw SchemaException::invalid_callable($callable);
|
||||
}
|
||||
protected static function ensure_ncallable(&$callable): void {
|
||||
if ($callable !== null) self::ensure_callable($callable);
|
||||
}
|
||||
protected static function ensure_array(&$array): void {
|
||||
if (!is_array($array)) $array = cl::with($array);
|
||||
}
|
||||
protected static function ensure_narray(&$array): void {
|
||||
if ($array !== null) self::ensure_array($array);
|
||||
}
|
||||
protected static function ensure_key(&$key): void {
|
||||
if (!is_string($key) && !is_int($key)) $key = strval($key);
|
||||
}
|
||||
protected static function ensure_nkey(&$key): void {
|
||||
if ($key !== null) self::ensure_key($key);
|
||||
}
|
||||
protected static function ensure_pkey(&$pkey): void {
|
||||
if (!is_string($pkey) && !is_int($pkey) && !is_array($pkey)) $pkey = strval($pkey);
|
||||
if (is_array($pkey)) {
|
||||
foreach ($pkey as &$key) {
|
||||
self::ensure_key($key);
|
||||
}; unset($key);
|
||||
}
|
||||
}
|
||||
protected static function ensure_npkey(&$pkey): void {
|
||||
if ($pkey !== null) self::ensure_pkey($pkey);
|
||||
}
|
||||
protected static function ensure_content(&$content): void {
|
||||
if (!is_string($content) && !is_array($content)) $content = strval($content);
|
||||
}
|
||||
protected static function ensure_ncontent(&$content): void {
|
||||
if ($content !== null) self::ensure_content($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array définition du schéma, à redéfinir le cas échéant dans une classe
|
||||
* dérivée
|
||||
|
@ -88,4 +43,28 @@ abstract class Schema {
|
|||
function isScalar(?ScalarSchema &$scalar=null): bool { return false; }
|
||||
|
||||
abstract function getValue(&$value, $key=null): IValue;
|
||||
|
||||
|
||||
#############################################################################
|
||||
# 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
<?php
|
||||
namespace nur\sery\schema\input;
|
||||
|
||||
#XXX implémenter le renommage de paramètres et faire des méthodes pour
|
||||
# construire des querystring et paramètres de formulaires
|
||||
/**
|
||||
* Class FormInput: accès à des paramètres de formulaire (POST ou GET, dans cet
|
||||
* ordre)
|
||||
*
|
||||
* cette implémentation lit depuis les paramètres de formulaire et écrit dans
|
||||
* une référence
|
||||
*
|
||||
*/
|
||||
class FormInput extends Input {
|
||||
function exists($key=null): bool {
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace nur\sery\schema\input;
|
|||
* cette implémentation lit depuis les paramètres de formulaire et écrit dans
|
||||
* une référence
|
||||
*/
|
||||
class GetInput extends Input {
|
||||
class GetInput extends FormInput {
|
||||
function exists($key=null): bool {
|
||||
if ($key === null) return false;
|
||||
return array_key_exists($key, $_GET);
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace nur\sery\schema\input;
|
|||
* cette implémentation lit depuis les paramètres de formulaire et écrit dans
|
||||
* une référence
|
||||
*/
|
||||
class PostInput extends Input {
|
||||
class PostInput extends FormInput {
|
||||
function exists($key=null): bool {
|
||||
if ($key === null) return false;
|
||||
return array_key_exists($key, $_POST);
|
||||
|
|
|
@ -5,7 +5,7 @@ class ref_schema {
|
|||
/** @var array schéma des natures de schéma */
|
||||
const NATURE_METASCHEMA = [
|
||||
"nature" => ["string", null, "nature du schéma",
|
||||
"key" => 0,
|
||||
"pkey" => 0,
|
||||
"allowed_values" => ["assoc", "list", "scalar"],
|
||||
],
|
||||
"title" => ["?string", null, "libellé de la valeur"],
|
||||
|
@ -35,7 +35,7 @@ class ref_schema {
|
|||
"" => ["assoc", "schema" => self::NATURE_METASCHEMA],
|
||||
],
|
||||
"name" => ["?string", null, "identifiant de la valeur"],
|
||||
"key" => ["?pkey", null, "chemin de clé de la valeur dans un tableau associatif"],
|
||||
"pkey" => ["?pkey", null, "chemin de clé de la valeur dans un tableau associatif"],
|
||||
"header" => ["?string", null, "nom de l'en-tête s'il faut présenter cette donnée dans un tableau"],
|
||||
"composite" => ["?bool", null, "ce champ fait-il partie d'une valeur composite?"],
|
||||
];
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
namespace nur\sery\schema\types;
|
||||
|
||||
use nulib\cl;
|
||||
|
||||
class tarray {
|
||||
static function ensure_array(&$array): void {
|
||||
if (!is_array($array)) $array = cl::with($array);
|
||||
}
|
||||
|
||||
static function ensure_narray(&$array): void {
|
||||
if ($array !== null) self::ensure_array($array);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
namespace nur\sery\schema\types;
|
||||
|
||||
class tbool {
|
||||
static function ensure_bool(&$bool): void {
|
||||
if (!is_bool($bool)) $bool = boolval($bool);
|
||||
}
|
||||
|
||||
static function ensure_nbool(&$bool): void {
|
||||
if ($bool !== null) self::ensure_bool($bool);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
namespace nur\sery\schema\types;
|
||||
|
||||
use nur\sery\schema\SchemaException;
|
||||
|
||||
class tcallable {
|
||||
static function ensure_callable(&$callable): void {
|
||||
if (!is_callable($callable)) throw SchemaException::invalid_callable($callable);
|
||||
}
|
||||
|
||||
static function ensure_ncallable(&$callable): void {
|
||||
if ($callable !== null) self::ensure_callable($callable);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
namespace nur\sery\schema\types;
|
||||
|
||||
class tcontent {
|
||||
static function ensure_content(&$content): void {
|
||||
if (!is_string($content) && !is_array($content)) $content = strval($content);
|
||||
}
|
||||
|
||||
static function ensure_ncontent(&$content): void {
|
||||
if ($content !== null) self::ensure_content($content);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
namespace nur\sery\schema\types;
|
||||
|
||||
class tkey {
|
||||
static function ensure_key(&$key): void {
|
||||
if (!is_string($key) && !is_int($key)) $key = strval($key);
|
||||
}
|
||||
|
||||
static function ensure_nkey(&$key): void {
|
||||
if ($key !== null) self::ensure_key($key);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
namespace nur\sery\schema\types;
|
||||
|
||||
class tpkey {
|
||||
static function ensure_pkey(&$pkey): void {
|
||||
if (!is_string($pkey) && !is_int($pkey) && !is_array($pkey)) $pkey = strval($pkey);
|
||||
if (is_array($pkey)) {
|
||||
foreach ($pkey as &$key) {
|
||||
tkey::ensure_key($key);
|
||||
};
|
||||
unset($key);
|
||||
}
|
||||
}
|
||||
|
||||
static function ensure_npkey(&$pkey): void {
|
||||
if ($pkey !== null) self::ensure_pkey($pkey);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
namespace nur\sery\schema\types;
|
||||
|
||||
class tstring {
|
||||
static function ensure_string(&$string): void {
|
||||
if (!is_string($string)) $string = strval($string);
|
||||
}
|
||||
|
||||
static function ensure_nstring(&$string): void {
|
||||
if ($string !== null) self::ensure_string($string);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue