Skip to content

Commit

Permalink
feat: added ignoring of properties (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
vuryss authored Jun 27, 2024
1 parent 5757867 commit 1b7a8a5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 17 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Supports modern PHP projects with fully typed properties. Older codebases with n
- [Serialization groups](#serialization-groups)
- [Deserialization groups](#deserialization-groups)
- [Custom date format](#custom-date-format)
- [Ignore property](#ignore-property)
- [Handling of NULL values](#handling-of-null-values)
- [Build, run & test locally](#build-run--test-locally)

Expand Down Expand Up @@ -149,6 +150,19 @@ $serializer = new Serializer(
);
```

### Ignore property

Those properties will not be included in the serialized values during serialization and will not be populated with
provided values during deserialization.

```php
class SomeClass
{
#[SerializerContext(ignore: true)]
public string $someProperty;
}
```

### Handling of NULL values

- By default, NULL values are included in the serialized value.
Expand Down
1 change: 1 addition & 0 deletions src/Attribute/SerializerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SerializerContext
public function __construct(
public ?string $name = null,
public ?array $typeMap = null,
public bool $ignore = false,
public array $groups = [],
public array $attributes = [],
) {}
Expand Down
10 changes: 5 additions & 5 deletions src/Denormalizer/ObjectDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public function denormalize(
$groups = $attributes[SerializerInterface::ATTRIBUTE_GROUPS] ?? null;

foreach ($classMetadata->properties as $name => $propertyMetadata) {
if (!array_key_exists($propertyMetadata->serializedName, $data)) {
continue;
}

if (null !== $groups && [] === array_intersect($groups, $propertyMetadata->groups)) {
if (
$propertyMetadata->ignore
|| !array_key_exists($propertyMetadata->serializedName, $data)
|| (null !== $groups && [] === array_intersect($groups, $propertyMetadata->groups))
) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions src/Metadata/MetadataExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private function extractPropertyMetadata(
attributes: $serializerContext->attributes,
readAccess: $this->getPropertyReadAccess($reflectionProperty),
writeAccess: $this->getPropertyWriteAccess($reflectionProperty),
ignore: $serializerContext->ignore,
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Metadata/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
public array $attributes,
public ReadAccess $readAccess,
public WriteAccess $writeAccess,
public bool $ignore,
) {
if (ReadAccess::GETTER == $this->readAccess) {
$this->getterMethod = 'get' . ucfirst($this->name);
Expand Down
21 changes: 9 additions & 12 deletions src/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ public function normalize(mixed $data, Normalizer $normalizer, array $attributes
$groups = $attributes[SerializerInterface::ATTRIBUTE_GROUPS] ?? null;

foreach ($classMetadata->properties as $name => $propertyMetadata) {
if (ReadAccess::NONE === $propertyMetadata->readAccess) {
if (
$propertyMetadata->ignore
|| ReadAccess::NONE === $propertyMetadata->readAccess
|| (null !== $groups && [] === array_intersect($groups, $propertyMetadata->groups))
) {
continue;
}

if (null !== $groups && [] === array_intersect($groups, $propertyMetadata->groups)) {
continue;
}

if (ReadAccess::DIRECT === $propertyMetadata->readAccess) {
/** @var scalar|null|object|array $value */
$value = $data->{$name};
} else {
/** @var scalar|null|object|array $value */
$value = $data->{$propertyMetadata->getterMethod}();
}
/** @var scalar|null|object|array $value */
$value = (ReadAccess::DIRECT === $propertyMetadata->readAccess)
? $data->{$name}
: $data->{$propertyMetadata->getterMethod}();

$localAttributes = array_merge($attributes, $propertyMetadata->attributes);

Expand Down
5 changes: 5 additions & 0 deletions tests/Datasets/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Vuryss\Serializer\Tests\Datasets;

use Vuryss\Serializer\Attribute\SerializerContext;

class Person
{
public string $firstName = 'John';
Expand All @@ -12,4 +14,7 @@ class Person
public bool $isStudent = true;

private string $unUsableProperty = 'unUsableProperty';

#[SerializerContext(ignore: true)]
public string $ignoredProperty = 'ignoredProperty';
}
25 changes: 25 additions & 0 deletions tests/Unit/IgnoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Vuryss\Serializer\Tests\Datasets\Person;

test('Can ignore properties on serialization', function () {
$person = new Person();
$serializer = new \Vuryss\Serializer\Serializer();
$json = $serializer->serialize($person);

expect($json)->toBe('{"firstName":"John","lastName":"Doe","age":25,"isStudent":true}');
});

test('Can ignore properties on deserialization', function () {
$json = '{"firstName":"Jane","lastName":"Bla","age":55,"isStudent":false,"ignoredProperty":"differentValue"}';
$serializer = new \Vuryss\Serializer\Serializer();
$person = $serializer->deserialize($json, Person::class);

expect($person->firstName)->toBe('Jane')
->and($person->lastName)->toBe('Bla')
->and($person->age)->toBe(55)
->and($person->isStudent)->toBe(false)
->and($person->ignoredProperty)->toBe('ignoredProperty');
});

0 comments on commit 1b7a8a5

Please sign in to comment.