Skip to content

Commit f8c466f

Browse files
committed
add support for non-negative-int and non-positive-int in phpDoc
1 parent 70b3025 commit f8c466f

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/Compiler/MapperFactory/DefaultMapperCompilerFactory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\ValidatedMapperCompiler;
5151
use ShipMonk\InputMapper\Compiler\Type\PhpDocTypeUtils;
5252
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertIntRange;
53+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertNegativeInt;
54+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertNonNegativeInt;
55+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertNonPositiveInt;
56+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertPositiveInt;
5357
use ShipMonk\InputMapper\Compiler\Validator\ValidatorCompiler;
5458
use ShipMonk\InputMapper\Runtime\Optional;
5559
use function class_exists;
@@ -116,8 +120,10 @@ public function create(TypeNode $type, array $options = []): MapperCompiler
116120

117121
default => match ($type->name) {
118122
'list' => new MapList(new MapMixed()),
119-
'negative-int' => new ValidatedMapperCompiler(new MapInt(), [new AssertIntRange(lt: 0)]),
120-
'positive-int' => new ValidatedMapperCompiler(new MapInt(), [new AssertIntRange(gt: 0)]),
123+
'negative-int' => new ValidatedMapperCompiler(new MapInt(), [new AssertNegativeInt()]),
124+
'non-negative-int' => new ValidatedMapperCompiler(new MapInt(), [new AssertNonNegativeInt()]),
125+
'non-positive-int' => new ValidatedMapperCompiler(new MapInt(), [new AssertNonPositiveInt()]),
126+
'positive-int' => new ValidatedMapperCompiler(new MapInt(), [new AssertPositiveInt()]),
121127
default => throw CannotCreateMapperCompilerException::fromType($type),
122128
},
123129
};

tests/Compiler/MapperFactory/Data/BrandInput.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
class BrandInput
99
{
1010

11+
/**
12+
* @param int<1900, 2100> $foundedIn
13+
*/
1114
public function __construct(
1215
public readonly string $name,
16+
public readonly int $foundedIn,
1317
)
1418
{
1519
}

tests/Compiler/MapperFactory/DefaultMapperCompilerFactoryTest.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\ValidatedMapperCompiler;
3333
use ShipMonk\InputMapper\Compiler\MapperFactory\DefaultMapperCompilerFactory;
3434
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertIntRange;
35+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertNegativeInt;
36+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertNonNegativeInt;
37+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertNonPositiveInt;
3538
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertPositiveInt;
3639
use ShipMonk\InputMapper\Compiler\Validator\String\AssertStringLength;
3740
use ShipMonk\InputMapper\Compiler\Validator\String\AssertUrl;
@@ -106,7 +109,13 @@ public static function provideCreateOkData(): iterable
106109
[],
107110
new MapObject(
108111
BrandInput::class,
109-
['name' => new MapString()],
112+
[
113+
'name' => new MapString(),
114+
'foundedIn' => new ValidatedMapperCompiler(
115+
new MapInt(),
116+
[new AssertIntRange(gte: 1_900, lte: 2_100)],
117+
),
118+
],
110119
allowExtraKeys: true,
111120
),
112121
];
@@ -240,6 +249,38 @@ public static function provideCreateOkData(): iterable
240249
]),
241250
];
242251

252+
yield 'positive-int' => [
253+
'positive-int',
254+
[],
255+
new ValidatedMapperCompiler(new MapInt(), [
256+
new AssertPositiveInt(),
257+
]),
258+
];
259+
260+
yield 'negative-int' => [
261+
'negative-int',
262+
[],
263+
new ValidatedMapperCompiler(new MapInt(), [
264+
new AssertNegativeInt(),
265+
]),
266+
];
267+
268+
yield 'non-positive-int' => [
269+
'non-positive-int',
270+
[],
271+
new ValidatedMapperCompiler(new MapInt(), [
272+
new AssertNonPositiveInt(),
273+
]),
274+
];
275+
276+
yield 'non-negative-int' => [
277+
'non-negative-int',
278+
[],
279+
new ValidatedMapperCompiler(new MapInt(), [
280+
new AssertNonNegativeInt(),
281+
]),
282+
];
283+
243284
yield '?list<string>' => [
244285
'?list<string>',
245286
[],

0 commit comments

Comments
 (0)