Skip to content

Commit 18b5e58

Browse files
committed
reorder methods, update interface
1 parent a4c1e40 commit 18b5e58

7 files changed

Lines changed: 122 additions & 107 deletions

File tree

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ $user = $userSchema->parse([
103103
All schemas support these methods:
104104

105105
```php
106-
$schema->nullable(); // Allow null values
107-
$schema->default($value); // Provide default when input is null
108-
$schema->preParse($fn); // Transform input before parsing
109-
$schema->postParse($fn); // Transform output after parsing
110-
$schema->catch($fn); // Handle errors and provide fallback
111-
$schema->parse($input); // Parse and throw on error
112-
$schema->safeParse($input); // Parse and return Result object
106+
$schema->nullable(); // Allow null values
107+
$schema->default($value); // Provide default when input is null
108+
$schema->preParse($fn); // Transform input before parsing
109+
$schema->postParse($fn); // Transform output after parsing
110+
$schema->parse($input); // Parse and throw on error
111+
$schema->safeParse($input); // Parse and return Result object
112+
$schema->catch($fn); // Handle errors and provide fallback
113113
```
114114

115115
## Error Handling

src/Schema/AbstractSchema.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ final public function nullable(bool $nullable = true): static
3434
return $clone;
3535
}
3636

37+
final public function default(mixed $default): static
38+
{
39+
return $this->preParse(static fn (mixed $input) => $input ?? $default);
40+
}
41+
3742
/**
3843
* @param \Closure(mixed $input): mixed $preParse
3944
*/
@@ -73,11 +78,6 @@ final public function catch(\Closure $catch): static
7378
return $clone;
7479
}
7580

76-
final public function default(mixed $default): static
77-
{
78-
return $this->preParse(static fn (mixed $input) => $input ?? $default);
79-
}
80-
8181
final protected function dispatchPreParses(mixed $data): mixed
8282
{
8383
return array_reduce(

src/Schema/LazySchema.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44

55
namespace Chubbyphp\Parsing\Schema;
66

7-
use Chubbyphp\Parsing\ErrorsException;
87
use Chubbyphp\Parsing\Result;
98

10-
/**
11-
* @phpstan-type Call array{name: string, arguments: array<mixed>}
12-
*/
139
final class LazySchema implements SchemaInterface
1410
{
1511
private ?SchemaInterface $schema = null;
@@ -19,38 +15,44 @@ final class LazySchema implements SchemaInterface
1915
*/
2016
public function __construct(private \Closure $lazy) {}
2117

18+
/**
19+
* @internal
20+
*
21+
* @infection-ignore-all
22+
*/
2223
public function nullable(bool $nullable = true): static
2324
{
24-
throw new \BadMethodCallException(
25-
\sprintf(
26-
'LazySchema does not support any modification, "nullable" called with %s.',
27-
$nullable ? 'true' : 'false'
28-
)
29-
);
25+
throw new \BadMethodCallException('LazySchema does not support any modification, "nullable" called.');
3026
}
3127

3228
/**
33-
* @param \Closure(mixed $input): mixed $preParse
29+
* @internal
30+
*
31+
* @infection-ignore-all
3432
*/
35-
public function preParse(\Closure $preParse): static
33+
public function default(mixed $default): static
3634
{
37-
throw new \BadMethodCallException('LazySchema does not support any modification, "preParse" called.');
35+
throw new \BadMethodCallException('LazySchema does not support any modification, "default" called.');
3836
}
3937

4038
/**
41-
* @param \Closure(mixed $input): mixed $postParse
39+
* @internal
40+
*
41+
* @infection-ignore-all
4242
*/
43-
public function postParse(\Closure $postParse): static
43+
public function preParse(\Closure $preParse): static
4444
{
45-
throw new \BadMethodCallException('LazySchema does not support any modification, "postParse" called.');
45+
throw new \BadMethodCallException('LazySchema does not support any modification, "preParse" called.');
4646
}
4747

4848
/**
49-
* @param \Closure(mixed $input, ErrorsException $e): mixed $catch
49+
* @internal
50+
*
51+
* @infection-ignore-all
5052
*/
51-
public function catch(\Closure $catch): static
53+
public function postParse(\Closure $postParse): static
5254
{
53-
throw new \BadMethodCallException('LazySchema does not support any modification, "catch" called.');
55+
throw new \BadMethodCallException('LazySchema does not support any modification, "postParse" called.');
5456
}
5557

5658
public function parse(mixed $input): mixed
@@ -67,6 +69,14 @@ public function safeParse(mixed $input): Result
6769
return $schema->safeParse($input);
6870
}
6971

72+
/**
73+
* @internal
74+
*/
75+
public function catch(\Closure $catch): static
76+
{
77+
throw new \BadMethodCallException('LazySchema does not support any modification, "catch" called.');
78+
}
79+
7080
private function resolveSchema(): SchemaInterface
7181
{
7282
if (!$this->schema) {

src/Schema/SchemaInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
use Chubbyphp\Parsing\ErrorsException;
88
use Chubbyphp\Parsing\Result;
99

10+
/**
11+
* @method static default(mixed $default)
12+
*/
1013
interface SchemaInterface
1114
{
1215
public function nullable(bool $nullable = true): static;
1316

17+
// public function default(mixed $default): static;
18+
1419
/**
1520
* @param \Closure(mixed $input): mixed $preParse
1621
*/

tests/Unit/ParserTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,22 @@ public function testObject(): void
138138
self::assertInstanceOf(ObjectSchema::class, $objectSchema);
139139
}
140140

141-
public function testString(): void
141+
public function testRecord(): void
142142
{
143143
$p = new Parser();
144144

145-
$stringSchema = $p->string();
145+
$recordSchema = $p->record($p->string());
146146

147-
self::assertInstanceOf(StringSchema::class, $stringSchema);
147+
self::assertInstanceOf(RecordSchema::class, $recordSchema);
148148
}
149149

150-
public function testRecord(): void
150+
public function testString(): void
151151
{
152152
$p = new Parser();
153153

154-
$recordSchema = $p->record($p->string());
154+
$stringSchema = $p->string();
155155

156-
self::assertInstanceOf(RecordSchema::class, $recordSchema);
156+
self::assertInstanceOf(StringSchema::class, $stringSchema);
157157
}
158158

159159
public function testTuple(): void

tests/Unit/Schema/IntSchemaTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -501,22 +501,6 @@ public function testParseWithInvalidNonPositive(): void
501501
}
502502
}
503503

504-
public function testParseWithValidToDateTime(): void
505-
{
506-
$input = 1705742100;
507-
508-
$schema = (new IntSchema())->toDateTime()->from(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'));
509-
510-
self::assertEquals(new \DateTimeImmutable('@'.$input), $schema->parse($input));
511-
}
512-
513-
public function testParseWithToDateTimeNullable(): void
514-
{
515-
$schema = (new IntSchema())->nullable()->toDateTime();
516-
517-
self::assertNull($schema->parse(null));
518-
}
519-
520504
public function testParseWithToFloat(): void
521505
{
522506
$input = 42;
@@ -548,4 +532,20 @@ public function testParseWithToStringNullable(): void
548532

549533
self::assertNull($schema->parse(null));
550534
}
535+
536+
public function testParseWithValidToDateTime(): void
537+
{
538+
$input = 1705742100;
539+
540+
$schema = (new IntSchema())->toDateTime()->from(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'));
541+
542+
self::assertEquals(new \DateTimeImmutable('@'.$input), $schema->parse($input));
543+
}
544+
545+
public function testParseWithToDateTimeNullable(): void
546+
{
547+
$schema = (new IntSchema())->nullable()->toDateTime();
548+
549+
self::assertNull($schema->parse(null));
550+
}
551551
}

0 commit comments

Comments
 (0)