Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapper: replace $path with MapperContext #44

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Compiler/Mapper/Array/MapArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ public function __construct(
{
}

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

$itemPath = $builder->arrayImmutableAppend($path, $builder->var($keyVariableName));
$itemKeyMapper = $this->keyMapperCompiler->compile($builder->var($keyVariableName), $itemPath, $builder);
$itemValueMapper = $this->valueMapperCompiler->compile($builder->var($valueVariableName), $itemPath, $builder);
$itemContext = $builder->mapperContextAppend($context, $builder->var($keyVariableName));
$itemKeyMapper = $this->keyMapperCompiler->compile($builder->var($keyVariableName), $itemContext, $builder);
$itemValueMapper = $this->valueMapperCompiler->compile($builder->var($valueVariableName), $itemContext, $builder);

$statements = [
$builder->if($builder->not($builder->funcCall($builder->importFunction('is_array'), [$value])), [
$builder->throw(
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectType',
[$value, $path, $builder->val('array')],
[$value, $context, $builder->val('array')],
),
),
]),
Expand Down
16 changes: 8 additions & 8 deletions src/Compiler/Mapper/Array/MapArrayShape.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
{
}

public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
{
$statements = [];
$mappedVariableName = $builder->uniqVariableName('mapped');
Expand All @@ -44,7 +44,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectType',
[$value, $path, $builder->val('array')],
[$value, $context, $builder->val('array')],
),
),
]);
Expand All @@ -56,14 +56,14 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$isMissing = $builder->not($isPresent);

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

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

if ($itemMapping->optional) {
Expand All @@ -75,7 +75,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'missingKey',
[$path, $builder->val($itemMapping->key)],
[$context, $builder->val($itemMapping->key)],
),
),
]);
Expand All @@ -85,7 +85,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
}

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

return new CompiledExpr($builder->var($mappedVariableName), $statements);
Expand Down Expand Up @@ -114,7 +114,7 @@ public function getOutputType(): TypeNode
/**
* @return list<Stmt>
*/
private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $builder): array
private function checkForExtraKeys(Expr $value, Expr $context, PhpCodeBuilder $builder): array
{
$statements = [];

Expand All @@ -131,7 +131,7 @@ private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $buil
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'extraKeys',
[$path, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
[$context, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
),
),
]);
Expand Down
8 changes: 4 additions & 4 deletions src/Compiler/Mapper/Array/MapList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public function __construct(
{
}

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

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

$isArray = $builder->funcCall($builder->importFunction('is_array'), [$value]);
$isList = $builder->funcCall($builder->importFunction('array_is_list'), [$value]);
Expand All @@ -39,7 +39,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectType',
[$value, $path, $builder->val('list')],
[$value, $context, $builder->val('list')],
),
),
]),
Expand Down
8 changes: 4 additions & 4 deletions src/Compiler/Mapper/MapRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
use ShipMonk\InputMapper\Compiler\CompiledExpr;
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder;
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
use ShipMonk\InputMapper\Runtime\MapperContext;

abstract class MapRuntime implements MapperCompiler
{

/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
abstract public static function mapValue(mixed $value, array $path): mixed;
abstract public static function mapValue(mixed $value, ?MapperContext $context): mixed;

public function compile(
Expr $value,
Expr $path,
Expr $context,
PhpCodeBuilder $builder,
): CompiledExpr
{
return new CompiledExpr(
$builder->staticCall(
$builder->importClass(static::class),
'mapValue',
[$value, $path],
[$value, $context],
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Mapper/MapperCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface MapperCompiler
/**
* @throws CannotCompileMapperException
*/
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr;
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr;

public function getInputType(): TypeNode;

Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Mapper/Mixed/MapMixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class MapMixed implements MapperCompiler
{

public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
{
return new CompiledExpr($value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Mapper/Object/DelegateMapperCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function __construct(
{
}

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

return new CompiledExpr($mapped);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Compiler/Mapper/Object/MapDateTimeImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
{
}

public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
{
$mappedVariableName = $builder->uniqVariableName('mapped');
$timezoneVariableName = $builder->uniqVariableName('timezone');
Expand All @@ -60,7 +60,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectType',
[$value, $path, $builder->val('string')],
[$value, $context, $builder->val('string')],
),
),
]),
Expand Down Expand Up @@ -97,7 +97,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectValue',
[$value, $path, $this->formatDescription],
[$value, $context, $this->formatDescription],
),
),
]);
Expand Down
6 changes: 3 additions & 3 deletions src/Compiler/Mapper/Object/MapEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function __construct(
{
}

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

$enumOrNull = $builder->staticCall($builder->importClass($this->enumName), 'tryFrom', [$backingValueMapper->expr]);
Expand All @@ -51,7 +51,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectValue',
[$value, $path, $expectedDescription],
[$value, $context, $expectedDescription],
),
),
]);
Expand Down
18 changes: 9 additions & 9 deletions src/Compiler/Mapper/Object/MapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public function __construct(
{
}

public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
{
$statements = [
$builder->if($builder->not($builder->funcCall($builder->importFunction('is_array'), [$value])), [
$builder->throw(
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectType',
[$value, $path, $builder->val('array')],
[$value, $context, $builder->val('array')],
),
),
]),
Expand All @@ -58,15 +58,15 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$isMissing = $builder->not($isPresent);

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

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

if (count($fallbackValueMapper->statements) > 0) {
$statements[] = $builder->if(
Expand All @@ -88,7 +88,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'missingKey',
[$path, $key],
[$context, $key],
),
),
]);
Expand All @@ -98,7 +98,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
}

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

return new CompiledExpr(
Expand All @@ -120,7 +120,7 @@ public function getOutputType(): TypeNode
/**
* @return list<Stmt>
*/
private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $builder): array
private function checkForExtraKeys(Expr $value, Expr $context, PhpCodeBuilder $builder): array
{
$statements = [];

Expand All @@ -137,7 +137,7 @@ private function checkForExtraKeys(Expr $value, Expr $path, PhpCodeBuilder $buil
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'extraKeys',
[$path, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
[$context, $builder->funcCall($builder->importFunction('array_keys'), [$builder->var($extraKeysVariableName)])],
),
),
]);
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Mapper/Scalar/MapBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
class MapBool implements MapperCompiler
{

public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr
public function compile(Expr $value, Expr $context, PhpCodeBuilder $builder): CompiledExpr
{
$statements = [
$builder->if($builder->not($builder->funcCall($builder->importFunction('is_bool'), [$value])), [
$builder->throw(
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectType',
[$value, $path, $builder->val('bool')],
[$value, $context, $builder->val('bool')],
),
),
]),
Expand Down
16 changes: 8 additions & 8 deletions src/Compiler/Mapper/Scalar/MapFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
{
}

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

Expand All @@ -44,22 +44,22 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
$builder->if(
if: $isFloat,
then: [
...$this->createFiniteCheckStatements($value, $path, $builder),
...$this->createFiniteCheckStatements($value, $context, $builder),
$builder->assign($builder->var($mappedVariableName), $value),
],
else: [
$builder->if(
if: $isInt,
then: [
...$this->createSafeIntCheckStatements($value, $path, $builder),
...$this->createSafeIntCheckStatements($value, $context, $builder),
$builder->assign($builder->var($mappedVariableName), $builder->funcCall($builder->importFunction('floatval'), [$value])),
],
else: [
$builder->throw(
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectType',
[$value, $path, 'float'],
[$value, $context, 'float'],
),
),
],
Expand All @@ -84,7 +84,7 @@ public function getOutputType(): TypeNode
/**
* @return list<Stmt>
*/
private function createFiniteCheckStatements(Expr $value, Expr $path, PhpCodeBuilder $builder): array
private function createFiniteCheckStatements(Expr $value, Expr $context, PhpCodeBuilder $builder): array
{
if (!$this->allowInfinity && !$this->allowNan) {
$finiteCheck = $builder->not($builder->funcCall($builder->importFunction('is_finite'), [$value]));
Expand All @@ -108,7 +108,7 @@ private function createFiniteCheckStatements(Expr $value, Expr $path, PhpCodeBui
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectType',
[$value, $path, $finiteLabel],
[$value, $context, $finiteLabel],
),
),
]),
Expand All @@ -118,7 +118,7 @@ private function createFiniteCheckStatements(Expr $value, Expr $path, PhpCodeBui
/**
* @return list<Stmt>
*/
private function createSafeIntCheckStatements(Expr $value, Expr $path, PhpCodeBuilder $builder): array
private function createSafeIntCheckStatements(Expr $value, Expr $context, PhpCodeBuilder $builder): array
{
$minSafeIntConstName = $builder->uniqConstantName('MIN_SAFE_INTEGER', self::MIN_SAFE_INTEGER);
$maxSafeIntConstName = $builder->uniqConstantName('MAX_SAFE_INTEGER', self::MAX_SAFE_INTEGER);
Expand All @@ -137,7 +137,7 @@ private function createSafeIntCheckStatements(Expr $value, Expr $path, PhpCodeBu
$builder->staticCall(
$builder->importClass(MappingFailedException::class),
'incorrectValue',
[$value, $path, 'float or int with value that can be losslessly converted to float'],
[$value, $context, 'float or int with value that can be losslessly converted to float'],
),
),
]),
Expand Down
Loading
Loading