Skip to content

Commit f87862b

Browse files
committed
refine
1 parent a095c08 commit f87862b

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/Schema/AbstractSchemaInnerParse.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace Chubbyphp\Parsing\Schema;
66

7+
use Chubbyphp\Parsing\Error;
78
use Chubbyphp\Parsing\ErrorsException;
89
use Chubbyphp\Parsing\Result;
910

1011
abstract 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 {

src/Schema/LazySchema.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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();

src/Schema/SchemaInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* @method static default(mixed $default)
12+
* @method static refine(\Closure(mixed $output): bool $refine, string $message)
1213
*/
1314
interface 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;

0 commit comments

Comments
 (0)