nur-sery/wip/schema/_list/ListSchema.php

54 lines
1.7 KiB
PHP
Raw Normal View History

2023-11-24 16:50:05 +04:00
<?php
2024-05-23 08:15:28 +04:00
namespace nur\sery\wip\schema\_list;
2023-11-27 22:39:35 +04:00
2024-01-01 01:01:27 +04:00
use nur\sery\ref\schema\ref_schema;
2024-05-23 08:15:28 +04:00
use nur\sery\wip\schema\Schema;
use nur\sery\wip\schema\Value;
2023-11-24 16:50:05 +04:00
class ListSchema extends Schema {
/** @var array meta-schema d'un schéma de nature liste */
2024-01-01 01:01:27 +04:00
const METASCHEMA = ref_schema::LIST_METASCHEMA;
2023-11-24 16:50:05 +04:00
/**
* indiquer si $definition est une définition de schéma de nature liste que
* {@link normalize()} pourrait normaliser
*/
static function isa_definition($definition): bool {
if (!is_array($definition)) return false;
# nature explicitement spécifiée
if (array_key_exists("", $definition)) {
$nature = $definition[""];
if ($nature === "list") return true;
if (is_array($nature)
&& array_key_exists(0, $nature)
&& $nature[0] === "list") {
return true;
}
return false;
}
# un unique élément tableau à l'index 0
$count = count($definition);
$haveIndex0 = array_key_exists(0, $definition);
return $count == 1 && $haveIndex0 && is_array($definition[0]);
}
2023-11-28 08:20:33 +04:00
static function normalize($definition, $definitionKey=null): array {
2023-11-24 16:50:05 +04:00
}
2023-11-28 08:20:33 +04:00
function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
2023-11-24 16:50:05 +04:00
if ($definition === null) $definition = static::SCHEMA;
2023-11-28 08:20:33 +04:00
if ($normalize) $definition = self::normalize($definition, $definitionKey);
2023-11-24 16:50:05 +04:00
$this->definition = $definition;
}
2023-11-24 22:36:33 +04:00
2023-11-25 10:04:24 +04:00
function isList(?ListSchema &$list=null): bool {
$list = $this;
2023-11-24 22:36:33 +04:00
return true;
}
2023-11-27 22:39:35 +04:00
2024-01-01 01:01:27 +04:00
function newValue(?Value &$destv=null, &$dest=null, $destKey=null): ListValue {
if ($destv instanceof ListValue) return $destv->reset($dest, $destKey);
else return ($destv = new ListValue($this, $dest, $destKey));
2023-11-27 22:39:35 +04:00
}
2023-11-24 16:50:05 +04:00
}