diff --git a/src/schema/_assoc/AssocWrapper.php b/src/schema/_assoc/AssocWrapper.php index a071c62..05fad7d 100644 --- a/src/schema/_assoc/AssocWrapper.php +++ b/src/schema/_assoc/AssocWrapper.php @@ -2,6 +2,7 @@ namespace nur\sery\wip\schema\_assoc; use nulib\ValueException; +use nur\sery\wip\schema\_scalar\ScalarResult; use nur\sery\wip\schema\_scalar\ScalarWrapper; use nur\sery\wip\schema\Result; use nur\sery\wip\schema\types\IType; @@ -71,7 +72,23 @@ class AssocWrapper extends Wrapper { * @param AssocWrapper $wrapper */ static function _analyze(?array $params, WrapperContext $context, Wrapper $wrapper): int { - return ScalarWrapper::_analyze($params, $context, $wrapper); + if ($context->ensureArray) { + $array = $context->input->get(); + if ($array === null) $context->input->set([]); + } + $what = ScalarWrapper::_analyze($params, $context, $wrapper); + /** @var ScalarResult $result */ + $result = $context->result; + if (!$result->valid) return $what; + foreach ($context->keyWrappers as $wrapper) { + $wrapper->analyze($params); + if (!$wrapper->isValid()) { + $result->setInvalid(null, $context->schema, $wrapper->getResult()->exception); + break; + #XXX + } + } + return $what; } static function _normalize(?array $params, WrapperContext $context, Wrapper $wrapper): bool { diff --git a/src/schema/_assoc/AssocWrapperContext.php b/src/schema/_assoc/AssocWrapperContext.php index a1a4693..07b9f79 100644 --- a/src/schema/_assoc/AssocWrapperContext.php +++ b/src/schema/_assoc/AssocWrapperContext.php @@ -2,10 +2,23 @@ namespace nur\sery\wip\schema\_assoc; use nur\sery\wip\schema\_scalar\ScalarWrapper; +use nur\sery\wip\schema\input\Input; +use nur\sery\wip\schema\Schema; 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->ensureArray = $params["ensure_array"] ?? false; + $this->ensureKeys = $params["ensure_keys"] ?? true; + $this->ensureOrder = $params["ensure_order"] ?? true; + } + + public bool $ensureArray; + public bool $ensureKeys; + public bool $ensureOrder; + public ?ScalarWrapper $arrayWrapper = null; /** liste des clés valides */