Skip to content

Commit

Permalink
Use the PhpDocExtractor in addition to the ReflectionExtractor. (#125)
Browse files Browse the repository at this point in the history
* Use PhpDocExtractor to deserialize objects

* Fix styling

---------

Co-authored-by: nick-potts <[email protected]>
Co-authored-by: Chris Morrell <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2024
1 parent 408a557 commit b178c35
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/verbs.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
Expand Down Expand Up @@ -42,6 +43,7 @@
'normalizers' => [
SelfSerializingNormalizer::class,
CollectionNormalizer::class,
ArrayDenormalizer::class,
ModelNormalizer::class,
StateNormalizer::class,
BitsNormalizer::class,
Expand Down
8 changes: 7 additions & 1 deletion src/VerbsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Illuminate\Support\DateFactory;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
Expand Down Expand Up @@ -107,7 +109,11 @@ public function packageRegistered()
: new AnnotationLoader;

return new PropertyNormalizer(
propertyTypeExtractor: new ReflectionExtractor,
propertyTypeExtractor: new PropertyInfoExtractor(
typeExtractors: [
new PhpDocExtractor,
new ReflectionExtractor,
]),
classDiscriminatorResolver: new ClassDiscriminatorFromClassMetadata(new ClassMetadataFactory($loader)),
);
});
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/SerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ public function __construct()
expect($result)->toBe('{"__verbs_initialized":false,"name":"Demo"}');
});

it('allows us to store a serializable class(es) as a property', function () {
$original_event = new EventWithPhpDocArray;

$serialized_data = app(Serializer::class)->serialize($original_event);

expect($serialized_data)->toBe('{"dto":{"fqcn":"DTO","foo":1},"dtos":[{"fqcn":"DTO","foo":1}]}');

$deserialized_event = app(Serializer::class)->deserialize(EventWithPhpDocArray::class, $serialized_data);

expect($deserialized_event->dto)->toBeInstanceOf(DTO::class)
->and($deserialized_event->dtos[0])->toBeInstanceOf(DTO::class);
});

class EventWithConstructorPromotion extends Event
{
public function __construct(
Expand Down Expand Up @@ -195,3 +208,12 @@ public function __construct()
$this->constructed = true;
}
}

class EventWithPhpDocArray extends Event
{
public function __construct(
public DTO $dto = new DTO,
/** @var DTO[] $dtos */
public array $dtos = [new DTO]
) {}
}

0 comments on commit b178c35

Please sign in to comment.