Skip to content

[ObjectMapper] More details on transform: in target class #21156

Open
@tacman

Description

@tacman

The transform examples are all shown on a source class, e.g.

#[Map(target: 'displayPrice', transform: [PriceFormatter::class, 'format'])]
public float $price = 0.0;

https://symfony.com/doc/current/object_mapper.html#transforming-values

If the data is coming from CSV or JSON and does not yet have a class, I'd like to know if it's possible to call a transformer from the target class before the assignment. In particular, I have

{ 
    'price': '$200'
}

And I'd like to transform it to an int without the '$'.

If this is possible, please add an example. If it's not possible, please document that transformers only work from within the source class, not the target class. If that's true, perhaps the #[Map] attribute should throw an error for something like this:

        #[Map(source: 'price'), transform: [self::class, 'cleanupCurrency'])]
        public int $price

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy