Skip to content

Commit ad4ce49

Browse files
committed
enhance readability
1 parent 33bff9c commit ad4ce49

3 files changed

Lines changed: 38 additions & 36 deletions

File tree

src/Schema/BoolSchema.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ public function toString(): StringSchema
4444

4545
protected function innerParse(mixed $input): mixed
4646
{
47-
if (!\is_bool($input)) {
48-
throw new ErrorsException(
49-
new Error(
50-
self::ERROR_TYPE_CODE,
51-
self::ERROR_TYPE_TEMPLATE,
52-
['given' => $this->getDataType($input)]
53-
)
54-
);
47+
if (\is_bool($input)) {
48+
return $input;
5549
}
5650

57-
return $input;
51+
throw new ErrorsException(
52+
new Error(
53+
self::ERROR_TYPE_CODE,
54+
self::ERROR_TYPE_TEMPLATE,
55+
['given' => $this->getDataType($input)]
56+
)
57+
);
5858
}
5959
}

src/Schema/DateTimeSchema.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,34 @@ final class DateTimeSchema extends AbstractSchemaInnerParse implements SchemaInt
2121
public function from(\DateTimeImmutable $from): static
2222
{
2323
return $this->postParse(static function (\DateTimeImmutable $datetime) use ($from) {
24-
if ($datetime < $from) {
25-
throw new ErrorsException(
26-
new Error(
27-
self::ERROR_FROM_CODE,
28-
self::ERROR_FROM_TEMPLATE,
29-
['from' => $from->format('c'), 'given' => $datetime->format('c')]
30-
)
31-
);
24+
if ($datetime >= $from) {
25+
return $datetime;
3226
}
3327

34-
return $datetime;
28+
throw new ErrorsException(
29+
new Error(
30+
self::ERROR_FROM_CODE,
31+
self::ERROR_FROM_TEMPLATE,
32+
['from' => $from->format('c'), 'given' => $datetime->format('c')]
33+
)
34+
);
3535
});
3636
}
3737

3838
public function to(\DateTimeImmutable $to): static
3939
{
4040
return $this->postParse(static function (\DateTimeImmutable $datetime) use ($to) {
41-
if ($datetime > $to) {
42-
throw new ErrorsException(
43-
new Error(
44-
self::ERROR_TO_CODE,
45-
self::ERROR_TO_TEMPLATE,
46-
['to' => $to->format('c'), 'given' => $datetime->format('c')]
47-
)
48-
);
41+
if ($datetime <= $to) {
42+
return $datetime;
4943
}
5044

51-
return $datetime;
45+
throw new ErrorsException(
46+
new Error(
47+
self::ERROR_TO_CODE,
48+
self::ERROR_TO_TEMPLATE,
49+
['to' => $to->format('c'), 'given' => $datetime->format('c')]
50+
)
51+
);
5252
});
5353
}
5454

@@ -74,16 +74,16 @@ public function toString(): StringSchema
7474

7575
protected function innerParse(mixed $input): mixed
7676
{
77-
if (!$input instanceof \DateTimeInterface) {
78-
throw new ErrorsException(
79-
new Error(
80-
self::ERROR_TYPE_CODE,
81-
self::ERROR_TYPE_TEMPLATE,
82-
['given' => $this->getDataType($input)]
83-
)
84-
);
77+
if ($input instanceof \DateTimeInterface) {
78+
return $input;
8579
}
8680

87-
return $input;
81+
throw new ErrorsException(
82+
new Error(
83+
self::ERROR_TYPE_CODE,
84+
self::ERROR_TYPE_TEMPLATE,
85+
['given' => $this->getDataType($input)]
86+
)
87+
);
8888
}
8989
}

tests/Unit/Schema/DateTimeSchemaTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function testImmutability(): void
2525
self::assertNotSame($schema, $schema->preParse(static fn (mixed $input) => $input));
2626
self::assertNotSame($schema, $schema->postParse(static fn (\DateTimeInterface $output) => $output));
2727
self::assertNotSame($schema, $schema->catch(static fn (\DateTimeInterface $output, ErrorsException $e) => $output));
28+
self::assertNotSame($schema, $schema->from(new \DateTimeImmutable('2024-01-20T09:15:00+00:00')));
29+
self::assertNotSame($schema, $schema->to(new \DateTimeImmutable('2024-01-20T09:15:00+00:00')));
2830
}
2931

3032
public function testParseSuccess(): void

0 commit comments

Comments
 (0)