Skip to content

Commit 98ea520

Browse files
committed
work less with negation, make the code easier to understand
1 parent f7e75fc commit 98ea520

6 files changed

Lines changed: 338 additions & 338 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
}

src/Schema/FloatSchema.php

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ final class FloatSchema extends AbstractSchemaInnerParse implements SchemaInterf
1212
public const string ERROR_TYPE_CODE = 'float.type';
1313
public const string ERROR_TYPE_TEMPLATE = 'Type should be "float", {{given}} given';
1414

15-
public const string ERROR_GT_CODE = 'float.gt';
16-
public const string ERROR_GT_TEMPLATE = 'Value should be greater than {{gt}}, {{given}} given';
17-
1815
public const string ERROR_GTE_CODE = 'float.gte';
1916
public const string ERROR_GTE_TEMPLATE = 'Value should be greater than or equal {{gte}}, {{given}} given';
2017

18+
public const string ERROR_GT_CODE = 'float.gt';
19+
public const string ERROR_GT_TEMPLATE = 'Value should be greater than {{gt}}, {{given}} given';
20+
2121
public const string ERROR_LT_CODE = 'float.lt';
2222
public const string ERROR_LT_TEMPLATE = 'Value should be lesser than {{lt}}, {{given}} given';
2323

@@ -27,71 +27,71 @@ final class FloatSchema extends AbstractSchemaInnerParse implements SchemaInterf
2727
public const string ERROR_INT_CODE = 'float.int';
2828
public const string ERROR_INT_TEMPLATE = 'Cannot convert {{given}} to int';
2929

30-
public function gt(float $gt): static
30+
public function gte(float $gte): static
3131
{
32-
return $this->postParse(static function (float $float) use ($gt) {
33-
if ($float <= $gt) {
34-
throw new ErrorsException(
35-
new Error(
36-
self::ERROR_GT_CODE,
37-
self::ERROR_GT_TEMPLATE,
38-
['gt' => $gt, 'given' => $float]
39-
)
40-
);
32+
return $this->postParse(static function (float $float) use ($gte) {
33+
if ($float >= $gte) {
34+
return $float;
4135
}
4236

43-
return $float;
37+
throw new ErrorsException(
38+
new Error(
39+
self::ERROR_GTE_CODE,
40+
self::ERROR_GTE_TEMPLATE,
41+
['gte' => $gte, 'given' => $float]
42+
)
43+
);
4444
});
4545
}
4646

47-
public function gte(float $gte): static
47+
public function gt(float $gt): static
4848
{
49-
return $this->postParse(static function (float $float) use ($gte) {
50-
if ($float < $gte) {
51-
throw new ErrorsException(
52-
new Error(
53-
self::ERROR_GTE_CODE,
54-
self::ERROR_GTE_TEMPLATE,
55-
['gte' => $gte, 'given' => $float]
56-
)
57-
);
49+
return $this->postParse(static function (float $float) use ($gt) {
50+
if ($float > $gt) {
51+
return $float;
5852
}
5953

60-
return $float;
54+
throw new ErrorsException(
55+
new Error(
56+
self::ERROR_GT_CODE,
57+
self::ERROR_GT_TEMPLATE,
58+
['gt' => $gt, 'given' => $float]
59+
)
60+
);
6161
});
6262
}
6363

6464
public function lt(float $lt): static
6565
{
6666
return $this->postParse(static function (float $float) use ($lt) {
67-
if ($float >= $lt) {
68-
throw new ErrorsException(
69-
new Error(
70-
self::ERROR_LT_CODE,
71-
self::ERROR_LT_TEMPLATE,
72-
['lt' => $lt, 'given' => $float]
73-
)
74-
);
67+
if ($float < $lt) {
68+
return $float;
7569
}
7670

77-
return $float;
71+
throw new ErrorsException(
72+
new Error(
73+
self::ERROR_LT_CODE,
74+
self::ERROR_LT_TEMPLATE,
75+
['lt' => $lt, 'given' => $float]
76+
)
77+
);
7878
});
7979
}
8080

8181
public function lte(float $lte): static
8282
{
8383
return $this->postParse(static function (float $float) use ($lte) {
84-
if ($float > $lte) {
85-
throw new ErrorsException(
86-
new Error(
87-
self::ERROR_LTE_CODE,
88-
self::ERROR_LTE_TEMPLATE,
89-
['lte' => $lte, 'given' => $float]
90-
)
91-
);
84+
if ($float <= $lte) {
85+
return $float;
9286
}
9387

94-
return $float;
88+
throw new ErrorsException(
89+
new Error(
90+
self::ERROR_LTE_CODE,
91+
self::ERROR_LTE_TEMPLATE,
92+
['lte' => $lte, 'given' => $float]
93+
)
94+
);
9595
});
9696
}
9797

@@ -127,17 +127,17 @@ public function toInt(): IntSchema
127127

128128
$intInput = (int) $input;
129129

130-
if ((float) $intInput !== $input) {
131-
throw new ErrorsException(
132-
new Error(
133-
self::ERROR_INT_CODE,
134-
self::ERROR_INT_TEMPLATE,
135-
['given' => $input]
136-
)
137-
);
130+
if ((float) $intInput === $input) {
131+
return $intInput;
138132
}
139133

140-
return $intInput;
134+
throw new ErrorsException(
135+
new Error(
136+
self::ERROR_INT_CODE,
137+
self::ERROR_INT_TEMPLATE,
138+
['given' => $input]
139+
)
140+
);
141141
})->nullable($this->nullable);
142142
}
143143

@@ -153,16 +153,16 @@ public function toString(): StringSchema
153153

154154
protected function innerParse(mixed $input): mixed
155155
{
156-
if (!\is_float($input)) {
157-
throw new ErrorsException(
158-
new Error(
159-
self::ERROR_TYPE_CODE,
160-
self::ERROR_TYPE_TEMPLATE,
161-
['given' => $this->getDataType($input)]
162-
)
163-
);
156+
if (\is_float($input)) {
157+
return $input;
164158
}
165159

166-
return $input;
160+
throw new ErrorsException(
161+
new Error(
162+
self::ERROR_TYPE_CODE,
163+
self::ERROR_TYPE_TEMPLATE,
164+
['given' => $this->getDataType($input)]
165+
)
166+
);
167167
}
168168
}

0 commit comments

Comments
 (0)