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

unwrap child class instance #1064

Closed
lsmith77 opened this issue Mar 21, 2019 · 2 comments
Closed

unwrap child class instance #1064

lsmith77 opened this issue Mar 21, 2019 · 2 comments

Comments

@lsmith77
Copy link
Contributor

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

I am wondering if there is a more elegant way to handle:

class Foo
{
    /**
     * @Serializer\Exclude()
     */
    private $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }

    /**
     * @Serializer\VirtualProperty()
     * @Serializer\SerializedName("Hello")
     * @Serializer\XmlElement(cdata=false)
     */
    public function getHello()
    {
        return $this->bar->world;
    }

There is obviously with annotating Bar to use Hello as the serialization name of the world property.

class Foo
{
    /**
     * @Serializer\Inline()
     */
    private $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }

But what if I do not want to define the mapping of world to Hello within the context of Foo class only?

Would something like the following make sense?

class Foo
{
    /**
     * @Serializer\Inline(cdata=false, map = {"Hello": "world"})
     */
    private $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
@lsmith77
Copy link
Contributor Author

I found a slightly better work around for the current status quo

class Foo
{
    /**
     * @Serializer\Exclude()
     */
    private $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }

    /**
     * @Serializer\VirtualProperty()
     * @Serializer\XmlElement(cdata=false)
     * @Serializer\XmlKeyValuePairs()
     * @Serializer\Inline()
     */
    public function getBar()
    {
        return [
            'Hello' => $this->bar->world,
            ..
        ];
    }

}

@lsmith77
Copy link
Contributor Author

I guess given this, I would say expanding @Inline is probably not sensible.

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

1 participant