File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55namespace Chubbyphp \Parsing \Schema ;
66
7+ use Chubbyphp \Parsing \Error ;
78use Chubbyphp \Parsing \ErrorsException ;
89use Chubbyphp \Parsing \Result ;
910
1011abstract class AbstractSchemaInnerParse implements SchemaInterface
1112{
13+ public const string ERROR_REFINE_CODE = 'refine ' ;
14+ public const string ERROR_REFINE_TEMPLATE = '{{message}} ' ;
15+
1216 protected bool $ nullable = false ;
1317
1418 /**
@@ -58,6 +62,26 @@ final public function postParse(\Closure $postParse): static
5862 return $ clone ;
5963 }
6064
65+ /**
66+ * @param \Closure(mixed $output): bool $refine
67+ */
68+ final public function refine (\Closure $ refine , string $ message ): static
69+ {
70+ return $ this ->postParse (static function (mixed $ output ) use ($ refine , $ message ): mixed {
71+ if (!$ refine ($ output )) {
72+ throw new ErrorsException (
73+ new Error (
74+ self ::ERROR_REFINE_CODE ,
75+ self ::ERROR_REFINE_TEMPLATE ,
76+ ['message ' => $ message ]
77+ )
78+ );
79+ }
80+
81+ return $ output ;
82+ });
83+ }
84+
6185 final public function parse (mixed $ input ): mixed
6286 {
6387 try {
Original file line number Diff line number Diff line change @@ -55,6 +55,16 @@ public function postParse(\Closure $postParse): static
5555 throw new \BadMethodCallException ('LazySchema does not support any modification, "postParse" called. ' );
5656 }
5757
58+ /**
59+ * @internal
60+ *
61+ * @param \Closure(mixed $output): bool $refine
62+ */
63+ public function refine (\Closure $ refine , string $ message ): static
64+ {
65+ throw new \BadMethodCallException ('LazySchema does not support any modification, "refine" called. ' );
66+ }
67+
5868 public function parse (mixed $ input ): mixed
5969 {
6070 $ schema = $ this ->resolveSchema ();
Original file line number Diff line number Diff line change 99
1010/**
1111 * @method static default(mixed $default)
12+ * @method static refine(\Closure(mixed $output): bool $refine, string $message)
1213 */
1314interface SchemaInterface
1415{
@@ -23,6 +24,11 @@ public function preParse(\Closure $preParse): static;
2324
2425 public function postParse (\Closure $ postParse ): static ;
2526
27+ // /**
28+ // * @param \Closure(mixed $output): bool $refine
29+ // */
30+ // public function refine(\Closure $refine, string $message): static;
31+
2632 public function parse (mixed $ input ): mixed ;
2733
2834 public function safeParse (mixed $ input ): Result ;
You can’t perform that action at this time.
0 commit comments