Skip to content

Commit 79678a6

Browse files
committed
Mapper: replace $path with MapperContext
1 parent ae27ab2 commit 79678a6

File tree

122 files changed

+610
-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

+610
-595
lines changed

src/Compiler/Mapper/Array/MapArray.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ public function __construct(
2424
{
2525
}
2626

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

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);
31+
$itemContext = $builder->mapperContextAppend($context, $builder->var($keyVariableName));
32+
$itemKeyMapper = $this->keyMapperCompiler->compile($builder->var($keyVariableName), $itemContext, $builder);
33+
$itemValueMapper = $this->valueMapperCompiler->compile($builder->var($valueVariableName), $itemContext, $builder);
3434

3535
$statements = [
3636
$builder->if($builder->not($builder->funcCall($builder->importFunction('is_array'), [$value])), [
3737
$builder->throw(
3838
$builder->staticCall(
3939
$builder->importClass(MappingFailedException::class),
4040
'incorrectType',
41-
[$value, $path, $builder->val('array')],
41+
[$value, $context, $builder->val('array')],
4242
),
4343
),
4444
]),

src/Compiler/Mapper/Array/MapArrayShape.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333
{
3434
}
3535

36-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
36+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
3737
{
3838
$statements = [];
3939
$mappedVariableName = $builder->uniqVariableName('mapped');
@@ -44,7 +44,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
4444
$builder->staticCall(
4545
$builder->importClass(MappingFailedException::class),
4646
'incorrectType',
47-
[$value, $path, $builder->val('array')],
47+
[$value, $context, $builder->val('array')],
4848
),
4949
),
5050
]);
@@ -56,14 +56,14 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
5656
$isMissing = $builder->not($isPresent);
5757

5858
$itemValue = $builder->arrayDimFetch($value, $builder->val($itemMapping->key));
59-
$itemPath = $builder->arrayImmutableAppend($path, $builder->val($itemMapping->key));
59+
$itemContext = $builder->mapperContextAppend($context, $builder->val($itemMapping->key));
6060
$itemMapperMethodName = $builder->uniqMethodName('map' . ucfirst($itemMapping->key));
6161
$itemMapperMethod = $builder->mapperMethod($itemMapperMethodName, $itemMapping->mapper)->makePrivate()->getNode();
6262
$builder->addMethod($itemMapperMethod);
6363

6464
$itemAssignment = $builder->assign(
6565
$builder->arrayDimFetch($builder->var($mappedVariableName), $builder->val($itemMapping->key)),
66-
$builder->methodCall($builder->var('this'), $itemMapperMethodName, [$itemValue, $itemPath]),
66+
$builder->methodCall($builder->var('this'), $itemMapperMethodName, [$itemValue, $itemContext]),
6767
);
6868

6969
if ($itemMapping->optional) {
@@ -75,7 +75,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
7575
$builder->staticCall(
7676
$builder->importClass(MappingFailedException::class),
7777
'missingKey',
78-
[$path, $builder->val($itemMapping->key)],
78+
[$context, $builder->val($itemMapping->key)],
7979
),
8080
),
8181
]);
@@ -85,7 +85,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
8585
}
8686

8787
if ($this->sealed) {
88-
array_push($statements, ...$this->checkForExtraKeys($value, $path, $builder));
88+
array_push($statements, ...$this->checkForExtraKeys($value, $context, $builder));
8989
}
9090

9191
return new CompiledExpr($builder->var($mappedVariableName), $statements);
@@ -114,7 +114,7 @@ public function getOutputType(): TypeNode
114114
/**
115115
* @return list<Stmt>
116116
*/
117-
private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $builder): array
117+
private function checkForExtraKeys(Expr $value, Expr $context, PhpCodeBuilder $builder): array
118118
{
119119
$statements = [];
120120

@@ -131,7 +131,7 @@ private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $buil
131131
$builder->staticCall(
132132
$builder->importClass(MappingFailedException::class),
133133
'extraKeys',
134-
[$path, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
134+
[$context, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
135135
),
136136
),
137137
]);

src/Compiler/Mapper/Array/MapList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public function __construct(
2222
{
2323
}
2424

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

2929
$itemValue = $builder->var($itemVariableName);
30-
$itemPath = $builder->arrayImmutableAppend($path, $builder->var($indexVariableName));
31-
$itemMapper = $this->itemMapperCompiler->compile($itemValue, $itemPath, $builder);
30+
$itemContext = $builder->mapperContextAppend($context, $builder->var($indexVariableName));
31+
$itemMapper = $this->itemMapperCompiler->compile($itemValue, $itemContext, $builder);
3232

3333
$isArray = $builder->funcCall($builder->importFunction('is_array'), [$value]);
3434
$isList = $builder->funcCall($builder->importFunction('array_is_list'), [$value]);
@@ -39,7 +39,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
3939
$builder->staticCall(
4040
$builder->importClass(MappingFailedException::class),
4141
'incorrectType',
42-
[$value, $path, $builder->val('list')],
42+
[$value, $context, $builder->val('list')],
4343
),
4444
),
4545
]),

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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public function __construct(
3737
{
3838
}
3939

40-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
40+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
4141
{
4242
$statements = [
4343
$builder->if($builder->not($builder->funcCall($builder->importFunction('is_array'), [$value])), [
4444
$builder->throw(
4545
$builder->staticCall(
4646
$builder->importClass(MappingFailedException::class),
4747
'incorrectType',
48-
[$value, $path, $builder->val('array')],
48+
[$value, $context, $builder->val('array')],
4949
),
5050
),
5151
]),
@@ -58,15 +58,15 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
5858
$isMissing = $builder->not($isPresent);
5959

6060
$propertyValue = $builder->arrayDimFetch($value, $builder->val($key));
61-
$propertyPath = $builder->arrayImmutableAppend($path, $builder->val($key));
61+
$propertyContext = $builder->mapperContextAppend($context, $builder->val($key));
6262
$propertyMapperMethodName = $builder->uniqMethodName('map' . ucfirst($key));
6363
$propertyMapperMethod = $builder->mapperMethod($propertyMapperMethodName, $argMapperCompiler)->makePrivate()->getNode();
64-
$propertyMapperCall = $builder->methodCall($builder->var('this'), $propertyMapperMethodName, [$propertyValue, $propertyPath]);
64+
$propertyMapperCall = $builder->methodCall($builder->var('this'), $propertyMapperMethodName, [$propertyValue, $propertyContext]);
6565
$builder->addMethod($propertyMapperMethod);
6666

6767
if ($argMapperCompiler instanceof UndefinedAwareMapperCompiler) {
6868
$propertyValueVarName = $builder->uniqVariableName($key);
69-
$fallbackValueMapper = $argMapperCompiler->compileUndefined($path, $builder->val($key), $builder);
69+
$fallbackValueMapper = $argMapperCompiler->compileUndefined($context, $builder->val($key), $builder);
7070

7171
if (count($fallbackValueMapper->statements) > 0) {
7272
$statements[] = $builder->if(
@@ -88,7 +88,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
8888
$builder->staticCall(
8989
$builder->importClass(MappingFailedException::class),
9090
'missingKey',
91-
[$path, $key],
91+
[$context, $key],
9292
),
9393
),
9494
]);
@@ -98,7 +98,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
9898
}
9999

100100
if (!$this->allowExtraKeys) {
101-
array_push($statements, ...$this->checkForExtraKeys($value, $path, $builder));
101+
array_push($statements, ...$this->checkForExtraKeys($value, $context, $builder));
102102
}
103103

104104
return new CompiledExpr(
@@ -120,7 +120,7 @@ public function getOutputType(): TypeNode
120120
/**
121121
* @return list<Stmt>
122122
*/
123-
private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $builder): array
123+
private function checkForExtraKeys(Expr $value, Expr $context, PhpCodeBuilder $builder): array
124124
{
125125
$statements = [];
126126

@@ -137,7 +137,7 @@ private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $buil
137137
$builder->staticCall(
138138
$builder->importClass(MappingFailedException::class),
139139
'extraKeys',
140-
[$path, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
140+
[$context, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
141141
),
142142
),
143143
]);

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
]),

src/Compiler/Mapper/Scalar/MapFloat.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333
{
3434
}
3535

36-
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
36+
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
3737
{
3838
$mappedVariableName = $builder->uniqVariableName('mapped');
3939

@@ -44,22 +44,22 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
4444
$builder->if(
4545
if: $isFloat,
4646
then: [
47-
...$this->createFiniteCheckStatements($value, $path, $builder),
47+
...$this->createFiniteCheckStatements($value, $context, $builder),
4848
$builder->assign($builder->var($mappedVariableName), $value),
4949
],
5050
else: [
5151
$builder->if(
5252
if: $isInt,
5353
then: [
54-
...$this->createSafeIntCheckStatements($value, $path, $builder),
54+
...$this->createSafeIntCheckStatements($value, $context, $builder),
5555
$builder->assign($builder->var($mappedVariableName), $builder->funcCall($builder->importFunction('floatval'), [$value])),
5656
],
5757
else: [
5858
$builder->throw(
5959
$builder->staticCall(
6060
$builder->importClass(MappingFailedException::class),
6161
'incorrectType',
62-
[$value, $path, 'float'],
62+
[$value, $context, 'float'],
6363
),
6464
),
6565
],
@@ -84,7 +84,7 @@ public function getOutputType(): TypeNode
8484
/**
8585
* @return list<Stmt>
8686
*/
87-
private function createFiniteCheckStatements(Expr $value, Expr $path, PhpCodeBuilder $builder): array
87+
private function createFiniteCheckStatements(Expr $value, Expr $context, PhpCodeBuilder $builder): array
8888
{
8989
if (!$this->allowInfinity && !$this->allowNan) {
9090
$finiteCheck = $builder->not($builder->funcCall($builder->importFunction('is_finite'), [$value]));
@@ -108,7 +108,7 @@ private function createFiniteCheckStatements(Expr $value, Expr $path, PhpCodeBui
108108
$builder->staticCall(
109109
$builder->importClass(MappingFailedException::class),
110110
'incorrectType',
111-
[$value, $path, $finiteLabel],
111+
[$value, $context, $finiteLabel],
112112
),
113113
),
114114
]),
@@ -118,7 +118,7 @@ private function createFiniteCheckStatements(Expr $value, Expr $path, PhpCodeBui
118118
/**
119119
* @return list<Stmt>
120120
*/
121-
private function createSafeIntCheckStatements(Expr $value, Expr $path, PhpCodeBuilder $builder): array
121+
private function createSafeIntCheckStatements(Expr $value, Expr $context, PhpCodeBuilder $builder): array
122122
{
123123
$minSafeIntConstName = $builder->uniqConstantName('MIN_SAFE_INTEGER', self::MIN_SAFE_INTEGER);
124124
$maxSafeIntConstName = $builder->uniqConstantName('MAX_SAFE_INTEGER', self::MAX_SAFE_INTEGER);
@@ -137,7 +137,7 @@ private function createSafeIntCheckStatements(Expr $value, Expr $path, PhpCodeBu
137137
$builder->staticCall(
138138
$builder->importClass(MappingFailedException::class),
139139
'incorrectValue',
140-
[$value, $path, 'float or int with value that can be losslessly converted to float'],
140+
[$value, $context, 'float or int with value that can be losslessly converted to float'],
141141
),
142142
),
143143
]),

0 commit comments

Comments
 (0)