Coder Social home page Coder Social logo

Comments (7)

samdark avatar samdark commented on June 12, 2024

How would you configure it?

from form.

armpogart avatar armpogart commented on June 12, 2024

We will configure some mapper/automapper in container by giving it mappings. If the names of the properties are the same it would be enough and it will allow also extended configuration.

Example from other library (there are multiple php mappers, the basic one will be built-in to the package) :

$config = new AutoMapperConfig();

// Simply registering the mapping is enough to convert properties with the same
// name. Custom actions can be registered for each individual property.
$config
    ->registerMapping(Employee::class, EmployeeDto::class)
    ->forMember('age', function (Employee $source) {
        return date('Y') - $source->getBirthYear();
    })
    ->reverseMap(); // Register the reverse mapping as well.
                            
$mapper = new AutoMapper($config);

// With this configuration we can start converting our objects.
$john = new Employee(10, "John", "Doe", 1980);
$dto = $mapper->map($john, EmployeeDto::class);

echo $dto->firstName; // => "John"
echo $dto->lastName; // => "Doe"
echo $dto->age; // => 37

from form.

samdark avatar samdark commented on June 12, 2024

Can't you, instead, add the following to your custom form model:

final class PersonForm extends FormModel
{
    // usual form stuff

    public function getPerson(): Person
    {
        $person = new Person();
        $person->firstName = $this->firstName;
        $person->lastName = $this->lastName;
        $person->age = date('Y') - $this->birthYear;
        return $person;
    }
}

That's only 6 lines compared to about 10 lines of configuring a mapper...

from form.

armpogart avatar armpogart commented on June 12, 2024

@samdark First of all you will also need the reverse one, so your code example will be larger. Second I am always recommending to do something you mentioned when just starting the project and you are not sure how big your entity and models will grow, how complex they will become, and how many of the you will have. But as practice shows it is a way more convenient to use some sort of automapper for projects with a lot of entity -> model mappings. And in case if you do the same thing with reverse and for a few model -> entity mappings, your code will be to long compared to configuring just automapper (take into account that you initialize mapper just ones and then configure only mappings, which will be just one line for simple models). I'm just proposing to have simple mapper built-in to the package (that will work flawlessly with our model) and not recommending to always use it by default.

If you think that the mapper is out of context of this package, we can simply document the possibility of using one of the mappers already available.

from form.

samdark avatar samdark commented on June 12, 2024

Alright. I forgot about reverse mapping. It makes sense overall. Let's plan it in detail before implementing.

from form.

armpogart avatar armpogart commented on June 12, 2024

Also mapper will help with nested forms (the implementation of nested forms #43). The code becomes very complicated when doing it manually in these cases.

from form.

arogachev avatar arogachev commented on June 12, 2024

This can be related:

from form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.