This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
450 additions
and
65 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
80 changes: 80 additions & 0 deletions
80
...taBind/MetaData/Type/StringFormType/Parser/Resolver/PrimitiveType/AliasedFormatResult.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,80 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* @author Martin Fris <rasta@lj.sk> | ||
*/ | ||
|
||
namespace BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\PrimitiveType; | ||
|
||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Format\ResultInterface as FormatResultInterface; | ||
use BetterSerializer\DataBind\MetaData\Type\TypeEnum; | ||
|
||
/** | ||
* | ||
*/ | ||
final class AliasedFormatResult implements FormatResultInterface | ||
{ | ||
|
||
/** | ||
* @var FormatResultInterface | ||
*/ | ||
private $delegate; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private static $aliases = [ | ||
'integer' => TypeEnum::INTEGER_TYPE, | ||
]; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $resolvedType; | ||
|
||
/** | ||
* @param FormatResultInterface $delegate | ||
*/ | ||
public function __construct(FormatResultInterface $delegate) | ||
{ | ||
$this->delegate = $delegate; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getType(): string | ||
{ | ||
if ($this->resolvedType === null) { | ||
$type = $this->delegate->getType(); | ||
$this->resolvedType = self::$aliases[$type] ?? $type; | ||
} | ||
|
||
return $this->resolvedType; | ||
} | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
public function getParameters(): ?string | ||
{ | ||
return $this->delegate->getParameters(); | ||
} | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
public function getNestedValueType(): ?string | ||
{ | ||
return $this->delegate->getNestedValueType(); | ||
} | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
public function getNestedKeyType(): ?string | ||
{ | ||
return $this->delegate->getNestedKeyType(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...taBind/MetaData/Type/StringFormType/Parser/Resolver/PrimitiveType/AliasedTypeResolver.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,45 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* @author Martin Fris <rasta@lj.sk> | ||
*/ | ||
|
||
namespace BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\PrimitiveType; | ||
|
||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Format\ResultInterface as FormatResultInterface; | ||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\ContextInterface; | ||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\ResolverInterface; | ||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\ResultInterface; | ||
|
||
/** | ||
* | ||
*/ | ||
final class AliasedTypeResolver implements ResolverInterface | ||
{ | ||
|
||
/** | ||
* @var ResolverInterface | ||
*/ | ||
private $delegate; | ||
|
||
/** | ||
* @param ResolverInterface $delegate | ||
*/ | ||
public function __construct(ResolverInterface $delegate) | ||
{ | ||
$this->delegate = $delegate; | ||
} | ||
|
||
/** | ||
* @param FormatResultInterface $formatResult | ||
* @param ContextInterface $context | ||
* @return ResultInterface|null | ||
*/ | ||
public function resolve(FormatResultInterface $formatResult, ContextInterface $context): ?ResultInterface | ||
{ | ||
$aliasedFormatResult = new AliasedFormatResult($formatResult); | ||
|
||
return $this->delegate->resolve($aliasedFormatResult, $context); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...nd/MetaData/Type/StringFormType/Parser/Resolver/PrimitiveType/AliasedFormatResultTest.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,65 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* @author Martin Fris <rasta@lj.sk> | ||
*/ | ||
|
||
namespace BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\PrimitiveType; | ||
|
||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Format\ResultInterface as FormatResultInterface; | ||
use BetterSerializer\DataBind\MetaData\Type\TypeEnum; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* | ||
*/ | ||
class AliasedFormatResultTest extends TestCase | ||
{ | ||
|
||
/** | ||
* | ||
*/ | ||
public function testEverythingUnaliased(): void | ||
{ | ||
$type = TypeEnum::INTEGER_TYPE; | ||
|
||
$delegate = $this->createMock(FormatResultInterface::class); | ||
$delegate->expects(self::once()) | ||
->method('getType') | ||
->willReturn($type); | ||
$delegate->expects(self::once()) | ||
->method('getParameters') | ||
->willReturn(null); | ||
$delegate->expects(self::once()) | ||
->method('getNestedValueType') | ||
->willReturn(null); | ||
$delegate->expects(self::once()) | ||
->method('getNestedKeyType') | ||
->willReturn(null); | ||
|
||
$aliased = new AliasedFormatResult($delegate); | ||
|
||
self::assertEquals($type, $aliased->getType()); | ||
self::assertNull($aliased->getParameters()); | ||
self::assertNull($aliased->getNestedValueType()); | ||
self::assertNull($aliased->getNestedKeyType()); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testAliasedType(): void | ||
{ | ||
$type = 'integer'; | ||
|
||
$delegate = $this->createMock(FormatResultInterface::class); | ||
$delegate->expects(self::once()) | ||
->method('getType') | ||
->willReturn($type); | ||
|
||
$aliased = new AliasedFormatResult($delegate); | ||
|
||
self::assertEquals(TypeEnum::INTEGER_TYPE, $aliased->getType()); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...nd/MetaData/Type/StringFormType/Parser/Resolver/PrimitiveType/AliasedTypeResolverTest.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,42 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* @author Martin Fris <rasta@lj.sk> | ||
*/ | ||
|
||
namespace BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\PrimitiveType; | ||
|
||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Format\ResultInterface as FormatResultInterface; | ||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\ContextInterface; | ||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\ResolverInterface; | ||
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\ResultInterface; | ||
use PHPUnit\Framework\Constraint\IsInstanceOf; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* | ||
*/ | ||
class AliasedTypeResolverTest extends TestCase | ||
{ | ||
|
||
/** | ||
* | ||
*/ | ||
public function testFormatResultDecoration(): void | ||
{ | ||
$expectedResult = $this->createMock(ResultInterface::class); | ||
$formatResult = $this->createMock(FormatResultInterface::class); | ||
$context = $this->createMock(ContextInterface::class); | ||
$typeResolver = $this->createMock(ResolverInterface::class); | ||
$typeResolver->expects(self::once()) | ||
->method('resolve') | ||
->with(new IsInstanceOf(AliasedFormatResult::class), $context) | ||
->willReturn($expectedResult); | ||
|
||
$aliasedResolver = new AliasedTypeResolver($typeResolver); | ||
$result = $aliasedResolver->resolve($formatResult, $context); | ||
|
||
self::assertSame($expectedResult, $result); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* @author Martin Fris <rasta@lj.sk> | ||
*/ | ||
|
||
namespace BetterSerializer\Dto; | ||
|
||
use BetterSerializer\DataBind\MetaData\Annotations as Serializer; | ||
|
||
/** | ||
* | ||
*/ | ||
final class Aliases | ||
{ | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
private $integer1; | ||
|
||
/** | ||
* @var integer | ||
* @Serializer\Property(type="integer") | ||
*/ | ||
private $integer2; | ||
|
||
/** | ||
* @param int $integer1 | ||
* @param int $integer2 | ||
*/ | ||
public function __construct(int $integer1, int $integer2) | ||
{ | ||
$this->integer1 = $integer1; | ||
$this->integer2 = $integer2; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getInteger1(): int | ||
{ | ||
return $this->integer1; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getInteger2(): int | ||
{ | ||
return $this->integer2; | ||
} | ||
} |
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.