Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrenkoAnton committed Feb 22, 2024
1 parent f97c57a commit 462a244
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 78 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ HandleDtoException
| Code | Message pattern | Exception | Group |
|------|:--------------------------------------------------------------------------------------------------------------|:----------------------------------|:-------------------|
| 101 | Dto: %s | Property: %s | Err: Missed property type declaration | NoTypeDeclarationException | InitDtoException |
| 102 | Dto: %s | Property: %s | Err: Unsupported nullable property type declaration | NullableDeclarationException | InitDtoException |
| 103 | Dto: %s | Property: %s | Err: Unsupported mixed property type declaration | MixedDeclarationException | InitDtoException |
| 104 | Dto: %s | Property: %s | Err: Unsupported object property type declaration | ObjectDeclarationException | InitDtoException |
| 105 | Dto: %s | Property: %s | Err: Class must implement DtoInterface | NotDtoClassDeclarationException | InitDtoException |
| 106 | Dto: %s | Property: %s | Err: No backing value for enum | EnumNoBackingValueException | InitDtoException |
| 107 | DtoCollection: %s | Err: Invalid constructor declaration | DtoCollectionConstructorException | InitDtoException |
| 102 | Dto: %s | Property: %s | Err: Unsupported mixed property type declaration | MixedDeclarationException | InitDtoException |
| 103 | Dto: %s | Property: %s | Err: Unsupported object property type declaration | ObjectDeclarationException | InitDtoException |
| 104 | Dto: %s | Property: %s | Err: Class must implement DtoInterface | NotDtoClassDeclarationException | InitDtoException |
| 105 | Dto: %s | Property: %s | Err: No backing value for enum | EnumNoBackingValueException | InitDtoException |
| 106 | DtoCollection: %s | Err: Invalid constructor declaration | DtoCollectionConstructorException | InitDtoException |
| 201 | DtoCollection: %s | Expected Dto: %s | Given Dto: %s | AddDtoException | SetupDtoException |
| 202 | Dto: %s | Property: %s | Err: No data | InputDataException | SetupDtoException |
| 203 | Dto: %s | Property: %s | Expected type: %s | Given type: %s | Value: %s | SetValueException | SetupDtoException |
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
2.0.0
14 changes: 8 additions & 6 deletions src/Dto.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Dto\Exception\DtoException\InitDtoException\DeclarationException\MixedDeclarationException;
use Dto\Exception\DtoException\InitDtoException\DeclarationException\NoTypeDeclarationException;
use Dto\Exception\DtoException\InitDtoException\DeclarationException\NotDtoClassDeclarationException;
use Dto\Exception\DtoException\InitDtoException\DeclarationException\NullableDeclarationException;
use Dto\Exception\DtoException\InitDtoException\DeclarationException\ObjectDeclarationException;
use Dto\Exception\DtoException\SetupDtoException\InputDataException;
use Dto\Exception\DtoException\SetupDtoException\SetValueEnumException;
Expand All @@ -36,6 +35,7 @@
use function implode;
use function is_a;
use function is_array;
use function is_null;
use function is_subclass_of;
use function key_exists;
use function lcfirst;
Expand Down Expand Up @@ -155,10 +155,6 @@ private function validateDeclaration(ReflectionProperty $property): void
throw new MixedDeclarationException($this::class, $name);
}

if ($type->allowsNull()) {
throw new NullableDeclarationException($this::class, $name);
}

if ($type->getName() === 'object') {
throw new ObjectDeclarationException($this::class, $name);
}
Expand Down Expand Up @@ -215,12 +211,18 @@ private function getExpectedProperty(string $expectedProperty): string
*/
private function setValue(ReflectionNamedType $propertyType, string $propertyName, mixed $value): void
{
$typeName = $propertyType->getName();
if (is_null($value) && $propertyType->allowsNull()) {
$this->{$propertyName} = null;

return;
}

if (is_a($value, Arrayable::class)) {
$value = $value->toArray();
}

$typeName = $propertyType->getName();

if ($propertyType->isBuiltin()) {
$this->setBuiltinType($typeName, $propertyName, $value);

Expand Down
11 changes: 5 additions & 6 deletions src/Exception/DtoException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ class DtoException extends Exception
{
// INIT
protected const NO_TYPE_DECLARATION_CODE = 101;
protected const NULLABLE_DECLARATION_CODE = 102;
protected const MIXED_DECLARATION_CODE = 103;
protected const OBJECT_DECLARATION_CODE = 104;
protected const NOT_DTO_CLASS_DECLARATION_CODE = 105;
protected const ENUM_NO_BACKING_VALUE_CODE = 106;
protected const INVALID_DTO_COLLECTION_CONSTRUCTOR_CODE = 107;
protected const MIXED_DECLARATION_CODE = 102;
protected const OBJECT_DECLARATION_CODE = 103;
protected const NOT_DTO_CLASS_DECLARATION_CODE = 104;
protected const ENUM_NO_BACKING_VALUE_CODE = 105;
protected const INVALID_DTO_COLLECTION_CONSTRUCTOR_CODE = 106;
// SETUP
protected const ADD_DTO_EXCEPTION = 201;
protected const EMPTY_INPUT_DATA_CODE = 202;
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion tests/DtoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function testAddMethodThrowsDtoCollectionConstructorException(): void
$this->expectException(DtoCollectionConstructorException::class);
// phpcs:ignore
$this->expectExceptionMessage('DtoCollection: Tests\Fixtures\Unsupported\InvalidContructorDtoCollection | Err: Invalid constructor declaration');
$this->expectExceptionCode(107);
$this->expectExceptionCode(106);

$dtoCollection = new InvalidContructorDtoCollection(1);
$dtoCollection->add(new EmptyClassDto([]));
Expand Down
2 changes: 1 addition & 1 deletion tests/DtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function dpInvalidMethod(): array
}

/**
* @group +
* @group ok
* @dataProvider dpTestToArray
*/
public function testToArray(KeyCase $keyCase, array $expected): void
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/ErrorDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Dto\Dto;

/**
* @method string getName()
* @method int getAge()
* @psalm-suppress PossiblyUnusedProperty
* @psalm-suppress PropertyNotSetInConstructor
*/
class ErrorDto extends Dto
{
protected string $message;
protected int $code;
}
19 changes: 19 additions & 0 deletions tests/Fixtures/NestedNullableDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Dto\Dto;

/**
* @method ?sting getType()
* @method ?ErrorDto getError()
* @psalm-suppress PossiblyUnusedProperty
* @psalm-suppress PropertyNotSetInConstructor
*/
class NestedNullableDto extends Dto
{
protected ?string $type;
protected ?ErrorDto $error;
}
16 changes: 0 additions & 16 deletions tests/Fixtures/Unsupported/NullableDeclarationDto.php

This file was deleted.

48 changes: 48 additions & 0 deletions tests/NestedNullableDtoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Test;

use PHPUnit\Framework\TestCase;
use Tests\Fixtures\NestedNullableDto;

class NestedNullableDtoTest extends TestCase
{
/**
* @group ok
* @dataProvider dp
*/
public function testGetNullableValue(array $data): void
{
$dto = new NestedNullableDto($data);
$this->assertEquals($data, $dto->toArray());
}

public static function dp(): array
{
return [
[
[
'type' => null,
'error' => null,
],
],
[
[
'type' => 'type',
'error' => null,
],
],
[
[
'type' => null,
'error' => [
'message' => 'Error message',
'code' => 0,
],
],
],
];
}
}
2 changes: 1 addition & 1 deletion tests/NoBackedEnumDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testGetValueSuccess(): void
$this->expectException(InitDtoException::class);
// phpcs:ignore
$this->expectExceptionMessage('Dto: Tests\Fixtures\WithNoBackedEnumDto | Property: color | Err: No backing value for enum');
$this->expectExceptionCode(106);
$this->expectExceptionCode(105);
new WithNoBackedEnumDto(['color' => 'red']);
}
}
23 changes: 3 additions & 20 deletions tests/UnsupportedDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPUnit\Framework\TestCase;
use Tests\Fixtures\Unsupported\MixedDeclarationDto;
use Tests\Fixtures\Unsupported\NoDeclarationDto;
use Tests\Fixtures\Unsupported\NullableDeclarationDto;
use Tests\Fixtures\Unsupported\ObjectDeclarationDto;
use Tests\Fixtures\Unsupported\RandomClass;
use Tests\Fixtures\Unsupported\RandomClassDeclarationDto;
Expand Down Expand Up @@ -51,24 +50,8 @@ public function testMixedPropertyTypeDeclarationThrowsException(): void
// phpcs:ignore
'Dto: Tests\Fixtures\Unsupported\MixedDeclarationDto | Property: name | Err: Unsupported mixed property type declaration',
);
$this->expectExceptionCode(103);
new MixedDeclarationDto($this->data);
}

/**
* @throws InitDtoException
*
* @group ok
*/
public function testNullablePropertyTypeDeclarationThrowsException(): void
{
$this->expectException(InitDtoException::class);
$this->expectExceptionMessage(
// phpcs:ignore
'Dto: Tests\Fixtures\Unsupported\NullableDeclarationDto | Property: name | Err: Unsupported nullable property type declaration',
);
$this->expectExceptionCode(102);
new NullableDeclarationDto($this->data);
new MixedDeclarationDto($this->data);
}

/**
Expand All @@ -83,7 +66,7 @@ public function testObjectPropertyTypeDeclarationThrowsException(): void
// phpcs:ignore
'Dto: Tests\Fixtures\Unsupported\ObjectDeclarationDto | Property: name | Err: Unsupported object property type declaration',
);
$this->expectExceptionCode(104);
$this->expectExceptionCode(103);
new ObjectDeclarationDto($this->data);
}

Expand All @@ -99,7 +82,7 @@ public function testRandomClassPropertyTypeDeclarationThrowsException(): void
// phpcs:ignore
'Dto: Tests\Fixtures\Unsupported\RandomClassDeclarationDto | Property: name | Err: Class must implement DtoInterface',
);
$this->expectExceptionCode(105);
$this->expectExceptionCode(104);
new RandomClassDeclarationDto(['name' => new RandomClass()]);
}
}

0 comments on commit 462a244

Please sign in to comment.