Skip to content

Commit 0d6c831

Browse files
committed
make exclusive*( their own function
1 parent 35b3d1f commit 0d6c831

7 files changed

Lines changed: 191 additions & 111 deletions

File tree

doc/Schema/FloatSchema.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ $data = $schema->parse(4.2); // Returns: 4.2
2020

2121
```php
2222
$schema->minimum(5.0); // Greater than or equal to 5.0
23-
$schema->minimum(5.0, true); // Greater than 5.0
24-
$schema->maximum(10.0, true); // Less than 10.0
23+
$schema->exclusiveMinimum(5.0); // Greater than 5.0
24+
$schema->exclusiveMaximum(10.0); // Less than 10.0
2525
$schema->maximum(10.0); // Less than or equal to 10.0
2626
```
2727

@@ -83,5 +83,7 @@ $coordinatesSchema->parse(['lat' => 47.1, 'lng' => 8.2]);
8383
|------|-------------|
8484
| `float.type` | Value is not a float |
8585
| `float.minimum` | Value is not greater than or equal to threshold |
86+
| `float.exclusiveMinimum` | Value is not greater than to threshold |
87+
| `float.exclusiveMaximum` | Value is not less than to threshold |
8688
| `float.maximum` | Value is not less than or equal to threshold |
8789
| `float.int` | Cannot convert float to int without precision loss (for `toInt()`) |

doc/Schema/IntSchema.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ $data = $schema->parse(1337); // Returns: 1337
2020

2121
```php
2222
$schema->minimum(5); // Greater than or equal to 5
23-
$schema->minimum(5, true); // Greater than 5
24-
$schema->maximum(10, true); // Less than 10
23+
$schema->exclusiveMinimum(5); // Greater than 5
24+
$schema->exclusiveMaximum(10); // Less than 10
2525
$schema->maximum(10); // Less than or equal to 10
2626
```
2727

@@ -91,4 +91,6 @@ $date = $timestampSchema->parse(1705744500);
9191
|------|-------------|
9292
| `int.type` | Value is not an integer |
9393
| `int.minimum` | Value is not greater than or equal to threshold |
94-
| `int.maximum` | Value is not less than or equal to threshold |
94+
| `int.exclusiveMinimum` | Value is not greater than to threshold |
95+
| `int.exclusiveMaximum` | Value is not less than to threshold |
96+
| `int.maximum` | Value is not less than or equal to threshold |

src/Schema/FloatSchema.php

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,33 @@ final class FloatSchema extends AbstractSchemaInnerParse implements SchemaInterf
1313
public const string ERROR_TYPE_TEMPLATE = 'Type should be "float", {{given}} given';
1414

1515
public const string ERROR_MINIMUM_CODE = 'float.minimum';
16-
public const string ERROR_MINIMUM_TEMPLATE = 'Value should be minimum {{minimum}} {{exclusiveMinimum}}, {{given}} given';
16+
public const string ERROR_MINIMUM_TEMPLATE = 'Value should be minimum {{minimum}}, {{given}} given';
17+
18+
public const string ERROR_EXCLUSIVE_MINIMUM_CODE = 'float.exclusiveMinimum';
19+
public const string ERROR_EXCLUSIVE_MINIMUM_TEMPLATE = 'Value should be greater than {{exclusiveMinimum}}, {{given}} given';
20+
21+
public const string ERROR_EXCLUSIVE_MAXIMUM_CODE = 'float.exclusiveMaximum';
22+
public const string ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE = 'Value should be lesser than {{exclusiveMaximum}}, {{given}} given';
1723

1824
public const string ERROR_MAXIMUM_CODE = 'float.maximum';
19-
public const string ERROR_MAXIMUM_TEMPLATE = 'Value should be maximum {{maximum}} {{exclusiveMaximum}}, {{given}} given';
25+
public const string ERROR_MAXIMUM_TEMPLATE = 'Value should be maximum {{maximum}}, {{given}} given';
2026

2127
/** @deprecated: see ERROR_MINIMUM_CODE */
2228
public const string ERROR_GTE_CODE = 'float.gte';
2329

2430
/** @deprecated: see ERROR_MINIMUM_TEMPLATE */
2531
public const string ERROR_GTE_TEMPLATE = 'Value should be greater than or equal {{gte}}, {{given}} given';
2632

27-
/** @deprecated: see ERROR_MINIMUM_CODE */
33+
/** @deprecated: see ERROR_EXCLUSIVE_MINIMUM_CODE */
2834
public const string ERROR_GT_CODE = 'float.gt';
2935

30-
/** @deprecated: see ERROR_MINIMUM_TEMPLATE */
36+
/** @deprecated: see ERROR_EXCLUSIVE_MINIMUM_TEMPLATE */
3137
public const string ERROR_GT_TEMPLATE = 'Value should be greater than {{gt}}, {{given}} given';
3238

33-
/** @deprecated: see ERROR_MAXIMUM_CODE */
39+
/** @deprecated: see ERROR_EXCLUSIVE_MAXIMUM_CODE */
3440
public const string ERROR_LT_CODE = 'float.lt';
3541

36-
/** @deprecated: see ERROR_MAXIMUM_TEMPLATE */
42+
/** @deprecated: see ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE */
3743
public const string ERROR_LT_TEMPLATE = 'Value should be lesser than {{lt}}, {{given}} given';
3844

3945
/** @deprecated: see ERROR_MAXIMUM_CODE */
@@ -45,35 +51,69 @@ final class FloatSchema extends AbstractSchemaInnerParse implements SchemaInterf
4551
public const string ERROR_INT_CODE = 'float.int';
4652
public const string ERROR_INT_TEMPLATE = 'Cannot convert {{given}} to int';
4753

48-
public function minimum(float $minimum, bool $exclusiveMinimum = false): static
54+
public function minimum(float $minimum): static
4955
{
50-
return $this->postParse(static function (float $float) use ($minimum, $exclusiveMinimum) {
51-
if ((!$exclusiveMinimum && $float >= $minimum) || ($exclusiveMinimum && $float > $minimum)) {
56+
return $this->postParse(static function (float $float) use ($minimum) {
57+
if ($float >= $minimum) {
5258
return $float;
5359
}
5460

5561
throw new ErrorsException(
5662
new Error(
5763
self::ERROR_MINIMUM_CODE,
5864
self::ERROR_MINIMUM_TEMPLATE,
59-
['minimum' => $minimum, 'exclusiveMinimum' => $exclusiveMinimum, 'given' => $float]
65+
['minimum' => $minimum, 'given' => $float]
66+
)
67+
);
68+
});
69+
}
70+
71+
public function exclusiveMinimum(float $exclusiveMinimum): static
72+
{
73+
return $this->postParse(static function (float $float) use ($exclusiveMinimum) {
74+
if ($float > $exclusiveMinimum) {
75+
return $float;
76+
}
77+
78+
throw new ErrorsException(
79+
new Error(
80+
self::ERROR_EXCLUSIVE_MINIMUM_CODE,
81+
self::ERROR_EXCLUSIVE_MINIMUM_TEMPLATE,
82+
['exclusiveMinimum' => $exclusiveMinimum, 'given' => $float]
83+
)
84+
);
85+
});
86+
}
87+
88+
public function exclusiveMaximum(float $exclusiveMaximum): static
89+
{
90+
return $this->postParse(static function (float $float) use ($exclusiveMaximum) {
91+
if ($float < $exclusiveMaximum) {
92+
return $float;
93+
}
94+
95+
throw new ErrorsException(
96+
new Error(
97+
self::ERROR_EXCLUSIVE_MAXIMUM_CODE,
98+
self::ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE,
99+
['exclusiveMaximum' => $exclusiveMaximum, 'given' => $float]
60100
)
61101
);
62102
});
63103
}
64104

65-
public function maximum(float $maximum, bool $exclusiveMaximum = false): static
105+
public function maximum(float $maximum): static
66106
{
67-
return $this->postParse(static function (float $float) use ($maximum, $exclusiveMaximum) {
68-
if ((!$exclusiveMaximum && $float <= $maximum) || ($exclusiveMaximum && $float < $maximum)) {
107+
return $this->postParse(static function (float $float) use ($maximum) {
108+
if ($float <= $maximum) {
69109
return $float;
70110
}
71111

72112
throw new ErrorsException(
73113
new Error(
74114
self::ERROR_MAXIMUM_CODE,
75115
self::ERROR_MAXIMUM_TEMPLATE,
76-
['maximum' => $maximum, 'exclusiveMaximum' => $exclusiveMaximum, 'given' => $float]
116+
['maximum' => $maximum, 'given' => $float]
77117
)
78118
);
79119
});
@@ -102,11 +142,11 @@ public function gte(float $gte): static
102142
}
103143

104144
/**
105-
* @deprecated Use minimum($gt, true) instead
145+
* @deprecated Use exclusiveMinimum($gt) instead
106146
*/
107147
public function gt(float $gt): static
108148
{
109-
@trigger_error('Use minimum('.$this->varExport($gt).', true) instead', E_USER_DEPRECATED);
149+
@trigger_error('Use exclusiveMinimum('.$this->varExport($gt).') instead', E_USER_DEPRECATED);
110150

111151
return $this->postParse(static function (float $float) use ($gt) {
112152
if ($float > $gt) {
@@ -124,11 +164,11 @@ public function gt(float $gt): static
124164
}
125165

126166
/**
127-
* @deprecated Use maximum($lt, true) instead
167+
* @deprecated Use exclusiveMaximum($lt) instead
128168
*/
129169
public function lt(float $lt): static
130170
{
131-
@trigger_error('Use maximum('.$this->varExport($lt).', true) instead', E_USER_DEPRECATED);
171+
@trigger_error('Use exclusiveMaximum('.$this->varExport($lt).') instead', E_USER_DEPRECATED);
132172

133173
return $this->postParse(static function (float $float) use ($lt) {
134174
if ($float < $lt) {

src/Schema/IntSchema.php

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,33 @@ final class IntSchema extends AbstractSchemaInnerParse implements SchemaInterfac
1313
public const string ERROR_TYPE_TEMPLATE = 'Type should be "int", {{given}} given';
1414

1515
public const string ERROR_MINIMUM_CODE = 'int.minimum';
16-
public const string ERROR_MINIMUM_TEMPLATE = 'Value should be minimum {{minimum}} {{exclusiveMinimum}}, {{given}} given';
16+
public const string ERROR_MINIMUM_TEMPLATE = 'Value should be minimum {{minimum}}, {{given}} given';
17+
18+
public const string ERROR_EXCLUSIVE_MINIMUM_CODE = 'int.exclusiveMinimum';
19+
public const string ERROR_EXCLUSIVE_MINIMUM_TEMPLATE = 'Value should be greater than {{exclusiveMinimum}}, {{given}} given';
20+
21+
public const string ERROR_EXCLUSIVE_MAXIMUM_CODE = 'int.exclusiveMaximum';
22+
public const string ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE = 'Value should be lesser than {{exclusiveMaximum}}, {{given}} given';
1723

1824
public const string ERROR_MAXIMUM_CODE = 'int.maximum';
19-
public const string ERROR_MAXIMUM_TEMPLATE = 'Value should be maximum {{maximum}} {{exclusiveMaximum}}, {{given}} given';
25+
public const string ERROR_MAXIMUM_TEMPLATE = 'Value should be maximum {{maximum}}, {{given}} given';
2026

2127
/** @deprecated: see ERROR_MINIMUM_CODE */
2228
public const string ERROR_GTE_CODE = 'int.gte';
2329

2430
/** @deprecated: see ERROR_MINIMUM_TEMPLATE */
2531
public const string ERROR_GTE_TEMPLATE = 'Value should be greater than or equal {{gte}}, {{given}} given';
2632

27-
/** @deprecated: see ERROR_MINIMUM_CODE */
33+
/** @deprecated: see ERROR_EXCLUSIVE_MINIMUM_CODE */
2834
public const string ERROR_GT_CODE = 'int.gt';
2935

30-
/** @deprecated: see ERROR_MINIMUM_TEMPLATE */
36+
/** @deprecated: see ERROR_EXCLUSIVE_MINIMUM_TEMPLATE */
3137
public const string ERROR_GT_TEMPLATE = 'Value should be greater than {{gt}}, {{given}} given';
3238

33-
/** @deprecated: see ERROR_MAXIMUM_CODE */
39+
/** @deprecated: see ERROR_EXCLUSIVE_MAXIMUM_CODE */
3440
public const string ERROR_LT_CODE = 'int.lt';
3541

36-
/** @deprecated: see ERROR_MAXIMUM_TEMPLATE */
42+
/** @deprecated: see ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE */
3743
public const string ERROR_LT_TEMPLATE = 'Value should be lesser than {{lt}}, {{given}} given';
3844

3945
/** @deprecated: see ERROR_MAXIMUM_CODE */
@@ -42,35 +48,69 @@ final class IntSchema extends AbstractSchemaInnerParse implements SchemaInterfac
4248
/** @deprecated: see ERROR_MAXIMUM_TEMPLATE */
4349
public const string ERROR_LTE_TEMPLATE = 'Value should be lesser than or equal {{lte}}, {{given}} given';
4450

45-
public function minimum(int $minimum, bool $exclusiveMinimum = false): static
51+
public function minimum(int $minimum): static
4652
{
47-
return $this->postParse(static function (int $int) use ($minimum, $exclusiveMinimum) {
48-
if ((!$exclusiveMinimum && $int >= $minimum) || ($exclusiveMinimum && $int > $minimum)) {
53+
return $this->postParse(static function (int $int) use ($minimum) {
54+
if ($int >= $minimum) {
4955
return $int;
5056
}
5157

5258
throw new ErrorsException(
5359
new Error(
5460
self::ERROR_MINIMUM_CODE,
5561
self::ERROR_MINIMUM_TEMPLATE,
56-
['minimum' => $minimum, 'exclusiveMinimum' => $exclusiveMinimum, 'given' => $int]
62+
['minimum' => $minimum, 'given' => $int]
63+
)
64+
);
65+
});
66+
}
67+
68+
public function exclusiveMinimum(int $exclusiveMinimum): static
69+
{
70+
return $this->postParse(static function (int $int) use ($exclusiveMinimum) {
71+
if ($int > $exclusiveMinimum) {
72+
return $int;
73+
}
74+
75+
throw new ErrorsException(
76+
new Error(
77+
self::ERROR_EXCLUSIVE_MINIMUM_CODE,
78+
self::ERROR_EXCLUSIVE_MINIMUM_TEMPLATE,
79+
['exclusiveMinimum' => $exclusiveMinimum, 'given' => $int]
80+
)
81+
);
82+
});
83+
}
84+
85+
public function exclusiveMaximum(int $exclusiveMaximum): static
86+
{
87+
return $this->postParse(static function (int $int) use ($exclusiveMaximum) {
88+
if ($int < $exclusiveMaximum) {
89+
return $int;
90+
}
91+
92+
throw new ErrorsException(
93+
new Error(
94+
self::ERROR_EXCLUSIVE_MAXIMUM_CODE,
95+
self::ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE,
96+
['exclusiveMaximum' => $exclusiveMaximum, 'given' => $int]
5797
)
5898
);
5999
});
60100
}
61101

62-
public function maximum(int $maximum, bool $exclusiveMaximum = false): static
102+
public function maximum(int $maximum): static
63103
{
64-
return $this->postParse(static function (int $int) use ($maximum, $exclusiveMaximum) {
65-
if ((!$exclusiveMaximum && $int <= $maximum) || ($exclusiveMaximum && $int < $maximum)) {
104+
return $this->postParse(static function (int $int) use ($maximum) {
105+
if ($int <= $maximum) {
66106
return $int;
67107
}
68108

69109
throw new ErrorsException(
70110
new Error(
71111
self::ERROR_MAXIMUM_CODE,
72112
self::ERROR_MAXIMUM_TEMPLATE,
73-
['maximum' => $maximum, 'exclusiveMaximum' => $exclusiveMaximum, 'given' => $int]
113+
['maximum' => $maximum, 'given' => $int]
74114
)
75115
);
76116
});
@@ -99,11 +139,11 @@ public function gte(int $gte): static
99139
}
100140

101141
/**
102-
* @deprecated Use minimum($gt, true) instead
142+
* @deprecated Use exclusiveMinimum($gt) instead
103143
*/
104144
public function gt(int $gt): static
105145
{
106-
@trigger_error('Use minimum('.$this->varExport($gt).', true) instead', E_USER_DEPRECATED);
146+
@trigger_error('Use exclusiveMinimum('.$this->varExport($gt).') instead', E_USER_DEPRECATED);
107147

108148
return $this->postParse(static function (int $int) use ($gt) {
109149
if ($int > $gt) {
@@ -121,11 +161,11 @@ public function gt(int $gt): static
121161
}
122162

123163
/**
124-
* @deprecated Use maximum($lt, true) instead
164+
* @deprecated Use exclusiveMaximum($lt) instead
125165
*/
126166
public function lt(int $lt): static
127167
{
128-
@trigger_error('Use maximum('.$this->varExport($lt).', true) instead', E_USER_DEPRECATED);
168+
@trigger_error('Use exclusiveMaximum('.$this->varExport($lt).') instead', E_USER_DEPRECATED);
129169

130170
return $this->postParse(static function (int $int) use ($lt) {
131171
if ($int < $lt) {

0 commit comments

Comments
 (0)