Skip to content

Commit 24928c9

Browse files
committed
Mapper: replace $path with MapperContext
1 parent ae27ab2 commit 24928c9

File tree

122 files changed

+689
-595
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+689
-595
lines changed

src/Compiler/Mapper/Array/MapArray.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder;
1313
use ShipMonk\InputMapper\Compiler\Type\PhpDocTypeUtils;
1414
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
15+
use ShipMonk\InputMapper\Runtime\MapperContext;
1516

1617
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
1718
class MapArray implements MapperCompiler
@@ -24,21 +25,21 @@ public function __construct(
2425
{
2526
}
2627

27-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
28+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
2829
{
2930
[$keyVariableName, $valueVariableName, $mappedVariableName] = $builder->uniqVariableNames('key', 'value', 'mapped');
3031

31-
$itemPath = $builder->arrayImmutableAppend($path, $builder->var($keyVariableName));
32-
$itemKeyMapper = $this->keyMapperCompiler->compile($builder->var($keyVariableName), $itemPath, $builder);
33-
$itemValueMapper = $this->valueMapperCompiler->compile($builder->var($valueVariableName), $itemPath, $builder);
32+
$itemContext = $builder->new($builder->importClass(MapperContext::class), [$context, $builder->var($keyVariableName)]);
33+
$itemKeyMapper = $this->keyMapperCompiler->compile($builder->var($keyVariableName), $itemContext, $builder);
34+
$itemValueMapper = $this->valueMapperCompiler->compile($builder->var($valueVariableName), $itemContext, $builder);
3435

3536
$statements = [
3637
$builder->if($builder->not($builder->funcCall($builder->importFunction('is_array'), [$value])), [
3738
$builder->throw(
3839
$builder->staticCall(
3940
$builder->importClass(MappingFailedException::class),
4041
'incorrectType',
41-
[$value, $path, $builder->val('array')],
42+
[$value, $context, $builder->val('array')],
4243
),
4344
),
4445
]),

src/Compiler/Mapper/Array/MapArrayShape.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ShipMonk\InputMapper\Compiler\Mapper\MapperCompiler;
1515
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder;
1616
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
17+
use ShipMonk\InputMapper\Runtime\MapperContext;
1718
use function array_fill_keys;
1819
use function array_map;
1920
use function array_push;
@@ -33,7 +34,7 @@ public function __construct(
3334
{
3435
}
3536

36-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
37+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
3738
{
3839
$statements = [];
3940
$mappedVariableName = $builder->uniqVariableName('mapped');
@@ -44,7 +45,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
4445
$builder->staticCall(
4546
$builder->importClass(MappingFailedException::class),
4647
'incorrectType',
47-
[$value, $path, $builder->val('array')],
48+
[$value, $context, $builder->val('array')],
4849
),
4950
),
5051
]);
@@ -56,14 +57,17 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
5657
$isMissing = $builder->not($isPresent);
5758

5859
$itemValue = $builder->arrayDimFetch($value, $builder->val($itemMapping->key));
59-
$itemPath = $builder->arrayImmutableAppend($path, $builder->val($itemMapping->key));
60+
$itemContext = $builder->new($builder->importClass(MapperContext::class), [
61+
$context,
62+
$builder->val($itemMapping->key),
63+
]);
6064
$itemMapperMethodName = $builder->uniqMethodName('map' . ucfirst($itemMapping->key));
6165
$itemMapperMethod = $builder->mapperMethod($itemMapperMethodName, $itemMapping->mapper)->makePrivate()->getNode();
6266
$builder->addMethod($itemMapperMethod);
6367

6468
$itemAssignment = $builder->assign(
6569
$builder->arrayDimFetch($builder->var($mappedVariableName), $builder->val($itemMapping->key)),
66-
$builder->methodCall($builder->var('this'), $itemMapperMethodName, [$itemValue, $itemPath]),
70+
$builder->methodCall($builder->var('this'), $itemMapperMethodName, [$itemValue, $itemContext]),
6771
);
6872

6973
if ($itemMapping->optional) {
@@ -75,7 +79,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
7579
$builder->staticCall(
7680
$builder->importClass(MappingFailedException::class),
7781
'missingKey',
78-
[$path, $builder->val($itemMapping->key)],
82+
[$context, $builder->val($itemMapping->key)],
7983
),
8084
),
8185
]);
@@ -85,7 +89,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
8589
}
8690

8791
if ($this->sealed) {
88-
array_push($statements, ...$this->checkForExtraKeys($value, $path, $builder));
92+
array_push($statements, ...$this->checkForExtraKeys($value, $context, $builder));
8993
}
9094

9195
return new CompiledExpr($builder->var($mappedVariableName), $statements);
@@ -114,7 +118,7 @@ public function getOutputType(): TypeNode
114118
/**
115119
* @return list<Stmt>
116120
*/
117-
private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $builder): array
121+
private function checkForExtraKeys(Expr $value, Expr $context, PhpCodeBuilder $builder): array
118122
{
119123
$statements = [];
120124

@@ -131,7 +135,7 @@ private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $buil
131135
$builder->staticCall(
132136
$builder->importClass(MappingFailedException::class),
133137
'extraKeys',
134-
[$path, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
138+
[$context, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
135139
),
136140
),
137141
]);

src/Compiler/Mapper/Array/MapList.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use ShipMonk\InputMapper\Compiler\Mapper\MapperCompiler;
1212
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder;
1313
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
14+
use ShipMonk\InputMapper\Runtime\MapperContext;
1415

1516
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
1617
class MapList implements MapperCompiler
@@ -22,13 +23,13 @@ public function __construct(
2223
{
2324
}
2425

25-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
26+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
2627
{
2728
[$indexVariableName, $itemVariableName, $mappedVariableName] = $builder->uniqVariableNames('index', 'item', 'mapped');
2829

2930
$itemValue = $builder->var($itemVariableName);
30-
$itemPath = $builder->arrayImmutableAppend($path, $builder->var($indexVariableName));
31-
$itemMapper = $this->itemMapperCompiler->compile($itemValue, $itemPath, $builder);
31+
$itemContext = $builder->new($builder->importClass(MapperContext::class), [$context, $builder->var($indexVariableName)]);
32+
$itemMapper = $this->itemMapperCompiler->compile($itemValue, $itemContext, $builder);
3233

3334
$isArray = $builder->funcCall($builder->importFunction('is_array'), [$value]);
3435
$isList = $builder->funcCall($builder->importFunction('array_is_list'), [$value]);
@@ -39,7 +40,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
3940
$builder->staticCall(
4041
$builder->importClass(MappingFailedException::class),
4142
'incorrectType',
42-
[$value, $path, $builder->val('list')],
43+
[$value, $context, $builder->val('list')],
4344
),
4445
),
4546
]),

src/Compiler/Mapper/MapRuntime.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
use ShipMonk\InputMapper\Compiler\CompiledExpr;
77
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder;
88
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
9+
use ShipMonk\InputMapper\Runtime\MapperContext;
910

1011
abstract class MapRuntime implements MapperCompiler
1112
{
1213

1314
/**
14-
* @param list<string|int> $path
1515
* @throws MappingFailedException
1616
*/
17-
abstract public static function mapValue(mixed $value, array $path): mixed;
17+
abstract public static function mapValue(mixed $value, ?MapperContext $context): mixed;
1818

1919
public function compile(
2020
Expr $value,
21-
Expr $path,
21+
Expr $context,
2222
PhpCodeBuilder $builder,
2323
): CompiledExpr
2424
{
2525
return new CompiledExpr(
2626
$builder->staticCall(
2727
$builder->importClass(static::class),
2828
'mapValue',
29-
[$value, $path],
29+
[$value, $context],
3030
),
3131
);
3232
}

src/Compiler/Mapper/MapperCompiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface MapperCompiler
1414
/**
1515
* @throws CannotCompileMapperException
1616
*/
17-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr;
17+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr;
1818

1919
public function getInputType(): TypeNode;
2020

src/Compiler/Mapper/Mixed/MapMixed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class MapMixed implements MapperCompiler
1515
{
1616

17-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
17+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
1818
{
1919
return new CompiledExpr($value);
2020
}

src/Compiler/Mapper/Object/DelegateMapperCompiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public function __construct(
2121
{
2222
}
2323

24-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
24+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
2525
{
2626
$shortName = $builder->importClass($this->className);
2727
$provider = $builder->propertyFetch($builder->var('this'), 'provider');
2828
$mapper = $builder->methodCall($provider, 'get', [$builder->classConstFetch($shortName, 'class')]);
29-
$mapped = $builder->methodCall($mapper, 'map', [$value, $path]);
29+
$mapped = $builder->methodCall($mapper, 'map', [$value, $context]);
3030

3131
return new CompiledExpr($mapped);
3232
}

src/Compiler/Mapper/Object/MapDateTimeImmutable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
{
3636
}
3737

38-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
38+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
3939
{
4040
$mappedVariableName = $builder->uniqVariableName('mapped');
4141
$timezoneVariableName = $builder->uniqVariableName('timezone');
@@ -60,7 +60,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
6060
$builder->staticCall(
6161
$builder->importClass(MappingFailedException::class),
6262
'incorrectType',
63-
[$value, $path, $builder->val('string')],
63+
[$value, $context, $builder->val('string')],
6464
),
6565
),
6666
]),
@@ -97,7 +97,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
9797
$builder->staticCall(
9898
$builder->importClass(MappingFailedException::class),
9999
'incorrectValue',
100-
[$value, $path, $this->formatDescription],
100+
[$value, $context, $this->formatDescription],
101101
),
102102
),
103103
]);

src/Compiler/Mapper/Object/MapEnum.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function __construct(
2626
{
2727
}
2828

29-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
29+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
3030
{
31-
$backingValueMapper = $this->backingValueMapperCompiler->compile($value, $path, $builder);
31+
$backingValueMapper = $this->backingValueMapperCompiler->compile($value, $context, $builder);
3232
$statements = $backingValueMapper->statements;
3333

3434
$enumOrNull = $builder->staticCall($builder->importClass($this->enumName), 'tryFrom', [$backingValueMapper->expr]);
@@ -51,7 +51,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
5151
$builder->staticCall(
5252
$builder->importClass(MappingFailedException::class),
5353
'incorrectValue',
54-
[$value, $path, $expectedDescription],
54+
[$value, $context, $expectedDescription],
5555
),
5656
),
5757
]);

src/Compiler/Mapper/Object/MapObject.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ShipMonk\InputMapper\Compiler\Mapper\UndefinedAwareMapperCompiler;
1313
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder;
1414
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
15+
use ShipMonk\InputMapper\Runtime\MapperContext;
1516
use function array_fill_keys;
1617
use function array_keys;
1718
use function array_push;
@@ -37,15 +38,15 @@ public function __construct(
3738
{
3839
}
3940

40-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
41+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
4142
{
4243
$statements = [
4344
$builder->if($builder->not($builder->funcCall($builder->importFunction('is_array'), [$value])), [
4445
$builder->throw(
4546
$builder->staticCall(
4647
$builder->importClass(MappingFailedException::class),
4748
'incorrectType',
48-
[$value, $path, $builder->val('array')],
49+
[$value, $context, $builder->val('array')],
4950
),
5051
),
5152
]),
@@ -58,15 +59,18 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
5859
$isMissing = $builder->not($isPresent);
5960

6061
$propertyValue = $builder->arrayDimFetch($value, $builder->val($key));
61-
$propertyPath = $builder->arrayImmutableAppend($path, $builder->val($key));
62+
$propertyContext = $builder->new($builder->importClass(MapperContext::class), [
63+
$context,
64+
$builder->val($key),
65+
]);
6266
$propertyMapperMethodName = $builder->uniqMethodName('map' . ucfirst($key));
6367
$propertyMapperMethod = $builder->mapperMethod($propertyMapperMethodName, $argMapperCompiler)->makePrivate()->getNode();
64-
$propertyMapperCall = $builder->methodCall($builder->var('this'), $propertyMapperMethodName, [$propertyValue, $propertyPath]);
68+
$propertyMapperCall = $builder->methodCall($builder->var('this'), $propertyMapperMethodName, [$propertyValue, $propertyContext]);
6569
$builder->addMethod($propertyMapperMethod);
6670

6771
if ($argMapperCompiler instanceof UndefinedAwareMapperCompiler) {
6872
$propertyValueVarName = $builder->uniqVariableName($key);
69-
$fallbackValueMapper = $argMapperCompiler->compileUndefined($path, $builder->val($key), $builder);
73+
$fallbackValueMapper = $argMapperCompiler->compileUndefined($context, $builder->val($key), $builder);
7074

7175
if (count($fallbackValueMapper->statements) > 0) {
7276
$statements[] = $builder->if(
@@ -88,7 +92,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
8892
$builder->staticCall(
8993
$builder->importClass(MappingFailedException::class),
9094
'missingKey',
91-
[$path, $key],
95+
[$context, $key],
9296
),
9397
),
9498
]);
@@ -98,7 +102,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
98102
}
99103

100104
if (!$this->allowExtraKeys) {
101-
array_push($statements, ...$this->checkForExtraKeys($value, $path, $builder));
105+
array_push($statements, ...$this->checkForExtraKeys($value, $context, $builder));
102106
}
103107

104108
return new CompiledExpr(
@@ -120,7 +124,7 @@ public function getOutputType(): TypeNode
120124
/**
121125
* @return list<Stmt>
122126
*/
123-
private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $builder): array
127+
private function checkForExtraKeys(Expr $value, Expr $context, PhpCodeBuilder $builder): array
124128
{
125129
$statements = [];
126130

@@ -137,7 +141,7 @@ private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $buil
137141
$builder->staticCall(
138142
$builder->importClass(MappingFailedException::class),
139143
'extraKeys',
140-
[$path, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
144+
[$context, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
141145
),
142146
),
143147
]);

src/Compiler/Mapper/Scalar/MapBool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
class MapBool implements MapperCompiler
1616
{
1717

18-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
18+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
1919
{
2020
$statements = [
2121
$builder->if($builder->not($builder->funcCall($builder->importFunction('is_bool'), [$value])), [
2222
$builder->throw(
2323
$builder->staticCall(
2424
$builder->importClass(MappingFailedException::class),
2525
'incorrectType',
26-
[$value, $path, $builder->val('bool')],
26+
[$value, $context, $builder->val('bool')],
2727
),
2828
),
2929
]),

0 commit comments

Comments
 (0)