Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrenkoAnton authored Feb 20, 2024
1 parent 2eb5a6e commit f97c57a
Show file tree
Hide file tree
Showing 18 changed files with 228 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: ramsey/composer-install@v2

- name: Run tests with coverage results
run: vendor/bin/phpunit --coverage-clover ./tests/output/clover.xml
run: vendor/bin/phpunit --coverage-clover ./tests/_output/clover.xml

- name: Upload coverage results to Coveralls
env:
Expand All @@ -41,7 +41,7 @@ jobs:
COVERALLS_FLAG_NAME: ${{ matrix.php }}
run: |
composer require php-coveralls/php-coveralls -n
vendor/bin/php-coveralls --coverage_clover=./tests/output/clover.xml -v --json_path=./tests/output/coveralls-upload.json
vendor/bin/php-coveralls --coverage_clover=./tests/_output/clover.xml -v --json_path=./tests/_output/coveralls-upload.json
finish:
name: Close parallel build
needs: tests
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
vendor
docker/.env
.phpunit.result.cache
/tests/output
!/tests/output/.gitkeep
/tests/_output
!/tests/_output/.gitkeep
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,36 @@ $color = $nestedDto->getColor(); // ColorEnum::Red
$colorValue = $colorEnum->value; // 'red'
```

#### Return DTO as array

```php
<?php

declare(strict_types=1);

use Dto\Dto;

/**
* @method int getActualNumber()
* @method string getProviderName()
*/
class SnakeCaseDto extends Dto
{
protected int $actualNumber;
protected string $providerName;
}

$data = [
'actualNumber' => 5,
'providerName' => 'Main Provider',
];

$dto = new SnakeCaseDto($data);

$array = $dto->toArray(); // ['actualNumber' => 5, 'providerName' => 'Main Provider']
$arrayWithSnakeCaseKeys = $dto->toArray(rawKeys: false); // ['actual_number' => 5, 'provider_name' => 'Main Provider']
```

## For developers

### Requirements
Expand Down Expand Up @@ -340,4 +370,5 @@ The [php-dto](https://github.com/PetrenkoAnton/php-dto/) library is open-sourced

## Related projects

- [PetrenkoAnton/key-normalizer](https://github.com/PetrenkoAnton/key-normalizer)
- [PetrenkoAnton/php-collection](https://github.com/PetrenkoAnton/php-collection)
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.2
1.2.0
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"require": {
"php": "^8.1",
"petrenkoanton/php-collection": "^1.0"
"petrenkoanton/php-collection": "^1.0",
"petrenkoanton/key-normalizer": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
57 changes: 56 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion docker/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
CONTAINER_NAME=php-dto
CONTAINER_NAME=php-dto
XDEBUG_SESSION=PHPSTORM
XDEBUG_IDE_KEY=PHPSTORM
XDEBUG_SERVER_NAME=localhost
XDEBUG_CLIENT_HOST=host.docker.internal
XDEBUG_START_WITH_REQUEST=trigger # yes | no | trigger
6 changes: 4 additions & 2 deletions docker/docker-compose-php81.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ services:
restart: unless-stopped
working_dir: /var/www/
environment:
- XDEBUG_SESSION=PHPSTORM
- PHP_IDE_CONFIG=serverName=localhost
XDEBUG_SESSION: ${XDEBUG_SESSION}
PHP_IDE_CONFIG: serverName=${XDEBUG_SERVER_NAME}
XDEBUG_CONFIG: "ide_key=${XDEBUG_IDE_KEY} client_host=${XDEBUG_CLIENT_HOST} start_with_request=${XDEBUG_START_WITH_REQUEST}"
volumes:
- ./../:/var/www
- ./ini/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- ./ini/php-opcache.ini:/usr/local/etc/php/conf.d/opcache.ini
- ./ini/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
- ./../tests/_output:/opt/phpstorm-coverage/
networks:
- default

Expand Down
6 changes: 4 additions & 2 deletions docker/docker-compose-php82.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ services:
restart: unless-stopped
working_dir: /var/www/
environment:
- XDEBUG_SESSION=PHPSTORM
- PHP_IDE_CONFIG=serverName=localhost
XDEBUG_SESSION: ${XDEBUG_SESSION}
PHP_IDE_CONFIG: serverName=${XDEBUG_SERVER_NAME}
XDEBUG_CONFIG: "ide_key=${XDEBUG_IDE_KEY} client_host=${XDEBUG_CLIENT_HOST} start_with_request=${XDEBUG_START_WITH_REQUEST}"
volumes:
- ./../:/var/www
- ./ini/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- ./ini/opcache.ini:/usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
- ./ini/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
- ./../tests/_output:/opt/phpstorm-coverage/
networks:
- default

Expand Down
6 changes: 4 additions & 2 deletions docker/docker-compose-php83.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ services:
restart: unless-stopped
working_dir: /var/www/
environment:
- XDEBUG_SESSION=PHPSTORM
- PHP_IDE_CONFIG=serverName=localhost
XDEBUG_SESSION: ${XDEBUG_SESSION}
PHP_IDE_CONFIG: serverName=${XDEBUG_SERVER_NAME}
XDEBUG_CONFIG: "ide_key=${XDEBUG_IDE_KEY} client_host=${XDEBUG_CLIENT_HOST} start_with_request=${XDEBUG_START_WITH_REQUEST}"
volumes:
- ./../:/var/www
- ./ini/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- ./ini/opcache.ini:/usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
- ./ini/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
- ./../tests/_output:/opt/phpstorm-coverage/
networks:
- default

Expand Down
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<directory name="./tests" useStrictTypes="true"/>
<ignoreFiles>
<directory name="./vendor"/>
<directory name="./tests/output"/>
<directory name="./tests/_output"/>
</ignoreFiles>
</projectFiles>
<plugins>
Expand Down
12 changes: 9 additions & 3 deletions src/Dto.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Dto\Exception\Internal\EnumBackingValueException;
use Error;
use InvalidArgumentException;
use KeyNormalizer\KeyNormalizer;
use ReflectionClass;
use ReflectionException;
use ReflectionNamedType;
Expand Down Expand Up @@ -112,7 +113,7 @@ public function __call(string $name, array $arguments): mixed
return $this->{$property};
}

public function toArray(): array
public function toArray(KeyCase $keyCase = KeyCase::CAMEL_CASE): array
{
$properties = (new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PROTECTED);

Expand All @@ -123,8 +124,13 @@ public function toArray(): array
$property->setAccessible(true);
$value = $property->getValue($this);

$data[$property->getName()] = is_a($value, Arrayable::class)
? $value->toArray()
$key = match ($keyCase) {
KeyCase::SNAKE_CASE => KeyNormalizer::toSnakeCase($property->getName()),
KeyCase::CAMEL_CASE => $property->getName(),
};

$data[$key] = is_a($value, Arrayable::class)
? is_a($value, self::class) ? $value->toArray($keyCase) : $value->toArray()
: $value;
}

Expand Down
11 changes: 11 additions & 0 deletions src/KeyCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Dto;

enum KeyCase
{
case CAMEL_CASE;
case SNAKE_CASE;
}
52 changes: 52 additions & 0 deletions tests/DtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Dto\Exception\DtoException;
use Dto\Exception\DtoException\HandleDtoException\GetValueException;
use Dto\Exception\DtoException\SetupDtoException\InputDataException;
use Dto\KeyCase;
use PHPUnit\Framework\TestCase;
use Tests\Fixtures\InfoArrayable;
use Tests\Fixtures\PersonDto;
use Tests\Fixtures\ProductDto;
use Tests\Fixtures\SnakeCaseNestedDto;

use function is_a;
use function sprintf;
Expand Down Expand Up @@ -304,4 +306,54 @@ public function dpInvalidMethod(): array
],
];
}

/**
* @group +
* @dataProvider dpTestToArray
*/
public function testToArray(KeyCase $keyCase, array $expected): void
{
$data = [
'snakeCase' => [
'actualNumber' => 5,
'listInfo' => ['First random string', 'Second random string'],
'infoArrayable' => new InfoArrayable(),
],
'actualNumber' => 10,
'providerName' => 'Main provider',
];

$dto = new SnakeCaseNestedDto($data);
$this->assertEquals($expected, $dto->toArray($keyCase));
}

public static function dpTestToArray(): array
{
return [
[
KeyCase::CAMEL_CASE,
[
'snakeCase' => [
'actualNumber' => 5,
'listInfo' => ['First random string', 'Second random string'],
'infoArrayable' => ['camelCaseKey' => 'value'],
],
'actualNumber' => 10,
'providerName' => 'Main provider',
],
],
[
KeyCase::SNAKE_CASE,
[
'snake_case' => [
'actual_number' => 5,
'list_info' => ['First random string', 'Second random string'],
'info_arrayable' => ['camelCaseKey' => 'value'],
],
'actual_number' => 10,
'provider_name' => 'Main provider',
],
],
];
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/InfoArrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class InfoArrayable implements Arrayable
{
public function toArray(): array
{
return ['key' => 'value'];
return ['camelCaseKey' => 'value'];
}
}
21 changes: 21 additions & 0 deletions tests/Fixtures/SnakeCaseDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Dto\Dto;

/**
* @method int getActualNumber()
* @method array getListInfo()
* @method array getInfoArrayable()
* @psalm-suppress PossiblyUnusedProperty
* @psalm-suppress PropertyNotSetInConstructor
*/
class SnakeCaseDto extends Dto
{
protected int $actualNumber;
protected array $listInfo;
protected array $infoArrayable;
}
21 changes: 21 additions & 0 deletions tests/Fixtures/SnakeCaseNestedDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Dto\Dto;

/**
* @method ProductDto getProduct()
* @method int getActualNumber()
* @method string getProviderName()
* @psalm-suppress PossiblyUnusedProperty
* @psalm-suppress PropertyNotSetInConstructor
*/
class SnakeCaseNestedDto extends Dto
{
protected SnakeCaseDto $snakeCase;
protected int $actualNumber;
protected string $providerName;
}
File renamed without changes.

0 comments on commit f97c57a

Please sign in to comment.