Skip to content

[Serializer] Add class/format/context to NameConverterInterface #19818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Changes from all commits
Commits
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
11 changes: 9 additions & 2 deletions serializer/custom_name_converter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,26 @@

class OrgPrefixNameConverter implements NameConverterInterface
{
public function normalize(string $propertyName): string
public function normalize(string $propertyName, string $class = null, ?string $format = null, array $context = []): string

Check failure on line 33 in serializer/custom_name_converter.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please use explicit nullable types.
{
// during normalization, add the prefix
return 'org_'.$propertyName;
}

public function denormalize(string $propertyName): string
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
{
// remove the 'org_' prefix on denormalizing
return str_starts_with($propertyName, 'org_') ? substr($propertyName, 4) : $propertyName;
}
}

.. versionadded:: 7.1

Accessing the current class name, format and context via
:method:`Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface::normalize`
and :method:`Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface::denormalize`
was introduced in Symfony 7.1.

.. note::

You can also implement
Expand Down
Loading