-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Optional attribute that allows specifying default value
- Loading branch information
Showing
10 changed files
with
376 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\InputMapper\Compiler\Mapper; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)] | ||
class Optional | ||
{ | ||
|
||
public function __construct( | ||
public readonly mixed $default = null, | ||
) | ||
{ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\InputMapper\Compiler\Mapper\Wrapper; | ||
|
||
use Attribute; | ||
use BackedEnum; | ||
use LogicException; | ||
use PhpParser\Node\Expr; | ||
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use ShipMonk\InputMapper\Compiler\CompiledExpr; | ||
use ShipMonk\InputMapper\Compiler\Mapper\MapperCompiler; | ||
use ShipMonk\InputMapper\Compiler\Mapper\UndefinedAwareMapperCompiler; | ||
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder; | ||
use ShipMonk\InputMapper\Compiler\Type\PhpDocTypeUtils; | ||
use function array_is_list; | ||
use function array_keys; | ||
use function array_map; | ||
use function array_values; | ||
use function get_debug_type; | ||
use function is_array; | ||
use function is_scalar; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)] | ||
class MapDefaultValue implements UndefinedAwareMapperCompiler | ||
{ | ||
|
||
public function __construct( | ||
public readonly MapperCompiler $mapperCompiler, | ||
public readonly mixed $defaultValue, | ||
) | ||
{ | ||
} | ||
|
||
public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): CompiledExpr | ||
{ | ||
return $this->mapperCompiler->compile($value, $path, $builder); | ||
} | ||
|
||
public function compileUndefined(Expr $path, Expr $key, PhpCodeBuilder $builder): CompiledExpr | ||
{ | ||
if ($this->defaultValue === null || is_scalar($this->defaultValue) || is_array($this->defaultValue)) { | ||
return new CompiledExpr($builder->val(null)); | ||
} | ||
|
||
if ($this->defaultValue instanceof BackedEnum) { | ||
return new CompiledExpr($builder->classConstFetch($builder->importClass($this->defaultValue::class), $this->defaultValue->name)); | ||
} | ||
|
||
throw new LogicException('Unsupported default value type: ' . get_debug_type($this->defaultValue)); | ||
} | ||
|
||
public function getInputType(): TypeNode | ||
{ | ||
return $this->mapperCompiler->getInputType(); | ||
} | ||
|
||
public function getOutputType(): TypeNode | ||
{ | ||
return $this->mapperCompiler->getOutputType(); | ||
} | ||
|
||
public function getDefaultValueType(): TypeNode | ||
{ | ||
return $this->typeFromValue($this->defaultValue); | ||
} | ||
|
||
private function typeFromValue(mixed $value): TypeNode | ||
{ | ||
if (is_scalar($value) || $value === null) { | ||
return new IdentifierTypeNode(get_debug_type($value)); | ||
} | ||
|
||
if (is_array($value)) { | ||
if (array_is_list($value)) { | ||
$valueType = PhpDocTypeUtils::union(...array_map($this->typeFromValue(...), $value)); | ||
return new GenericTypeNode(new IdentifierTypeNode('list'), [$valueType]); | ||
} | ||
|
||
$keyType = PhpDocTypeUtils::union(...array_map($this->typeFromValue(...), array_keys($value))); | ||
$valueType = PhpDocTypeUtils::union(...array_map($this->typeFromValue(...), array_values($value))); | ||
return new GenericTypeNode(new IdentifierTypeNode('array'), [$keyType, $valueType]); | ||
} | ||
|
||
if ($value instanceof BackedEnum) { | ||
return new IdentifierTypeNode($value::class); | ||
} | ||
|
||
throw new LogicException('Unsupported default value type: ' . get_debug_type($value)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tests/Compiler/Mapper/Wrapper/Data/IntWithDefaultValueMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare (strict_types=1); | ||
|
||
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper\Data; | ||
|
||
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\MapDefaultValue; | ||
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException; | ||
use ShipMonk\InputMapper\Runtime\Mapper; | ||
use ShipMonk\InputMapper\Runtime\MapperProvider; | ||
use function is_int; | ||
|
||
/** | ||
* Generated mapper by {@see MapDefaultValue}. Do not edit directly. | ||
* | ||
* @implements Mapper<int> | ||
*/ | ||
class IntWithDefaultValueMapper implements Mapper | ||
{ | ||
public function __construct(private readonly MapperProvider $provider) | ||
{ | ||
} | ||
|
||
/** | ||
* @param list<string|int> $path | ||
* @throws MappingFailedException | ||
*/ | ||
public function map(mixed $data, array $path = []): int | ||
{ | ||
if (!is_int($data)) { | ||
throw MappingFailedException::incorrectType($data, $path, 'int'); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare (strict_types = 1); | ||
|
||
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper\Data; | ||
|
||
use ShipMonk\InputMapper\Compiler\Mapper\Optional; | ||
|
||
class Semaphore | ||
{ | ||
|
||
public function __construct( | ||
#[Optional(default: SemaphoreColorEnum::Red)] | ||
public readonly SemaphoreColorEnum $color, | ||
|
||
#[Optional] | ||
public readonly ?string $manufacturer, | ||
) | ||
{ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php declare (strict_types = 1); | ||
|
||
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper\Data; | ||
|
||
enum SemaphoreColorEnum: string | ||
{ | ||
|
||
case Red = 'red'; | ||
case Yellow = 'yellow'; | ||
case Green = 'green'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php declare (strict_types=1); | ||
|
||
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper\Data; | ||
|
||
use ShipMonk\InputMapper\Compiler\Mapper\Object\MapObject; | ||
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException; | ||
use ShipMonk\InputMapper\Runtime\Mapper; | ||
use ShipMonk\InputMapper\Runtime\MapperProvider; | ||
use function array_column; | ||
use function array_diff_key; | ||
use function array_key_exists; | ||
use function array_keys; | ||
use function count; | ||
use function implode; | ||
use function is_array; | ||
use function is_string; | ||
|
||
/** | ||
* Generated mapper by {@see MapObject}. Do not edit directly. | ||
* | ||
* @implements Mapper<Semaphore> | ||
*/ | ||
class SemaphoreMapper implements Mapper | ||
{ | ||
public function __construct(private readonly MapperProvider $provider) | ||
{ | ||
} | ||
|
||
/** | ||
* @param list<string|int> $path | ||
* @throws MappingFailedException | ||
*/ | ||
public function map(mixed $data, array $path = []): Semaphore | ||
{ | ||
if (!is_array($data)) { | ||
throw MappingFailedException::incorrectType($data, $path, 'array'); | ||
} | ||
|
||
$knownKeys = ['color' => true, 'manufacturer' => true]; | ||
$extraKeys = array_diff_key($data, $knownKeys); | ||
|
||
if (count($extraKeys) > 0) { | ||
throw MappingFailedException::extraKeys($path, array_keys($extraKeys)); | ||
} | ||
|
||
return new Semaphore( | ||
array_key_exists('color', $data) ? $this->mapColor($data['color'], [...$path, 'color']) : SemaphoreColorEnum::Green, | ||
array_key_exists('manufacturer', $data) ? $this->mapManufacturer($data['manufacturer'], [...$path, 'manufacturer']) : null, | ||
); | ||
} | ||
|
||
/** | ||
* @param list<string|int> $path | ||
* @throws MappingFailedException | ||
*/ | ||
private function mapColor(mixed $data, array $path = []): SemaphoreColorEnum | ||
{ | ||
if (!is_string($data)) { | ||
throw MappingFailedException::incorrectType($data, $path, 'string'); | ||
} | ||
|
||
$enum = SemaphoreColorEnum::tryFrom($data); | ||
|
||
if ($enum === null) { | ||
throw MappingFailedException::incorrectValue($data, $path, 'one of ' . implode(', ', array_column(SemaphoreColorEnum::cases(), 'value'))); | ||
} | ||
|
||
return $enum; | ||
} | ||
|
||
/** | ||
* @param list<string|int> $path | ||
* @throws MappingFailedException | ||
*/ | ||
private function mapManufacturer(mixed $data, array $path = []): ?string | ||
{ | ||
if ($data === null) { | ||
$mapped = null; | ||
} else { | ||
if (!is_string($data)) { | ||
throw MappingFailedException::incorrectType($data, $path, 'string'); | ||
} | ||
|
||
$mapped = $data; | ||
} | ||
|
||
return $mapped; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper; | ||
|
||
use ShipMonk\InputMapper\Compiler\Mapper\Object\MapEnum; | ||
use ShipMonk\InputMapper\Compiler\Mapper\Object\MapObject; | ||
use ShipMonk\InputMapper\Compiler\Mapper\Scalar\MapInt; | ||
use ShipMonk\InputMapper\Compiler\Mapper\Scalar\MapString; | ||
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\MapDefaultValue; | ||
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\MapNullable; | ||
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException; | ||
use ShipMonkTests\InputMapper\Compiler\Mapper\MapperCompilerTestCase; | ||
use ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper\Data\Semaphore; | ||
use ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper\Data\SemaphoreColorEnum; | ||
|
||
class MapDefaultValueTest extends MapperCompilerTestCase | ||
{ | ||
|
||
public function testCompile(): void | ||
{ | ||
$mapperCompiler = new MapDefaultValue(new MapInt(), null); | ||
$mapper = $this->compileMapper('IntWithDefaultValue', $mapperCompiler); | ||
|
||
self::assertSame(1, $mapper->map(1)); | ||
self::assertSame(2, $mapper->map(2)); | ||
|
||
self::assertException( | ||
MappingFailedException::class, | ||
'Failed to map data at path /: Expected int, got "1"', | ||
static fn() => $mapper->map('1'), | ||
); | ||
} | ||
|
||
public function testCompileUndefined(): void | ||
{ | ||
$mapperCompiler = new MapObject(Semaphore::class, [ | ||
'color' => new MapDefaultValue(new MapEnum(SemaphoreColorEnum::class, new MapString()), SemaphoreColorEnum::Green), | ||
'manufacturer' => new MapDefaultValue(new MapNullable(new MapString()), null), | ||
]); | ||
|
||
$mapper = $this->compileMapper('Semaphore', $mapperCompiler); | ||
|
||
self::assertEquals(new Semaphore(SemaphoreColorEnum::Green, null), $mapper->map([])); | ||
self::assertEquals(new Semaphore(SemaphoreColorEnum::Red, null), $mapper->map(['color' => 'red'])); | ||
self::assertEquals(new Semaphore(SemaphoreColorEnum::Red, 'Siemens'), $mapper->map(['color' => 'red', 'manufacturer' => 'Siemens'])); | ||
} | ||
|
||
} |
Oops, something went wrong.