Skip to content

Commit

Permalink
Add default renameWith logic to the class attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crell committed Mar 23, 2023
1 parent 2fe4bc7 commit 83e1609
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Support for serializing/deserializing from CSV files.
- Support for stream-serializing to a CSV format.
- Support for specifying a custom format and timezone when serializing DateTime fields.
- Support for making individual fields required when deserializing.
- Support for specifying at the class level that fields are required unless otherwise specified.

### Deprecated
- Nothing
Expand Down
2 changes: 2 additions & 0 deletions src/Attributes/ClassSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Crell\AttributeUtils\ParseMethods;
use Crell\AttributeUtils\ParseProperties;
use Crell\AttributeUtils\SupportsScopes;
use Crell\Serde\Renaming\RenamingStrategy;
use Crell\Serde\TypeMap;
use function Crell\fp\prop;

Expand Down Expand Up @@ -50,6 +51,7 @@ public function __construct(
public readonly bool $includeFieldsByDefault = true,
public readonly array $scopes = [null],
public readonly bool $requireValues = false,
public readonly ?RenamingStrategy $renameWith = null,
) {}

public function fromReflection(\ReflectionClass $subject): void
Expand Down
1 change: 1 addition & 0 deletions src/Attributes/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function fromClassAttribute(object $class): void
{
// If there is no requireValue flag set, inherit it from the class attribute.
$this->requireValue ??= $class->requireValues;
$this->rename ??= $class->renameWith ?? null;
}

protected function getDefaultValueFromConstructor(\ReflectionProperty $subject): mixed
Expand Down
8 changes: 8 additions & 0 deletions tests/ArrayBasedFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,12 @@ public function datetime_fields_support_custom_output_format_validate(mixed $ser
self::assertSame('2022-07-04T18:22:22.000+00:00', $toTest['forceToUtc']);
self::assertSame('2022-07-04T13:22:22.000-05:00', $toTest['forceToChicago']);
}

public function class_level_renaming_applies_validate(mixed $serialized): void
{
$toTest = $this->arrayify($serialized);

self::assertArrayHasKey('foo_string', $toTest);
self::assertArrayHasKey('the_number', $toTest);
}
}
19 changes: 19 additions & 0 deletions tests/Records/ClassWithDefaultRenaming.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Crell\Serde\Records;

use Crell\Serde\Attributes\ClassSettings;
use Crell\Serde\Attributes\Field;
use Crell\Serde\Renaming\Prefix;

#[ClassSettings(renameWith: new Prefix('foo_'))]
class ClassWithDefaultRenaming
{
public function __construct(
public string $string = 'A',
#[Field(serializedName: 'the_number')]
public int $int = 5,
) {}
}
24 changes: 24 additions & 0 deletions tests/SerdeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Crell\Serde\Records\BackedSize;
use Crell\Serde\Records\Callbacks\CallbackHost;
use Crell\Serde\Records\CircularReference;
use Crell\Serde\Records\ClassWithDefaultRenaming;
use Crell\Serde\Records\DateTimeExample;
use Crell\Serde\Records\DictionaryKeyTypes;
use Crell\Serde\Records\Drupal\EmailItem;
Expand Down Expand Up @@ -1467,6 +1468,29 @@ public function missing_required_value_for_class_with_default_does_not_throw():
self::assertEquals('B', $result->b);
}

/**
* @test
*/
public function class_level_renaming_applies(): void
{
$s = new SerdeCommon(formatters: $this->formatters);

$data = new ClassWithDefaultRenaming(string: 'B', int: 12);

$serialized = $s->serialize($data, $this->format);

$this->class_level_renaming_applies_validate($serialized);

$result = $s->deserialize($serialized, from: $this->format, to: $data::class);

self::assertEquals($data, $result);
}

public function class_level_renaming_applies_validate(mixed $serialized): void
{

}

/**
* @test
* @dataProvider scopes_examples()
Expand Down

0 comments on commit 83e1609

Please sign in to comment.