modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-03-19 10:06:53 +04:00
parent 62fc315b9e
commit 9328aac9e9
8 changed files with 67 additions and 117 deletions

View File

@ -65,31 +65,31 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate {
}
/** analyser la valeur */
abstract protected function _analyze(?array $params): int;
abstract static function _analyze(?array $params, WrapperContext $context, Wrapper $wrapper): int;
function analyze(?array $params=null): bool {
$context = $this->context;
$reanalyze = $params["reanalyze"] ?? false;
if ($context->analyzed && !$reanalyze) return false;
$this->_analyze($params);
static::_analyze($params, $context, $this);
$context->analyzed = true;
return true;
}
/** normaliser la valeur */
abstract protected function _normalize(?array $params): bool;
abstract static function _normalize(?array $params, WrapperContext $context, Wrapper $wrapper): bool;
function normalize(?array $params=null): bool {
$context = $this->context;
// il faut que la valeur soit analysée avant de la normaliser
$this->analyze($params);
static::analyze($params);
if (!$context->analyzed) return false;
$renormalize = $params["renormalize"] ?? false;
if ($renormalize || !$context->normalized) {
$modified = $this->_normalize($params);
$modified = static::_normalize($params, $context, $this);
$context->normalized = true;
} else {
$modified = false;
@ -159,7 +159,6 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate {
return $this->getResult($key)->normalized;
}
function get($default=null, $key=false) {
$context = $this->context;
if (!$context->result->available) return $default;

View File

@ -10,16 +10,11 @@ class WrapperContext {
$this->schema = $schema;
if ($input !== null) $this->input = $input;
$this->valueKey = $valueKey;
$this->origValue = null;
$this->value = null;
$this->selectedKey = null;
$this->type = null;
$this->result = null;
}
public ?array $params;
public bool $analyze, $analyzed;
public bool $normalize, $normalized;
public bool $analyze, $analyzed = false;
public bool $normalize, $normalized = false;
public ?bool $throw;
function resetParams(?array $params): void {
@ -36,14 +31,14 @@ class WrapperContext {
/** @var string|int|null clé de la valeur dans le tableau destination */
public $valueKey;
/** @var mixed */
public $origValue;
public $origValue = null;
/** @var mixed */
public $value;
public $value = null;
/** @var string|int|null clé sélectionnée */
public $selectedKey;
public $selectedKey = null;
/** type de la valeur de la clé sélectionnée après analyse */
public ?IType $type;
public ?IType $type = null;
/** résultat de l'analyse de la valeur de la clé sélectionnée */
public ?Result $result;
public ?Result $result = null;
}

View File

@ -11,12 +11,6 @@ use nur\sery\wip\schema\Wrapper;
* Class AssocSchema
*/
class AssocSchema extends Schema {
//const METASCHEMA = ref_schema::VALUE_METASCHEMA;
//const NATURE_METASCHEMA = [
// ...ref_schema::NATURE_METASCHEMA,
// ...ref_schema::ASSOC_NATURE_METASCHEMA,
//];
/**
* indiquer si $definition est une définition de schéma de nature tableau
* associatif que {@link normalize_definition()} pourrait normaliser

View File

@ -2,8 +2,7 @@
namespace nur\sery\wip\schema\_assoc;
use nulib\ValueException;
use nur\sery\wip\schema\_scalar\ScalarResult;
use nur\sery\wip\schema\input\Input;
use nur\sery\wip\schema\_scalar\ScalarWrapper;
use nur\sery\wip\schema\Result;
use nur\sery\wip\schema\types\IType;
use nur\sery\wip\schema\Wrapper;
@ -12,16 +11,13 @@ use nur\sery\wip\schema\WrapperContext;
class AssocWrapper extends Wrapper {
function __construct(AssocSchema $schema, &$value=null, $valueKey=null, ?array $params=null) {
$keys = $schema->getKeys();
$keyTypes = [];
$keyWrappers = [];
foreach ($keys as $key) {
$keyTypes[$key] = null;
$keyWrappers[$key] = $schema->getSchema($key)->getWrapper();
}
$this->context = $context = new AssocWrapperContext($schema, null, null, $params);
$context->arrayResult = new ScalarResult();
$context->arrayWrapper = new ScalarWrapper($schema, $dummy, null, null, $context);
$context->keys = $keys;
$context->keyTypes = $keyTypes;
$context->keyWrappers = $keyWrappers;
# calculer manuellemet throw ici parce que WrapperContext le met à true par
@ -43,17 +39,13 @@ class AssocWrapper extends Wrapper {
protected function resetContext($resetSelectedKey): void {
$context = $this->context;
$context->arrayResult->reset();
$context->arrayWrapper->getResult()->reset();
foreach ($context->keyWrappers as $wrapper) {
$wrapper->getResult()->reset();
}
$context->analyzed = false;
$context->normalized = false;
if ($resetSelectedKey) {
$context->selectedKey = null;
$context->type = $context->arrayType;
$context->result = $context->arrayResult;
}
if ($resetSelectedKey) $context->selectedKey = null;
}
function getKeys(): array {
@ -61,6 +53,7 @@ class AssocWrapper extends Wrapper {
}
protected function _getWrapper($key): Wrapper {
if ($key === null) return $this->context->arrayWrapper;
$wrapper = $context->keyWrappers[$key] ?? null;
if ($wrapper === null) throw ValueException::invalid_key($key);
return $wrapper;
@ -68,76 +61,57 @@ class AssocWrapper extends Wrapper {
/** @param string|int|null $key */
function select($key=null): Wrapper {
$context = $this->context;
if ($key === null) {
$context->selectedKey = null;
$context->type = $context->arrayType;
$context->result = $context->arrayResult;
return $this;
}
$wrapper = $this->_getWrapper($key);
$context->selectedKey = $key;
$context->type = $wrapper->getType();
$context->result = $wrapper->getResult();
$this->context->selectedKey = $key;
return $wrapper;
}
protected function _analyze(?array $params): int {
return -1;
/**
* @param AssocWrapperContext $context
* @param AssocWrapper $wrapper
*/
static function _analyze(?array $params, WrapperContext $context, Wrapper $wrapper): int {
return ScalarWrapper::_analyze($params, $context, $wrapper);
}
protected function _normalize(?array $params): bool {
return false;
static function _normalize(?array $params, WrapperContext $context, Wrapper $wrapper): bool {
return ScalarWrapper::_normalize($params, $context, $wrapper);
}
function getResult($key=false): Result {
if ($key === false) return $this->context->result;
elseif ($key === null) return $this->context->arrayResult;
else return $this->_getWrapper($key)->getResult();
if ($key === false) $key = $this->context->selectedKey;
return $this->_getWrapper($key)->getResult();
}
function getType($key=false): IType {
if ($key === false) return $this->context->type;
elseif ($key === null) return $this->context->arrayType;
else return $this->_getWrapper($key)->getType();
if ($key === false) $key = $this->context->selectedKey;
return $this->_getWrapper($key)->getType();
}
function get($default=null, $key=false) {
$context = $this->context;
if (!$context->arrayResult->available) return $default;
if (!$context->arrayWrapper->isAvailable()) return $default;
if ($key === false) $key = $context->selectedKey;
if ($key === null) return $context->input->get($context->valueKey);
else return $this->_getWrapper($key)->get($default);
return $this->_getWrapper($key)->get($default);
}
function set($value, ?array $params=null, $key=false): Wrapper {
$context = $this->context;
if ($key === false) $key = $context->selectedKey;
if ($key === null) {
$context->input->set($value, $context->valueKey);
$this->afterModify($params);
} else {
$this->_getWrapper($key)->set($value);
}
$this->_getWrapper($key)->set($value);
return $this;
}
function unset(?array $params=null, $key=false): Wrapper {
$context = $this->context;
if ($key === false) $key = $context->selectedKey;
if ($key === null) {
$context->input->unset($context->valueKey);
$this->afterModify($params);
} else {
$this->_getWrapper($key)->unset();
}
$this->_getWrapper($key)->unset();
return $this;
}
function format($format=null, $key=false): string {
$context = $this->context;
if ($key === false) $key = $context->selectedKey;
if ($key === null) return $this->_format($context, $format);
else return $this->_getWrapper($key)->format($format);
return $this->_getWrapper($key)->format($format);
}
}

View File

@ -1,30 +1,16 @@
<?php
namespace nur\sery\wip\schema\_assoc;
use nur\sery\wip\schema\_scalar\ScalarResult;
use nur\sery\wip\schema\input\Input;
use nur\sery\wip\schema\Schema;
use nur\sery\wip\schema\types\IType;
use nur\sery\wip\schema\_scalar\ScalarWrapper;
use nur\sery\wip\schema\Wrapper;
use nur\sery\wip\schema\WrapperContext;
class AssocWrapperContext extends WrapperContext {
function __construct(Schema $schema, ?Input $input, $valueKey, ?array $params) {
parent::__construct($schema, $input, $valueKey, $params);
$this->arrayType = null;
$this->arrayResult = null;
}
public ?IType $arrayType;
/** résultat de l'analyse du tableau */
public ?ScalarResult $arrayResult;
public ?ScalarWrapper $arrayWrapper = null;
/** liste des clés valides */
public array $keys;
/** @var ?IType[] */
public array $keyTypes;
/** @var Wrapper[] */
public array $keyWrappers;
}

View File

@ -6,6 +6,7 @@ use nulib\ref\schema\ref_analyze;
use nulib\ref\schema\ref_schema;
use nulib\ValueException;
use nur\sery\wip\schema\Result;
use nur\sery\wip\schema\Schema;
use Throwable;
/**
@ -45,13 +46,13 @@ class ScalarResult extends Result {
$this->result[$name] = $value;
}
protected function getMessage(string $key, ScalarSchema $schema): string {
protected function getMessage(string $key, Schema $schema): string {
$message = cl::get($schema->messages, $key);
if ($message !== null) return $message;
return cl::get(ref_schema::MESSAGES, $key);
}
function setMissing(ScalarSchema $schema): int {
function setMissing( Schema $schema): int {
$this->resultAvailable = true;
$this->present = false;
$this->available = false;
@ -67,7 +68,7 @@ class ScalarResult extends Result {
}
}
function setUnavailable(ScalarSchema $schema): int {
function setUnavailable( Schema $schema): int {
$this->resultAvailable = true;
$this->present = true;
$this->available = false;
@ -83,7 +84,7 @@ class ScalarResult extends Result {
}
}
function setNull(ScalarSchema $schema): int {
function setNull( Schema $schema): int {
$this->resultAvailable = true;
$this->present = true;
$this->available = true;
@ -99,7 +100,7 @@ class ScalarResult extends Result {
}
}
function setInvalid($value, ScalarSchema $schema, ?Throwable $exception=null): int {
function setInvalid($value, Schema $schema, ?Throwable $exception=null): int {
$this->resultAvailable = true;
$this->present = true;
$this->available = true;

View File

@ -11,12 +11,6 @@ use nur\sery\wip\schema\Wrapper;
* Class ScalarSchema
*/
class ScalarSchema extends Schema {
//const METASCHEMA = ref_schema::VALUE_METASCHEMA;
//const NATURE_METASCHEMA = [
// ...ref_schema::NATURE_METASCHEMA,
// ...ref_schema::SCALAR_NATURE_METASCHEMA,
//];
/**
* indiquer si $definition est une définition de schéma scalaire que
* {@link normalize_definition()} pourrait normaliser

View File

@ -4,6 +4,9 @@ namespace nur\sery\wip\schema\_scalar;
use nulib\php\func;
use nulib\ref\schema\ref_analyze;
use nulib\ValueException;
use nur\sery\wip\schema\_assoc\AssocWrapper;
use nur\sery\wip\schema\_assoc\AssocWrapperContext;
use nur\sery\wip\schema\Schema;
use nur\sery\wip\schema\types;
use nur\sery\wip\schema\types\IType;
use nur\sery\wip\schema\Wrapper;
@ -12,15 +15,16 @@ use nur\sery\wip\schema\WrapperContext;
/**
* Class ScalarWrapper
*
* @method ScalarWrapper reset()
* @method ScalarResult getResult()
* @method self set()
* @method self unset()
* @method ScalarWrapper reset(&$value, $valueKey=null, ?array $params=null)
* @method ScalarResult getResult($key=false)
* @method self set($value, ?array $params=null, $key=false)
* @method self unset(?array $params=null, $key=false)
*/
class ScalarWrapper extends Wrapper {
function __construct(ScalarSchema $schema, &$value=null, $valueKey=null, ?array $params=null) {
$this->context = $context = new WrapperContext($schema, null, null, $params);
function __construct(Schema $schema, &$value=null, $valueKey=null, ?array $params=null, ?WrapperContext $context=null) {
if ($context === null) $context = new WrapperContext($schema, null, null, $params);
$context->result = new ScalarResult();
$this->context = $context;
# calculer manuellemet throw ici parce que WrapperContext le met à true par
# défaut. on veut pouvoir mettre temporairement throw à false si jamais il
@ -49,8 +53,7 @@ class ScalarWrapper extends Wrapper {
}
/** analyser la valeur et résoudre son type */
protected function _analyze0(): int {
$context = $this->context;
protected static function _analyze0(WrapperContext $context): int {
/** @var ScalarSchema $schema */
$schema = $context->schema;
$input = $context->input;
@ -141,8 +144,10 @@ class ScalarWrapper extends Wrapper {
}
}
protected function _analyze(?array $params): int {
$context = $this->context;
/**
* @param ScalarWrapper $wrapper
*/
static function _analyze(?array $params, WrapperContext $context, Wrapper $wrapper): int {
/** @var ScalarSchema $schema */
$schema = $context->schema;
$input = $context->input;
@ -152,15 +157,15 @@ class ScalarWrapper extends Wrapper {
/** @var func $analyzerFunc */
$analyzerFunc = $schema->analyzerFunc;
if ($analyzerFunc !== null) $what = $analyzerFunc->invoke([$context, $this]);
else $what = $this->_analyze0();
if ($analyzerFunc !== null) $what = $analyzerFunc->invoke([$context, $wrapper]);
else $what = self::_analyze0($context);
if ($what !== ref_analyze::STRING) return $what;
$value = $context->value;
try {
/** @var func $extractorFunc */
$extractorFunc = $schema->extractorFunc;
if ($extractorFunc !== null) $extracted = $extractorFunc->invoke([$value, $context, $this]);
if ($extractorFunc !== null) $extracted = $extractorFunc->invoke([$value, $context, $wrapper]);
else $extracted = $context->type->extract($value);
$context->value = $extracted;
} catch (ValueException $e) {
@ -171,7 +176,7 @@ class ScalarWrapper extends Wrapper {
try {
/** @var func $parserFunc */
$parserFunc = $schema->parserFunc;
if ($parserFunc !== null) $parsed = $parserFunc->invoke([$extracted, $context, $this]);
if ($parserFunc !== null) $parsed = $parserFunc->invoke([$extracted, $context, $wrapper]);
else $parsed = $context->type->parse($extracted);
$context->value = $parsed;
} catch (ValueException $e) {
@ -188,8 +193,10 @@ class ScalarWrapper extends Wrapper {
}
}
protected function _normalize(?array $params): bool {
$context = $this->context;
/**
* @param ScalarWrapper $wrapper
*/
static function _normalize(?array $params, WrapperContext $context, Wrapper $wrapper): bool {
/** @var ScalarSchema $schema */
$schema = $context->schema;
$input = $context->input;
@ -226,7 +233,7 @@ class ScalarWrapper extends Wrapper {
$normalizerFunc = $schema->normalizerFunc;
if ($normalizerFunc !== null) {
$orig = $value;
$value = $normalizerFunc->invoke([$orig, $context, $this]);
$value = $normalizerFunc->invoke([$orig, $context, $wrapper]);
$modified = $value !== $orig;
} else {
$modified = $context->type->verifix($value, $result, $schema);