Skip to content
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

Inline virtual property #1558

Open
Bilge opened this issue Sep 23, 2024 · 5 comments
Open

Inline virtual property #1558

Bilge opened this issue Sep 23, 2024 · 5 comments

Comments

@Bilge
Copy link

Bilge commented Sep 23, 2024

Q A
Bug report? yes
Feature request? yes
BC Break report? no
RFC? no

How is one supposed to inline a virtual property? The following does not work:

#[Serializer\VirtualProperty(name: 'xD', exp: 'object.format("c")', options: [[Serializer\Inline::class, []]])]
class Foo extends \DateTimeImmutable { ... }

(Aside, it shouldn't be necessary to specify a name for an inlined property, nor should it be necessary to specify an empty array after the inline class).

Expected Result

  • {Foo: "2026-12-31T12:30:00+00:00"}

Actual Result

  • {Foo: {}}
@scyzoryck
Copy link
Collaborator

Hi.
Can you provide full working example of the code that you are trying to execute and achieve?
In general you are trying to Inline the property that has string type - when Inline was designed to work with nested objects.
If you think that we are missing some feature feel free to provide some Pull Request with changes. I will be glad to help to add this code to the next release.

Best Marcin.

@Bilge
Copy link
Author

Bilge commented Sep 23, 2024

The purpose of what I was trying to achieve is just to have an object be represented as some value, without having to specify in everything that consumes it how to represent it. With VirtualProperty and a bunch of Excludes as necessary, we can represent the object as a single key/value pair, but I just want a value with no key (i.e. the formatted date of the DateTimeImmutable object).

@scyzoryck
Copy link
Collaborator

I would suggest to use custom handler for such classes - see DateHandler as an example. I believe it will be an easier solution to maintain in a long term but also better from performance point of view.

Best Marcin

@Bilge
Copy link
Author

Bilge commented Sep 23, 2024

Perhaps DateHandler should automatically apply to objects extending DateTime/DateTimeImmutable, instead of only matching the class names verbatim.

@scyzoryck
Copy link
Collaborator

Handlers are meant to subscribe to specific class only - they do not apply to the child classes. Event listeners can apply to child classes and interfaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants