Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Merge 3d25814 into 32cda02
Browse files Browse the repository at this point in the history
  • Loading branch information
Rastusik committed Jun 1, 2018
2 parents 32cda02 + 3d25814 commit f1b7f1c
Show file tree
Hide file tree
Showing 22 changed files with 450 additions and 65 deletions.
10 changes: 9 additions & 1 deletion config/di.pimple.php
Expand Up @@ -10,6 +10,7 @@
use BetterSerializer\DataBind\MetaData\Reader\PropertyReader\TypeResolver\DocBlockPropertyTypeResolver;
use BetterSerializer\DataBind\MetaData\Type\Factory\Chain as TypeFactoryChain;
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\HigherType;
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\PrimitiveType\AliasedTypeResolver;
use BetterSerializer\DataBind\Reader\Instantiator\Factory\Standard\ParamProcessor;
use BetterSerializer\DataBind\Reader\Processor\Factory as ReaderProcessorFactory;
use BetterSerializer\DataBind\Writer\Processor\Factory as WriterProcessorFactory;
Expand Down Expand Up @@ -577,7 +578,7 @@ function () {
function (Container $c) {
return new BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\ResolverChain(
[
$c[BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\PrimitiveTypeResolver::class],
$c[AliasedTypeResolver::class],
$c[BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\HigherTypeResolver::class],
]
);
Expand All @@ -588,6 +589,13 @@ function () {
return new BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\PrimitiveTypeResolver();
};

$container[AliasedTypeResolver::class] =
function (Container $c) {
return new AliasedTypeResolver(
$c[BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\PrimitiveTypeResolver::class]
);
};

$container[BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\HigherTypeResolver::class] =
function (Container $c) {
return new BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\Resolver\HigherTypeResolver(
Expand Down
Expand Up @@ -10,6 +10,7 @@
use BetterSerializer\DataBind\MetaData\Reader\PropertyReader\Context\PropertyContextInterface;
use BetterSerializer\DataBind\MetaData\Type\StringFormType\ContextStringFormTypeInterface;
use BetterSerializer\DataBind\MetaData\Type\StringFormType\Parser\StringTypeParserInterface;
use InvalidArgumentException;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use phpDocumentor\Reflection\DocBlockFactoryInterface;

Expand Down Expand Up @@ -43,6 +44,7 @@ public function __construct(DocBlockFactoryInterface $docBlockFactory, StringTyp
/**
* @param PropertyContextInterface $context
* @return ContextStringFormTypeInterface|null
* @throws InvalidArgumentException
*/
public function resolveType(PropertyContextInterface $context): ?ContextStringFormTypeInterface
{
Expand Down
@@ -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();
}
}
@@ -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);
}
}
@@ -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());
}
}
@@ -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);
}
}
54 changes: 54 additions & 0 deletions tests/BetterSerializer/Dto/Aliases.php
@@ -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;
}
}
3 changes: 0 additions & 3 deletions tests/BetterSerializer/Dto/Car.php
Expand Up @@ -10,9 +10,6 @@
use JMS\Serializer\Annotation as JmsSerializer;

/**
* Class Car
* @author mfris
* @package BetterSerializer\Dto
* @Serializer\RootName(value="car")
*/
class Car implements CarInterface
Expand Down
4 changes: 1 addition & 3 deletions tests/BetterSerializer/Dto/Car2.php
Expand Up @@ -12,9 +12,7 @@
use DateTimeImmutable;

/**
* Class Car
* @author mfris
* @package BetterSerializer\Dto
*
*/
class Car2
{
Expand Down
3 changes: 0 additions & 3 deletions tests/BetterSerializer/Dto/CarInterface.php
Expand Up @@ -7,9 +7,6 @@
namespace BetterSerializer\Dto;

/**
* Class Car
* @author mfris
* @package BetterSerializer\Dto
* @Serializer\RootName(value="car")
*/
interface CarInterface
Expand Down
10 changes: 5 additions & 5 deletions tests/BetterSerializer/Dto/Category.php
Expand Up @@ -6,12 +6,11 @@
*/
namespace BetterSerializer\Dto;

use Exception;
use JMS\Serializer\Annotation as JmsSerializer;

/**
* Class Category
* @author mfris
* @package BetterSerializer\Dto
*
*/
class Category
{
Expand Down Expand Up @@ -39,9 +38,10 @@ class Category
private $children = [];

/**
* @param int $id
* @param int $id
* @param Category|null $parent
* @param Category[] $children
* @param Category[] $children
* @throws Exception
*/
public function __construct(int $id, Category $parent = null, array $children = [])
{
Expand Down

0 comments on commit f1b7f1c

Please sign in to comment.