-
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 (#60)
- Loading branch information
Showing
21 changed files
with
476 additions
and
19 deletions.
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
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
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
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,63 @@ | ||
<?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\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 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($this->defaultValue)); | ||
} | ||
|
||
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 PhpDocTypeUtils::fromValue($this->defaultValue); | ||
} | ||
|
||
} |
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
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
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
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
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
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
Oops, something went wrong.