Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.0.0 #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
strategy:
matrix:
php-version:
- "7.3"
- "7.4"
- "8.0"
runs-on: ubuntu-latest
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
# DTO Bundle
Package for an automatic request to predefined structures conversion in symfony applications.

[![Build Status](https://travis-ci.org/vseinstrumentiru/dto-bundle.svg?branch=master)](https://travis-ci.org/github/vseinstrumentiru/dto-bundle)
[![Coverage Status](https://coveralls.io/repos/github/vseinstrumentiru/dto-bundle/badge.svg?branch=master)](https://coveralls.io/github/vseinstrumentiru/dto-bundle?branch=master)

## Installation

```bash
$ composer require vi-tech/dto-bundle
$ composer require vseinstrumentiru/dto-bundle
```

Declare bundle in configuration:

```php
// config/bundles.php
return [
\ViTech\DataObjectBundle\DataObjectBundle::class => ['all' => true],
\Vseinstrumentiru\DataObjectBundle\DataObjectBundle::class => ['all' => true],
];
```

## Usage

Keep in mind that resolver matches only one strategy at a time and is fully dependent on
Request::getMethod.
**GET** Requests are mapped from query `Request->query->all()` .
Every other method is mapped from request body `Request->request->all()`

> This means that one can not map data from body and query in the same request.
> Normally there should not be such a situation.

```php
<?php

use Symfony\Component\HttpFoundation\Response;
use ViTech\DataObjectBundle\Object\AbstractObject;
use Vseinstrumentiru\DataObjectBundle\Object\AbstractObject;

class RegistrationDto extends AbstractObject
{
Expand Down Expand Up @@ -61,7 +68,7 @@ The simplest way to do that is to declare *constraints* annotations for the same
<?php

use Symfony\Component\HttpFoundation\Response;
use ViTech\DataObjectBundle\Object\AbstractObject;
use Vseinstrumentiru\DataObjectBundle\Object\AbstractObject;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class RegistrationDto extends AbstractObject
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "vi-tech/dto-bundle",
"name": "vseinstrumentiru/dto-bundle",
"description": "DTO resolver for symfony projects",
"minimum-stability": "stable",
"license": "MIT",
Expand All @@ -11,16 +11,16 @@
],
"autoload": {
"psr-4": {
"ViTech\\DataObjectBundle\\": "src/"
"Vseinstrumentiru\\DataObjectBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Test\\ViTech\\DataObjectBundle\\": "test/"
"Test\\Vseinstrumentiru\\DataObjectBundle\\": "test/"
}
},
"require": {
"php": "^7.3|~8.0.0",
"php": "~7.4.0|~8.0.0",
"symfony/dependency-injection": "^4.1|^5.0",
"symfony/http-kernel": "^4.1|^5.1",
"ext-json": "*",
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
<rule ref="Squiz.Commenting.VariableComment.TagNotAllowed">
<severity>0</severity>
</rule>
<rule ref="Squiz.Commenting.VariableComment.MissingVar">
<severity>0</severity>
</rule>
<rule ref="Squiz.Functions.FunctionDuplicateArgument"/>
<rule ref="Squiz.Functions.GlobalFunction"/>
<rule ref="Squiz.Operators.IncrementDecrementUsage"/>
Expand Down
10 changes: 5 additions & 5 deletions src/DataObjectBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace ViTech\DataObjectBundle;
namespace Vseinstrumentiru\DataObjectBundle;

use ViTech\DataObjectBundle\Resolver\RequestObjectResolver;
use Vseinstrumentiru\DataObjectBundle\Resolver\RequestObjectResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -17,11 +17,11 @@ public function build(ContainerBuilder $container)
parent::build($container);

$definition = new Definition(ObjectFactory::class);
$container->setDefinition('vi_tech.dto.object_factory', $definition);
$container->setAlias(ObjectFactoryInterface::class, 'vi_tech.dto.object_factory');
$container->setDefinition('vseinstrumentiru.dto.object_factory', $definition);
$container->setAlias(ObjectFactoryInterface::class, 'vseinstrumentiru.dto.object_factory');

$definition = new Definition(RequestObjectResolver::class, [new Reference(ObjectFactoryInterface::class)]);
$definition->addTag('controller.argument_value_resolver');
$container->setDefinition('vi_tech.dto.resolver.request_object', $definition);
$container->setDefinition('vseinstrumentiru.dto.resolver.request_object', $definition);
}
}
2 changes: 1 addition & 1 deletion src/Exception/ObjectInitError.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ViTech\DataObjectBundle\Exception;
namespace Vseinstrumentiru\DataObjectBundle\Exception;

class ObjectInitError extends \Exception
{
Expand Down
2 changes: 1 addition & 1 deletion src/Object/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ViTech\DataObjectBundle\Object;
namespace Vseinstrumentiru\DataObjectBundle\Object;

use Spatie\DataTransferObject\DataTransferObject;

Expand Down
2 changes: 1 addition & 1 deletion src/Object/DataObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ViTech\DataObjectBundle\Object;
namespace Vseinstrumentiru\DataObjectBundle\Object;

/**
* Data object interface
Expand Down
6 changes: 3 additions & 3 deletions src/ObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace ViTech\DataObjectBundle;
namespace Vseinstrumentiru\DataObjectBundle;

use Throwable;
use ViTech\DataObjectBundle\Exception\ObjectInitError;
use ViTech\DataObjectBundle\Object\AbstractObject;
use Vseinstrumentiru\DataObjectBundle\Exception\ObjectInitError;
use Vseinstrumentiru\DataObjectBundle\Object\AbstractObject;

class ObjectFactory implements ObjectFactoryInterface
{
Expand Down
4 changes: 2 additions & 2 deletions src/ObjectFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace ViTech\DataObjectBundle;
namespace Vseinstrumentiru\DataObjectBundle;

use ViTech\DataObjectBundle\Exception\ObjectInitError;
use Vseinstrumentiru\DataObjectBundle\Exception\ObjectInitError;

interface ObjectFactoryInterface
{
Expand Down
18 changes: 12 additions & 6 deletions src/Resolver/RequestObjectResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

declare(strict_types=1);

namespace ViTech\DataObjectBundle\Resolver;
namespace Vseinstrumentiru\DataObjectBundle\Resolver;

use ViTech\DataObjectBundle\Exception\ObjectInitError;
use ViTech\DataObjectBundle\ObjectFactoryInterface;
use Vseinstrumentiru\DataObjectBundle\Exception\ObjectInitError;
use Vseinstrumentiru\DataObjectBundle\ObjectFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class RequestObjectResolver implements ArgumentValueResolverInterface
{
/** @var ObjectFactoryInterface */
private $objectFactory;
/**
* Dto initializer
*/
private ObjectFactoryInterface $objectFactory;

public function __construct(ObjectFactoryInterface $objectFactory)
{
Expand All @@ -40,7 +42,11 @@ public function resolve(Request $request, ArgumentMetadata $argument)
yield null;
}

$requestData = $request->request->all();
if ($request->isMethod('GET')) {
$requestData = $request->query->all();
} else {
$requestData = $request->request->all();
}

try {
yield $this->objectFactory->createDataObject($requestData, $argument->getType());
Expand Down
14 changes: 4 additions & 10 deletions test/Fixture/CollectionItemDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

declare(strict_types=1);

namespace Test\ViTech\DataObjectBundle\Fixture;
namespace Test\Vseinstrumentiru\DataObjectBundle\Fixture;

use ViTech\DataObjectBundle\Object\AbstractObject;
use Vseinstrumentiru\DataObjectBundle\Object\AbstractObject;

class CollectionItemDto extends AbstractObject
{
/**
* @var string
*/
public $name;
public string $name;

/**
* @var float
*/
public $order;
public float $order;
}
20 changes: 7 additions & 13 deletions test/Fixture/SomeDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

declare(strict_types=1);

namespace Test\ViTech\DataObjectBundle\Fixture;
namespace Test\Vseinstrumentiru\DataObjectBundle\Fixture;

use ViTech\DataObjectBundle\Object\AbstractObject;
use Vseinstrumentiru\DataObjectBundle\Object\AbstractObject;

class SomeDto extends AbstractObject
{
/**
* @var string
*/
public $someProperty;
public string $someProperty;

/**
* Fully qualified namespace is required by spatie/dto
* @var \Test\ViTech\DataObjectBundle\Fixture\SomeSubDto
*/
public $someEmbeddedProperty;
public SomeSubDto $someEmbeddedProperty;

/**
* @var \Test\ViTech\DataObjectBundle\Fixture\CollectionItemDto[]
* Fully qualified namespace is required by spatie/dto.
* @var \Test\Vseinstrumentiru\DataObjectBundle\Fixture\CollectionItemDto[]
*/
public $collectionItems;
public array $collectionItems;
}
14 changes: 4 additions & 10 deletions test/Fixture/SomeSubDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

declare(strict_types=1);

namespace Test\ViTech\DataObjectBundle\Fixture;
namespace Test\Vseinstrumentiru\DataObjectBundle\Fixture;

use ViTech\DataObjectBundle\Object\AbstractObject;
use Vseinstrumentiru\DataObjectBundle\Object\AbstractObject;

class SomeSubDto extends AbstractObject
{
/**
* @var int
*/
public $property1;
public int $property1;

/**
* @var string
*/
public $property2;
public string $property2;
}
Loading