Skip to content

Commit

Permalink
Merge pull request #50 from superbrave/symfony4
Browse files Browse the repository at this point in the history
Added Symfony 4 compatability
  • Loading branch information
Jelle van Oosterbosch authored Feb 11, 2020
2 parents 3e79983 + a27f81c commit 79f4842
Show file tree
Hide file tree
Showing 18 changed files with 465 additions and 158 deletions.
2 changes: 1 addition & 1 deletion Anonymize/AnonymizerCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function addAnonymizer($type, $anonymizer)
public function getAnonymizer($type)
{
if (!array_key_exists($type, $this->anonymizers)) {
throw new LogicException(sprintf('Anonymizer %s is not registered.', $type));
throw new LogicException(sprintf('Anonymizer "%s" is not registered.', $type));
}

return $this->anonymizers[$type];
Expand Down
5 changes: 3 additions & 2 deletions DependencyInjection/Compiler/AddAnonymizersCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Superbrave\GdprBundle\DependencyInjection\Compiler;

use LogicException;
use Superbrave\GdprBundle\Anonymize\AnonymizerCollection;
use Superbrave\GdprBundle\Anonymize\Type\AnonymizerInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -36,11 +37,11 @@ class AddAnonymizersCompilerPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('superbrave_gdpr.anonymizer_collection') === false) {
if ($container->hasDefinition(AnonymizerCollection::class) === false) {
return;
}

$anonymizerManagerDefinition = $container->getDefinition('superbrave_gdpr.anonymizer_collection');
$anonymizerManagerDefinition = $container->getDefinition(AnonymizerCollection::class);

$anonymizers = $container->findTaggedServiceIds('superbrave_gdpr.anonymizer');
foreach ($anonymizers as $anonymizer => $attributes) {
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/SuperbraveGdprExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.yml');
$loader->load('services.exporter.types.yml');
$loader->load('services.anonymizer.types.yml');
$loader->load('legacy_aliases.yml');
}
}
7 changes: 7 additions & 0 deletions Resources/config/legacy_aliases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
superbrave_gdpr.annotation.reader: '@Superbrave\GdprBundle\Annotation\AnnotationReader'
superbrave_gdpr.exporter: '@Superbrave\GdprBundle\Export\Exporter'
superbrave_gdpr.anonymizer: '@Superbrave\GdprBundle\Anonymize\Anonymizer'
superbrave_gdpr.property_manipulator: '@Superbrave\GdprBundle\Manipulator\PropertyManipulator'
superbrave_gdpr.anonymizer_collection: '@Superbrave\GdprBundle\Anonymize\AnonymizerCollection'
superbrave_gdpr.property_anonymizer: '@Superbrave\GdprBundle\Anonymize\PropertyAnonymizer'
8 changes: 4 additions & 4 deletions Resources/config/services.anonymizer.types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
superbrave_gdpr.fixed_anonymizer:
class: Superbrave\GdprBundle\Anonymize\Type\FixedAnonymizer
arguments:
- "@superbrave_gdpr.property_manipulator"
- '@Superbrave\GdprBundle\Manipulator\PropertyManipulator'
tags:
- { name: superbrave_gdpr.anonymizer, type: fixed }

Expand All @@ -22,18 +22,18 @@ services:
superbrave_gdpr.object_anonymizer:
class: Superbrave\GdprBundle\Anonymize\Type\ObjectAnonymizer
arguments:
- "@superbrave_gdpr.anonymizer"
- '@Superbrave\GdprBundle\Anonymize\Anonymizer'
tags:
- { name: superbrave_gdpr.anonymizer, type: object }

superbrave_gdpr.collection_anonymizer:
class: Superbrave\GdprBundle\Anonymize\Type\ArrayAnonymizer
arguments:
- "@superbrave_gdpr.object_anonymizer"
- '@superbrave_gdpr.object_anonymizer'
tags:
- { name: superbrave_gdpr.anonymizer, type: collection }

superbrave_gdpr.null_anonymizer:
class: Superbrave\GdprBundle\Anonymize\Type\NullAnonymizer
tags:
- { name: superbrave_gdpr.anonymizer, type: "null" }
- { name: superbrave_gdpr.anonymizer, type: 'null' }
25 changes: 17 additions & 8 deletions Resources/config/services.exporter.types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ services:
_defaults:
public: false

superbrave_gdpr.exporter.serializer.normalizer.export_annotation:
class: Superbrave\GdprBundle\Serializer\Normalizer\AnnotationNormalizer
superbrave_gdpr.exporter.serializer.normalizer.iterable_normalizer:
class: Superbrave\GdprBundle\Serializer\Normalizer\IterableNormalizer
tags:
- { name: 'superbrave_gdpr.serializer.normalizer' }

superbrave_gdpr.exporter.serializer.normalizer.datetime_normalizer:
class: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer
tags:
- { name: 'superbrave_gdpr.serializer.normalizer' }

Superbrave\GdprBundle\Serializer\Normalizer\AnnotationNormalizer:
arguments:
- "@superbrave_gdpr.annotation.reader"
- "Superbrave\\GdprBundle\\Annotation\\Export"
- "@superbrave_gdpr.property_manipulator"
- '@Superbrave\GdprBundle\Annotation\AnnotationReader'
- 'Superbrave\GdprBundle\Annotation\Export'
- '@Superbrave\GdprBundle\Manipulator\PropertyManipulator'
tags:
- { name: "superbrave_gdpr.serializer.normalizer" }
- { name: 'superbrave_gdpr.serializer.normalizer' }

superbrave_gdpr.exporter.serializer.encoder.xml:
class: Symfony\Component\Serializer\Encoder\XmlEncoder
tags:
- { name: "superbrave_gdpr.serializer.encoder" }
- { name: 'superbrave_gdpr.serializer.encoder' }

superbrave_gdpr.exporter.serializer.encoder.json:
class: Symfony\Component\Serializer\Encoder\JsonEncoder
tags:
- { name: "superbrave_gdpr.serializer.encoder" }
- { name: 'superbrave_gdpr.serializer.encoder' }
36 changes: 15 additions & 21 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,37 @@ services:
_defaults:
public: false

superbrave_gdpr.annotation.reader:
class: Superbrave\GdprBundle\Annotation\AnnotationReader
Superbrave\GdprBundle\Annotation\AnnotationReader:

superbrave_gdpr.exporter:
class: Superbrave\GdprBundle\Export\Exporter
Superbrave\GdprBundle\Export\Exporter:
arguments:
- "@superbrave_gdpr.exporter.serializer"
- '@superbrave_gdpr.exporter.serializer'
public: true

superbrave_gdpr.anonymizer:
class: Superbrave\GdprBundle\Anonymize\Anonymizer
Superbrave\GdprBundle\Anonymize\Anonymizer:
arguments:
- "@superbrave_gdpr.annotation.reader"
- "@superbrave_gdpr.property_anonymizer"
- '@Superbrave\GdprBundle\Annotation\AnnotationReader'
- '@Superbrave\GdprBundle\Anonymize\PropertyAnonymizer'
public: true

superbrave_gdpr.exporter.serializer:
class: Symfony\Component\Serializer\Serializer
factory: ["Superbrave\\GdprBundle\\DependencyInjection\\ObjectFactory", "createSerializer"]
factory: ['Superbrave\GdprBundle\DependencyInjection\ObjectFactory', 'createSerializer']
arguments:
- !tagged "superbrave_gdpr.serializer.normalizer"
- !tagged "superbrave_gdpr.serializer.encoder"
- !tagged 'superbrave_gdpr.serializer.normalizer'
- !tagged 'superbrave_gdpr.serializer.encoder'

superbrave_gdpr.property_accessor:
class: Symfony\Component\PropertyAccess\PropertyAccessor

superbrave_gdpr.property_manipulator:
class: Superbrave\GdprBundle\Manipulator\PropertyManipulator
Superbrave\GdprBundle\Manipulator\PropertyManipulator:
arguments:
- "@superbrave_gdpr.property_accessor"
- '@superbrave_gdpr.property_accessor'

superbrave_gdpr.anonymizer_collection:
class: Superbrave\GdprBundle\Anonymize\AnonymizerCollection
Superbrave\GdprBundle\Anonymize\AnonymizerCollection:
public: true

superbrave_gdpr.property_anonymizer:
class: Superbrave\GdprBundle\Anonymize\PropertyAnonymizer
Superbrave\GdprBundle\Anonymize\PropertyAnonymizer:
arguments:
- "@superbrave_gdpr.property_manipulator"
- "@superbrave_gdpr.anonymizer_collection"
- '@Superbrave\GdprBundle\Manipulator\PropertyManipulator'
- '@Superbrave\GdprBundle\Anonymize\AnonymizerCollection'
21 changes: 15 additions & 6 deletions Serializer/Normalizer/AnnotationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
use Superbrave\GdprBundle\Annotation\AnnotationReader;
use Superbrave\GdprBundle\Manipulator\PropertyManipulator;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* Normalizes object data based on the specified property annotation.
*
* @author Niels Nijens <nn@superbrave.nl>
* @author Jelle van Oosterbosch <jvo@superbrave.nl>
*/
class AnnotationNormalizer implements NormalizerInterface
class AnnotationNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

/**
* The AnnotationReader instance.
*
Expand Down Expand Up @@ -71,6 +76,8 @@ public function __construct(
* @param string $format The format being (de-)serialized from or into
*
* @return bool
*
* @throws \ReflectionException
*/
public function supportsNormalization($data, $format = null)
{
Expand All @@ -94,6 +101,8 @@ public function supportsNormalization($data, $format = null)
* @param array $context Context options for the normalizer
*
* @return array|string|int|float|bool
*
* @throws \ReflectionException
*/
public function normalize($object, $format = null, array $context = [])
{
Expand All @@ -109,7 +118,11 @@ public function normalize($object, $format = null, array $context = [])
$propertyName = $propertyAnnotation->alias;
}

$normalizedData[$propertyName] = $this->getMappedPropertyValue($propertyAnnotation, $propertyValue);
if (false === is_scalar($propertyValue)) {
$normalizedData[$propertyName] = $this->normalizer->normalize($propertyValue, $format, $context);
} else {
$normalizedData[$propertyName] = $this->getMappedPropertyValue($propertyAnnotation, $propertyValue);
}
}

return $normalizedData;
Expand All @@ -125,10 +138,6 @@ public function normalize($object, $format = null, array $context = [])
*/
private function getMappedPropertyValue($annotation, $propertyValue)
{
if (is_scalar($propertyValue) === false) {
return $propertyValue;
}

if (property_exists($annotation, 'valueMap') === false || isset($annotation->valueMap) === false) {
return $propertyValue;
}
Expand Down
54 changes: 54 additions & 0 deletions Serializer/Normalizer/IterableNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* This file is part of the GDPR bundle.
*
* @category Bundle
*
* @author SuperBrave <info@superbrave.nl>
* @copyright 2018 SuperBrave <info@superbrave.nl>
* @license https://github.com/superbrave/gdpr-bundle/blob/master/LICENSE MIT
*
* @see https://www.superbrave.nl/
*/

namespace Superbrave\GdprBundle\Serializer\Normalizer;

use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* Normalizes data if it's iterable by calling the normalizer chain.
*
* @author Jelle van Oosterbosch <jvo@superbrave.nl>
*/
class IterableNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
if (is_array($data) || $data instanceof \Traversable) {
return true;
}

return false;
}

/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = [])
{
$normalizedData = [];

foreach ($object as $value) {
$normalizedData[] = $this->normalizer->normalize($value, $format, $context);
}

return $normalizedData;
}
}
20 changes: 20 additions & 0 deletions Tests/AnnotatedMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Class used to test the @see GDPR\AnnotationReader.
*
* @author Niels Nijens <nn@superbrave.nl>
* @author Jelle van Oosterbosch <jvo@superbrave.nl>
*/
class AnnotatedMock
{
Expand Down Expand Up @@ -60,6 +61,15 @@ class AnnotatedMock
*/
private $quux;

/**
* The quuz property.
*
* @GDPR\Export()
*
* @var \DateTime
*/
private $quuz;

/**
* The property that is annotated with the Export annotation, but without getter method.
*
Expand Down Expand Up @@ -91,8 +101,10 @@ public function __construct(AnnotatedMock $annotatedMock = null)
$elements = [];
if ($annotatedMock instanceof AnnotatedMock) {
$elements[] = $annotatedMock;
$elements[] = clone $annotatedMock;
}

$this->quuz = new \DateTime('2016/01/01');
$this->quux = new ArrayCollection($elements);
}

Expand Down Expand Up @@ -136,6 +148,14 @@ public function getQuux()
return $this->quux;
}

/**
* @return \DateTime
*/
public function getQuuz()
{
return $this->quuz;
}

/**
* Returns the value of the notAnnotatedProperty property.
*
Expand Down
10 changes: 6 additions & 4 deletions Tests/Annotation/AnnotationReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* AnnotationReaderTest.
*
* @author Niels Nijens <nn@superbrave.nl>
* @author Jelle van Oosterbosch <jvo@superbrave.nl>
*/
class AnnotationReaderTest extends TestCase
{
Expand Down Expand Up @@ -65,9 +66,9 @@ public function testGetPropertiesWithAnnotationExport(): void
);

$this->assertIsArray($result);
$this->assertCount(5, $result);
$this->assertCount(6, $result);
$this->assertSame(
['foo', 'baz', 'qux', 'quux', 'annotatedPropertyWithoutMethod'],
['foo', 'baz', 'qux', 'quux', 'quuz', 'annotatedPropertyWithoutMethod'],
array_keys($result)
);
$this->assertInstanceOf(Export::class, current($result));
Expand Down Expand Up @@ -109,11 +110,12 @@ public function testGetPropertiesWithAnnotationForExtendedClass(): void
);

$this->assertIsArray($result);
$this->assertCount(6, $result);
$this->assertCount(7, $result);
$this->assertSame(
['extendedProperty', 'foo', 'baz', 'qux', 'quux', 'annotatedPropertyWithoutMethod'],
['extendedProperty', 'foo', 'baz', 'qux', 'quux', 'quuz', 'annotatedPropertyWithoutMethod'],
array_keys($result)
);

$this->assertInstanceOf(Export::class, current($result));
}
}
1 change: 1 addition & 0 deletions Tests/Resources/json/annotation_normalize_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo":"bar","baz":1,"qux":[],"quuxs":[{"foo":"bar","baz":1,"qux":[],"quuxs":[],"quuz":"2016-01-01T00:00:00+00:00","annotatedPropertyWithoutMethod":"Yes"},{"foo":"bar","baz":1,"qux":[],"quuxs":[],"quuz":"2016-01-01T00:00:00+00:00","annotatedPropertyWithoutMethod":"Yes"}],"quuz":"2016-01-01T00:00:00+00:00","annotatedPropertyWithoutMethod":"Yes"}
1 change: 1 addition & 0 deletions Tests/Resources/json/iterable_normalize_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["bar",1,[["2020-01-01T00:00:00+00:00","2020-01-01T00:00:00+00:00"]]]
2 changes: 1 addition & 1 deletion Tests/Resources/xml/annotation_normalizer_result.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0"?>
<mock><foo>bar</foo><baz>1</baz><qux/><quuxs><item key="0"><foo>bar</foo><baz>1</baz><qux/><quuxs/><annotatedPropertyWithoutMethod>Yes</annotatedPropertyWithoutMethod></item></quuxs><annotatedPropertyWithoutMethod>Yes</annotatedPropertyWithoutMethod></mock>
<mock><foo>bar</foo><baz>1</baz><qux/><quuxs><foo>bar</foo><baz>1</baz><qux/><quuxs/><quuz>2016-01-01T00:00:00+00:00</quuz><annotatedPropertyWithoutMethod>Yes</annotatedPropertyWithoutMethod></quuxs><quuxs><foo>bar</foo><baz>1</baz><qux/><quuxs/><quuz>2016-01-01T00:00:00+00:00</quuz><annotatedPropertyWithoutMethod>Yes</annotatedPropertyWithoutMethod></quuxs><quuz>2016-01-01T00:00:00+00:00</quuz><annotatedPropertyWithoutMethod>Yes</annotatedPropertyWithoutMethod></mock>
Loading

0 comments on commit 79f4842

Please sign in to comment.