Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

v3.0.0 #25

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Changes from 10 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
44b43eb
[WIP] Begin v3.0.0
NicolasGuilloux May 20, 2020
d708f90
Fix circular services injection
NicolasGuilloux May 22, 2020
3475897
Improve normalizer
NicolasGuilloux May 29, 2020
1f3e37d
Merge branch 'master' into v3.0.0
Jun 17, 2020
10af235
Try to be minimalistic in updateCollection, add an optional argument …
Jun 29, 2020
7d59a1c
Fix bad EntityIdNormalizer context tag in dto normalizer
NicolasGuilloux Aug 27, 2020
e8d04be
Fix systematic entity id normalization on dto
NicolasGuilloux Aug 27, 2020
4fe13a3
Allow merging from new list to entity list in updateCollection
mdevlamynck Sep 7, 2020
02079db
Fix a small issue in the normalizer
NicolasGuilloux Nov 23, 2020
31746c7
Merge branch 'v3.0.0' of github.com:NicolasGuilloux/chaplean-dto-hand…
NicolasGuilloux Nov 23, 2020
4a04a7a
Merge branch 'master' into v3.0.0
mdevlamynck Jan 5, 2021
28cf00f
Fix a instanciation bug when no value is given
NicolasGuilloux Jan 27, 2021
529fe08
Merge branch 'v3.0.0' of github.com:NicolasGuilloux/chaplean-dto-hand…
NicolasGuilloux Jan 27, 2021
257fb0f
Fix array convertion
Apr 16, 2021
714a10c
Enlarge compatibility to Symfony 5, fix tests
NicolasGuilloux Jul 8, 2021
77c8889
Merge branch 'v3.0.0' of github.com:NicolasGuilloux/chaplean-dto-hand…
NicolasGuilloux Jul 8, 2021
cb6f0f8
Extends compatibility to Symfony 5 fuly with attributes
NicolasGuilloux Jul 8, 2021
ca486fb
Fix PHPUnit in CI
NicolasGuilloux Jul 8, 2021
3be8499
Fix translator interface class
NicolasGuilloux Jul 8, 2021
28a8f8f
Fix composer PHP dependency
NicolasGuilloux Jul 20, 2021
db30764
Add SubKey Annotation
Jan 31, 2022
3a6546c
Add SubKey Annotation
Jan 31, 2022
cb0b9b1
Add new validation groups key in context
Feb 4, 2022
594b473
Edit SubKey condition
Feb 7, 2022
f967f8f
Add AbstractClassResolverInterface
Apr 26, 2022
42b6cf2
Add OBJECT_TO_POPULATE option
Apr 29, 2022
ed73b47
Fix compiler pass
May 3, 2022
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
26 changes: 0 additions & 26 deletions Annotation/DTO.php

This file was deleted.

12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 3.0.0

Breaking Change:
* The DTO annotation has been removed.
* You now need to implement the `DataTransferObjectInterface` to your DTO
* `DtoUtility::loadArrayToDto` is now available through the Symfony Serializer using the denormalizer.
* `DtoUtility::updateEntityList` is now available at `EntityUtils::updateCollection`
* You no longer need to declare the DTO as a service

New feature:
* Implementation of a DTO normalizer/denormalizer for Symfony's Serializer

## 2.3.2

Bug fix:
11 changes: 0 additions & 11 deletions ChapleanDtoHandlerBundle.php
Original file line number Diff line number Diff line change
@@ -11,8 +11,6 @@

namespace Chaplean\Bundle\DtoHandlerBundle;

use Chaplean\Bundle\DtoHandlerBundle\DependencyInjection\Compiler\DataTransferObjectPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
@@ -25,13 +23,4 @@
*/
class ChapleanDtoHandlerBundle extends Bundle
{
/**
* @param ContainerBuilder $container
*
* @return void
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new DataTransferObjectPass());
}
}
14 changes: 14 additions & 0 deletions DataTransferObject/DataTransferObjectInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Chaplean\Bundle\DtoHandlerBundle\DataTransferObject;

/**
* Interface DataTransferObjectInterface
*
* @package Chaplean\Bundle\DtoHandlerBundle\DataTransferObject
* @author Nicolas Guilloux <nguilloux@richcongress.com>
* @copyright 2014 - 2020 RichCongress (https://www.richcongress.com)
*/
interface DataTransferObjectInterface
{
}
11 changes: 9 additions & 2 deletions DependencyInjection/ChapleanDtoHandlerExtension.php
Original file line number Diff line number Diff line change
@@ -16,6 +16,13 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* Class ChapleanDtoHandlerExtension
*
* @package Chaplean\Bundle\DtoHandlerBundle\DependencyInjection
* @author Nicolas Guilloux <nguilloux@richcongress.com>
* @copyright 2014 - 2020 RichCongress (https://www.richcongress.com)
*/
class ChapleanDtoHandlerExtension extends Extension
{
/**
@@ -33,8 +40,8 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

$container->setParameter('chaplean_dto_handler', $config);
$this->setParameters($container, 'chaplean_dto_handler', $config);
$container->setParameter(Configuration::CONFIG_NODE, $config);
$this->setParameters($container, Configuration::CONFIG_NODE, $config);
}

/**
31 changes: 0 additions & 31 deletions DependencyInjection/Compiler/DataTransferObjectPass.php

This file was deleted.

8 changes: 6 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -20,13 +20,17 @@
*/
final class Configuration implements ConfigurationInterface
{
public const CONFIG_NODE = 'chaplean_dto_handler';

/**
* @return TreeBuilder
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('chaplean_dto_handler');
$treeBuilder = new TreeBuilder(self::CONFIG_NODE);
$rootNode = \method_exists(TreeBuilder::class, 'getRootNode')
? $treeBuilder->getRootNode()
: $treeBuilder->root(self::CONFIG_NODE);

$rootNode
->children()
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
use Chaplean\Bundle\DtoHandlerBundle\Exception\DataTransferObjectValidationException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
@@ -27,13 +27,13 @@ public static function getSubscribedEvents(): array
}

/**
* @param GetResponseForExceptionEvent $event
* @param ExceptionEvent $event
*
* @return void
*/
public function onKernelException(GetResponseForExceptionEvent $event): void
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getException();
$exception = $event->getThrowable();

if (!$exception instanceof DataTransferObjectValidationException) {
return;
17 changes: 2 additions & 15 deletions ParamConverter/DataTransferObjectParamConverter.php
Original file line number Diff line number Diff line change
@@ -11,11 +11,10 @@

namespace Chaplean\Bundle\DtoHandlerBundle\ParamConverter;

use Chaplean\Bundle\DtoHandlerBundle\Annotation\DTO;
use Chaplean\Bundle\DtoHandlerBundle\ConfigurationExtractor\PropertyConfigurationExtractor;
use Chaplean\Bundle\DtoHandlerBundle\DataTransferObject\DataTransferObjectInterface;
use Chaplean\Bundle\DtoHandlerBundle\Exception\DataTransferObjectValidationException;
use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager;
@@ -182,9 +181,6 @@ public function apply(Request $originalRequest, ParamConverter $configuration):
* @param ParamConverter $configuration
*
* @return boolean
*
* @throws \ReflectionException
* @throws AnnotationException
*/
public function supports(ParamConverter $configuration): bool
{
@@ -194,16 +190,7 @@ public function supports(ParamConverter $configuration): bool
return false;
}

if (\in_array($class, $this->taggedDtoClasses, true)) {
return true;
}

$propertyReflectionClass = new \ReflectionClass($class);

$annotationReader = new AnnotationReader();
$typeAnnotation = $annotationReader->getClassAnnotation($propertyReflectionClass, DTO::class);

return $typeAnnotation !== null;
return is_subclass_of($class, DataTransferObjectInterface::class);
}

/**
14 changes: 12 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -20,10 +20,20 @@
<tag name="request.param_converter" converter="data_transfer_object_converter" />
</service>

<service id="Chaplean\Bundle\DtoHandlerBundle\Utility\DtoUtility">
<service id="Chaplean\Bundle\DtoHandlerBundle\Validator\Constraints\UniqueEntityDataValidator" />

<!-- Serializer -->
<service id="Chaplean\Bundle\DtoHandlerBundle\Serializer\DataTransferObjectDenormalizer">
<argument key="$paramConverterManager" type="service" id="sensio_framework_extra.converter.manager" />
<tag name="serializer.denormalizer" priority="50" />
</service>
<service id="Chaplean\Bundle\DtoHandlerBundle\Serializer\DataTransferObjectNormalizer" lazy="true">
<argument key="$normalizer" type="service" id="serializer" />
<tag name="serializer.normalizer" priority="50" />
</service>
<service id="Chaplean\Bundle\DtoHandlerBundle\Serializer\EntityIdNormalizer">
<tag name="serializer.normalizer" priority="50" />
</service>

<service id="Chaplean\Bundle\DtoHandlerBundle\Validator\Constraints\UniqueEntityDataValidator" />
</services>
</container>
78 changes: 78 additions & 0 deletions Serializer/DataTransferObjectDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php declare(strict_types=1);

namespace Chaplean\Bundle\DtoHandlerBundle\Serializer;

use Chaplean\Bundle\DtoHandlerBundle\DataTransferObject\DataTransferObjectInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

/**
* Class DataTransferObjectDenormalizer
*
* @package Chaplean\Bundle\DtoHandlerBundle\Serializer
* @author Nicolas Guilloux <nguilloux@richcongress.com>
* @copyright 2014 - 2020 RichCongress (https://www.richcongress.com)
*/
class DataTransferObjectDenormalizer implements DenormalizerInterface
{
public const VALIDATION_CONTEXT_KEY = '_dto_validation';

/**
* @var ParamConverterManager
*/
protected $paramConverterManager;

/**
* DtoUtility constructor.
*
* @param ParamConverterManager $paramConverterManager
*/
public function __construct(ParamConverterManager $paramConverterManager)
{
$this->paramConverterManager = $paramConverterManager;
}

/**
* @param mixed $data
* @param string $type
* @param null $format
* @param array $context
*
* @return array|object|void
*/
public function denormalize($data, $type, $format = null, array $context = [])
{
$request = new Request(
[],
$data,
[$type => 'dto']
);

$config = new ParamConverter([]);
$config->setName('dto');
$config->setClass($type);
$config->setIsOptional(false);
$config->setConverter('data_transfer_object_converter');
$config->setOptions([
'validate' => $context[self::VALIDATION_CONTEXT_KEY] ?? true
]);

$this->paramConverterManager->apply($request, $config);

return $request->get('dto');
}

/**
* @param mixed $data
* @param string $type
* @param null $format
*
* @return bool|void
*/
public function supportsDenormalization($data, $type, $format = null): bool
{
return is_array($data) && is_subclass_of($type, DataTransferObjectInterface::class);
}
}
Loading