modifs.mineures sans commentaires
This commit is contained in:
parent
4b84f11f99
commit
50cf0eca33
@ -13,9 +13,9 @@ abstract class Result {
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
function isAssoc(?AssocResult &$assoc=null): bool { return false; }
|
||||
function isList(?ListResult &$list=null): bool { return false; }
|
||||
function isScalar(?ScalarResult &$scalar=null): bool { return false; }
|
||||
function isAssoc(?AssocResult &$result=null): bool { return false; }
|
||||
function isList(?ListResult &$result=null): bool { return false; }
|
||||
function isScalar(?ScalarResult &$result=null): bool { return false; }
|
||||
|
||||
/**
|
||||
* Obtenir la liste des clés valides pour les valeurs accessibles via cet
|
||||
|
@ -9,6 +9,7 @@ use nulib\ref\schema\ref_types;
|
||||
use nur\sery\wip\schema\_assoc\AssocSchema;
|
||||
use nur\sery\wip\schema\_list\ListSchema;
|
||||
use nur\sery\wip\schema\_scalar\ScalarSchema;
|
||||
use nur\sery\wip\schema\types\IType;
|
||||
use nur\sery\wip\schema\types\tarray;
|
||||
use nur\sery\wip\schema\types\tbool;
|
||||
use nur\sery\wip\schema\types\tcallable;
|
||||
@ -26,18 +27,18 @@ abstract class Schema implements ArrayAccess {
|
||||
* l'instance de Schema nouvelle créée
|
||||
* - sinon, prendre $definition comme définition
|
||||
*/
|
||||
static function ns(&$schema, $definition=null, $definitionKey=null): self {
|
||||
static function ns(&$schema, $definition=null, $definitionKey=null, bool $normalize=true): self {
|
||||
if (is_array($schema)) {
|
||||
$definition = $schema;
|
||||
$schema = null;
|
||||
}
|
||||
if ($schema === null) {
|
||||
if (AssocSchema::isa_definition($definition)) {
|
||||
$schema = new AssocSchema($definition, $definitionKey);
|
||||
$schema = new AssocSchema($definition, $definitionKey, $normalize);
|
||||
} elseif (ListSchema::isa_definition($definition)) {
|
||||
$schema = new ListSchema($definition, $definitionKey);
|
||||
$schema = new ListSchema($definition, $definitionKey, $normalize);
|
||||
} elseif (ScalarSchema::isa_definition($definition)) {
|
||||
$schema = new ScalarSchema($definition, $definitionKey);
|
||||
$schema = new ScalarSchema($definition, $definitionKey, $normalize);
|
||||
} else {
|
||||
throw SchemaException::invalid_schema();
|
||||
}
|
||||
@ -59,7 +60,22 @@ abstract class Schema implements ArrayAccess {
|
||||
return self::ns($schema, $definition)->getWrapper($value, $valueKey, $wrapper);
|
||||
}
|
||||
|
||||
protected static function _normalize($definition, $definitionKey=null): array {
|
||||
protected static function have_nature(array $definition, ?string &$nature=null): bool {
|
||||
$definitionNature = $definition[""] ?? null;
|
||||
if (is_string($definitionNature)) {
|
||||
$nature = $definitionNature;
|
||||
return true;
|
||||
}
|
||||
if (is_array($definitionNature)
|
||||
&& array_key_exists(0, $definitionNature)
|
||||
&& is_string($definitionNature[0])) {
|
||||
$nature = $definitionNature;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static function _normalize(&$definition, $definitionKey=null): void {
|
||||
if (!is_array($definition)) $definition = [$definition];
|
||||
# s'assurer que toutes les clés existent avec leur valeur par défaut
|
||||
$index = 0;
|
||||
@ -152,16 +168,14 @@ abstract class Schema implements ArrayAccess {
|
||||
|
||||
switch ($nature[0] ?? null) {
|
||||
case "assoc":
|
||||
foreach ($definition["schema"] as $definitionKey => &$subdef) {
|
||||
$subdef = self::_normalize($subdef, $definitionKey);
|
||||
}; unset($subdef);
|
||||
foreach ($definition["schema"] as $key => &$keydef) {
|
||||
self::_normalize($keydef, $key);
|
||||
}; unset($keydef);
|
||||
break;
|
||||
case "list":
|
||||
self::_normalize($definition["schema"]);
|
||||
break;
|
||||
}
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
protected static function _ensure_nature(array $definition, string $expectedNature, ?string $expectedType=null): void {
|
||||
@ -181,7 +195,7 @@ abstract class Schema implements ArrayAccess {
|
||||
$types = $definition["type"];
|
||||
$nullable = $definition["nullable"];
|
||||
# s'il n'y a qu'une seul type, l'instancier tout de suite
|
||||
if (count($types) == 1 && $types[0] !== null) {
|
||||
if (is_array($types) && count($types) == 1 && $types[0] !== null) {
|
||||
foreach ($types as $key => $name) {
|
||||
if ($key === 0) {
|
||||
$args = null;
|
||||
@ -194,10 +208,9 @@ abstract class Schema implements ArrayAccess {
|
||||
}
|
||||
switch ($definition[""][0]) {
|
||||
case "assoc":
|
||||
foreach ($definition["schema"] as &$definitionValue) {
|
||||
self::_ensure_type($definitionValue);
|
||||
};
|
||||
unset($definitionValue);
|
||||
foreach ($definition["schema"] as &$keydef) {
|
||||
self::_ensure_type($keydef);
|
||||
}; unset($keydef);
|
||||
break;
|
||||
case "list":
|
||||
self::_ensure_type($definition["schema"]);
|
||||
@ -205,35 +218,18 @@ abstract class Schema implements ArrayAccess {
|
||||
}
|
||||
}
|
||||
|
||||
protected static function _ensure_schema_instances(array $definition): array {
|
||||
protected static function _ensure_schema_instances(array &$definition): void {
|
||||
switch ($definition[""][0]) {
|
||||
case "assoc":
|
||||
foreach ($definition["schema"] as &$subdef) {
|
||||
self::_ensure_schema_instances($subdef);
|
||||
Schema::ns($subdef);
|
||||
}; unset($subdef);
|
||||
foreach ($definition["schema"] as &$keydef) {
|
||||
self::_ensure_schema_instances($keydef);
|
||||
Schema::ns($keydef, null, null, false);
|
||||
}; unset($keydef);
|
||||
break;
|
||||
case "list":
|
||||
Schema::ns($definition["schema"]);
|
||||
Schema::ns($definition["schema"], null, null, false);
|
||||
break;
|
||||
}
|
||||
return $definition;
|
||||
}
|
||||
|
||||
protected static function _ensure_schema_array(array $definition): array {
|
||||
switch ($definition[""][0]) {
|
||||
case "assoc":
|
||||
foreach ($definition["schema"] as &$subdef) {
|
||||
if ($subdef instanceof Schema) $subdef = $subdef->getDefinition();
|
||||
}; unset($subdef);
|
||||
break;
|
||||
case "list":
|
||||
$subdef = $definition["schema"];
|
||||
if ($subdef instanceof Schema) $subdef = $subdef->getDefinition();
|
||||
$definition["schema"] = $subdef;
|
||||
break;
|
||||
}
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -242,10 +238,12 @@ abstract class Schema implements ArrayAccess {
|
||||
*/
|
||||
const SCHEMA = null;
|
||||
|
||||
protected array $_definition;
|
||||
|
||||
protected array $definition;
|
||||
|
||||
function getDefinition(): array {
|
||||
return self::_ensure_schema_array($this->definition);
|
||||
return $this->_definition;
|
||||
}
|
||||
|
||||
/** retourner true si le schéma est de nature tableau associatif */
|
||||
|
@ -1,5 +1,7 @@
|
||||
# nulib\schema
|
||||
|
||||
* tdate et tdatetime. qu'en est-il des autres classes (delay, etc.)
|
||||
* possibilité de spécifier le format de la date à analyser
|
||||
* ScalarSchema::from_property()
|
||||
* possibilité de spécifier un type via sa classe, e.g
|
||||
~~~php
|
||||
|
@ -4,5 +4,5 @@ namespace nur\sery\wip\schema\_assoc;
|
||||
use nur\sery\wip\schema\Result;
|
||||
|
||||
class AssocResult extends Result {
|
||||
function isAssoc(?AssocResult &$assoc=null): bool { $assoc = $this; return true;}
|
||||
function isAssoc(?AssocResult &$result=null): bool { $result = $this; return true;}
|
||||
}
|
||||
|
@ -13,25 +13,18 @@ class AssocSchema extends Schema {
|
||||
/** @var array meta-schema d'un schéma de nature tableau associatif */
|
||||
const METASCHEMA = ref_schema::ASSOC_METASCHEMA;
|
||||
|
||||
private static function have_nature(array $definition): bool {
|
||||
$nature = $definition[""] ?? null;
|
||||
if ($nature === "assoc") return true;
|
||||
if (is_array($nature)
|
||||
&& array_key_exists(0, $nature)
|
||||
&& $nature[0] === "assoc") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* indiquer si $definition est une définition de schéma de nature tableau
|
||||
* associatif que {@link normalize()} pourrait normaliser
|
||||
*/
|
||||
static function isa_definition($definition): bool {
|
||||
if (!is_array($definition)) return false;
|
||||
# nature explicitement spécifiée ou tableau associatif
|
||||
return self::have_nature($definition) || !cl::have_num_keys($definition);
|
||||
# nature explicitement spécifiée
|
||||
if (self::have_nature($definition, $nature)) {
|
||||
return $nature === "assoc";
|
||||
}
|
||||
# tableau associatif
|
||||
return !cl::have_num_keys($definition);
|
||||
}
|
||||
|
||||
static function normalize($definition, $definitionKey=null): array {
|
||||
@ -43,15 +36,19 @@ class AssocSchema extends Schema {
|
||||
"schema" => $definition,
|
||||
];
|
||||
}
|
||||
$definition = self::_normalize($definition, $definitionKey);
|
||||
self::_normalize($definition, $definitionKey);
|
||||
self::_ensure_nature($definition, "assoc", "array");
|
||||
return $definition;
|
||||
}
|
||||
|
||||
function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
|
||||
if ($definition === null) $definition = static::SCHEMA;
|
||||
if ($normalize) $definition = self::normalize($definition, $definitionKey);
|
||||
self::_ensure_type($definition);
|
||||
if ($normalize) {
|
||||
$definition = self::normalize($definition, $definitionKey);
|
||||
$this->_definition = $definition;
|
||||
self::_ensure_type($definition);
|
||||
self::_ensure_schema_instances($definition);
|
||||
}
|
||||
$this->definition = $definition;
|
||||
}
|
||||
|
||||
|
@ -4,5 +4,5 @@ namespace nur\sery\wip\schema\_list;
|
||||
use nur\sery\wip\schema\Result;
|
||||
|
||||
abstract/*XXX*/ class ListResult extends Result {
|
||||
function isList(?ListResult &$list=null): bool { $list = $this; return true;}
|
||||
function isList(?ListResult &$result=null): bool { $result = $this; return true;}
|
||||
}
|
||||
|
@ -9,17 +9,6 @@ class ListSchema extends Schema {
|
||||
/** @var array meta-schema d'un schéma de nature liste */
|
||||
const METASCHEMA = ref_schema::LIST_METASCHEMA;
|
||||
|
||||
private static function have_nature(array $definition): bool {
|
||||
$nature = $definition[""] ?? null;
|
||||
if ($nature === "list") return true;
|
||||
if (is_array($nature)
|
||||
&& array_key_exists(0, $nature)
|
||||
&& $nature[0] === "list") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* indiquer si $definition est une définition de schéma de nature liste que
|
||||
* {@link normalize()} pourrait normaliser
|
||||
@ -27,7 +16,9 @@ class ListSchema extends Schema {
|
||||
static function isa_definition($definition): bool {
|
||||
if (!is_array($definition)) return false;
|
||||
# nature explicitement spécifiée
|
||||
if (self::have_nature($definition)) return true;
|
||||
if (self::have_nature($definition, $nature)) {
|
||||
return $nature === "list";
|
||||
}
|
||||
# un unique élément tableau à l'index 0
|
||||
$count = count($definition);
|
||||
$haveIndex0 = array_key_exists(0, $definition);
|
||||
@ -43,15 +34,19 @@ class ListSchema extends Schema {
|
||||
"schema" => $definition[0],
|
||||
];
|
||||
}
|
||||
$definition = self::_normalize($definition, $definitionKey);
|
||||
self::_normalize($definition, $definitionKey);
|
||||
self::_ensure_nature($definition, "list", "array");
|
||||
return $definition;
|
||||
}
|
||||
|
||||
function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
|
||||
if ($definition === null) $definition = static::SCHEMA;
|
||||
if ($normalize) $definition = self::normalize($definition, $definitionKey);
|
||||
self::_ensure_type($definition);
|
||||
if ($normalize) {
|
||||
$definition = self::normalize($definition, $definitionKey);
|
||||
$this->_definition = $definition;
|
||||
self::_ensure_type($definition);
|
||||
self::_ensure_schema_instances($definition);
|
||||
}
|
||||
$this->definition = $definition;
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ class ScalarResult extends Result {
|
||||
"origValue", "normalizedValue",
|
||||
];
|
||||
|
||||
function isScalar(?ScalarResult &$scalar=null): bool { $scalar = $this; return true; }
|
||||
function isScalar(?ScalarResult &$result=null): bool { $result = $this; return true; }
|
||||
|
||||
function getKeys(): array {
|
||||
return [null];
|
||||
|
@ -1,19 +1,9 @@
|
||||
<?php
|
||||
namespace nur\sery\wip\schema\_scalar;
|
||||
|
||||
use nulib\cl;
|
||||
use nulib\ref\schema\ref_schema;
|
||||
use nulib\ref\schema\ref_types;
|
||||
use nur\sery\wip\schema\Schema;
|
||||
use nur\sery\wip\schema\SchemaException;
|
||||
use nur\sery\wip\schema\types;
|
||||
use nur\sery\wip\schema\types\IType;
|
||||
use nur\sery\wip\schema\types\tarray;
|
||||
use nur\sery\wip\schema\types\tbool;
|
||||
use nur\sery\wip\schema\types\tcallable;
|
||||
use nur\sery\wip\schema\types\tcontent;
|
||||
use nur\sery\wip\schema\types\tpkey;
|
||||
use nur\sery\wip\schema\types\trawstring;
|
||||
use nur\sery\wip\schema\Wrapper;
|
||||
|
||||
/**
|
||||
@ -75,15 +65,19 @@ class ScalarSchema extends Schema {
|
||||
}
|
||||
|
||||
static function normalize($definition, $definitionKey=null): array {
|
||||
$definition = self::_normalize($definition, $definitionKey);
|
||||
self::_normalize($definition, $definitionKey);
|
||||
self::_ensure_nature($definition, "scalar");
|
||||
return $definition;
|
||||
}
|
||||
|
||||
function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
|
||||
if ($definition === null) $definition = static::SCHEMA;
|
||||
if ($normalize) $definition = self::normalize($definition, $definitionKey);
|
||||
self::_ensure_type($definition);
|
||||
if ($normalize) {
|
||||
$definition = self::normalize($definition, $definitionKey);
|
||||
$this->_definition = $definition;
|
||||
self::_ensure_type($definition);
|
||||
self::_ensure_schema_instances($definition);
|
||||
}
|
||||
$this->definition = $definition;
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace nur\sery\wip\schema\_assoc;
|
||||
|
||||
use nulib\ext\yaml;
|
||||
use nulib\tests\TestCase;
|
||||
use nur\sery\wip\schema\_scalar\ScalarSchemaTest;
|
||||
use nur\sery\wip\schema\types\IType;
|
||||
|
||||
class AssocSchemaTest extends TestCase {
|
||||
const NULL_SCHEMA = [
|
||||
@ -67,4 +67,29 @@ class AssocSchemaTest extends TestCase {
|
||||
"c" => "bool",
|
||||
]));
|
||||
}
|
||||
|
||||
function testConstructor() {
|
||||
$schema = new AssocSchema([
|
||||
"a" => "string",
|
||||
"b" => "int",
|
||||
"c" => "bool",
|
||||
]);
|
||||
self::assertSame(self::schema([
|
||||
"type" => ["array"], "nullable" => true,
|
||||
], [
|
||||
"a" => [
|
||||
"type" => ["string"], "nullable" => false,
|
||||
"name" => "a", "pkey" => "a", "header" => "a",
|
||||
],
|
||||
"b" => [
|
||||
"type" => ["int"], "nullable" => false,
|
||||
"name" => "b", "pkey" => "b", "header" => "b",
|
||||
],
|
||||
"c" => [
|
||||
"type" => ["bool"], "nullable" => false,
|
||||
"name" => "c", "pkey" => "c", "header" => "c",
|
||||
],
|
||||
]), $schema->getDefinition());
|
||||
yaml::dump($schema->getDefinition());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user