From 93d13c1ada522a259cf9366f66926c8fa9025442 Mon Sep 17 00:00:00 2001 From: Dominik Zogg Date: Sat, 31 Jan 2026 17:15:35 +0100 Subject: [PATCH] refine --- src/Schema/AbstractSchemaInnerParse.php | 22 ++++++++++++++++++++++ src/Schema/LazySchema.php | 11 +++++++++++ src/Schema/SchemaInterface.php | 7 +++++++ 3 files changed, 40 insertions(+) diff --git a/src/Schema/AbstractSchemaInnerParse.php b/src/Schema/AbstractSchemaInnerParse.php index 19afe98..37a58b5 100644 --- a/src/Schema/AbstractSchemaInnerParse.php +++ b/src/Schema/AbstractSchemaInnerParse.php @@ -4,6 +4,7 @@ namespace Chubbyphp\Parsing\Schema; +use Chubbyphp\Parsing\Error; use Chubbyphp\Parsing\ErrorsException; use Chubbyphp\Parsing\Result; @@ -58,6 +59,27 @@ final public function postParse(\Closure $postParse): static return $clone; } + /** + * @param \Closure(mixed $output): bool $refine + * @param array $variables + */ + final public function refine(\Closure $refine, string $code, string $template, array $variables = []): static + { + return $this->postParse(static function (mixed $output) use ($refine, $code, $template, $variables): mixed { + if (!$refine($output)) { + throw new ErrorsException( + new Error( + $code, + $template, + $variables + ) + ); + } + + return $output; + }); + } + final public function parse(mixed $input): mixed { try { diff --git a/src/Schema/LazySchema.php b/src/Schema/LazySchema.php index 01964e2..ed80984 100644 --- a/src/Schema/LazySchema.php +++ b/src/Schema/LazySchema.php @@ -55,6 +55,17 @@ public function postParse(\Closure $postParse): static throw new \BadMethodCallException('LazySchema does not support any modification, "postParse" called.'); } + /** + * internal. + * + * @param \Closure(mixed $output): bool $refine + * @param array $variables + */ + public function refine(\Closure $refine, string $code, string $template, array $variables = []): static + { + throw new \BadMethodCallException('LazySchema does not support any modification, "refine" called.'); + } + public function parse(mixed $input): mixed { $schema = $this->resolveSchema(); diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 4bca069..3f0d913 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -9,6 +9,7 @@ /** * @method static default(mixed $default) + * @method static refine(\Closure(mixed $output): bool $refine, string $code, string $template, array $variables = []) */ interface SchemaInterface { @@ -23,6 +24,12 @@ public function preParse(\Closure $preParse): static; public function postParse(\Closure $postParse): static; + // /** + // * @param \Closure(mixed $output): bool $refine + // * @param array $variables + // */ + // public function refine(\Closure $refine, string $code, string $template, array $variables = []): static; + public function parse(mixed $input): mixed; public function safeParse(mixed $input): Result;