Code Monkey home page Code Monkey logo

automapper-bundle's Issues

Custom model transformer not being applied

I have created this model transformer:

<?php

namespace App\AutoMapper;

use App\ApiResource\JobApi;
use App\Entity\Job;
use AutoMapper\Transformer\CustomTransformer\CustomModelTransformerInterface;
use Symfony\Component\PropertyInfo\Type;

final readonly class JobToJobApiCustomTransformer implements CustomModelTransformerInterface
{
    public function supports(array $sourceTypes, array $targetTypes): bool
    {
        return $this->hasType($sourceTypes, Job::class) && $this->hasType($targetTypes, JobApi::class);
    }

    /**
     * @param object|array<int, mixed> $source
     * @return JobApi
     * @throws \Exception
     */
    public function transform(object|array $source): JobApi
    {
        if (!($source instanceof Job)) {
            throw new \Exception(sprintf('Expected instance of %s, %s given', Job::class, gettype($source)));
        }

        $target = new JobApi();

        $target->setId($source->getId());
        $target->setUser($source->getUser());
       
        // more...

        return $target;
    }

    /**
     * @param Type[] $types
     * @param class-string $class
     */
    private function hasType(array $types, string $class): bool
    {
        foreach ($types as $type) {
            if ($type->getClassName() === $class) {
                return true;
            }
        }

        return false;
    }
}

And using it as:

/** @var ArrayCollection<int, JobApi> $jobs */
$jobs = new ArrayCollection();

/** @var Job $item */
foreach ($pagination->getItems() as $item) {
    /** @var JobApi $job */
    $job = $this->autoMapper->map($item, JobApi::class);
    $jobs->add($job);
}

it just doesn't find the custom transformer at all. It does find custom property transformers as I use them but this custom model transformer is nowhere to be applied...

I tested this for a few hours, no matter how I put it, I can't trigger the custom transformer at all.

@Korbeil - can you please check this?

Error when installing on Symfony 7

On a new Symfony 7 project, composer require works until we reach "Executing script cache:clear" and then fails with:

In CheckExceptionOnInvalidReferenceBehaviorPass.php line 85:
The service "automapper.property_info.cache.inner" has a dependency on a non-existent service "property_info.phpstan_extractor". Did you mean one of these: "property_info.serializer_extractor", "property_info.reflection_extractor"?

This turns out to be an error Google knows nothing about :)

Tested with bundle versions v8.0.2 and dev-main 9c51bde

Automapper-bundle Installation Fatal Error in Symfony

Description:
Here is the error that I'm encountering while trying to install the automapper-bundle in Symfony:
PHP Fatal error: Class AutoMapper\Extractor\MapToContextPropertyInfoExtractorDecorator contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Component\PropertyInfo\Extractor\ConstructorArgumentTypeExtractorInterface::getTypeFromConstructor) in /Users/mouradchab/Sites/sf71-serializer-turbo-2/vendor/jolicode/automapper/src/Extractor/MapToContextPropertyInfoExtractorDecorator.php on line 22

Steps to reproduce:
Create a new Symfony project using version 7.1.x-dev
Install the automapper-bundle with composer

See error
Additional details:
My environment details:
Operating System: macOS Sonoma
PHP version: 8.2

Dev mode and cache

I am very new to using this bundle, so I might be wrong, but after working with it for a day, it seems that in dev mode, if the mapped class files have been generated, they're there to stay, you need to clear the app cache in order to have changes reflected. This isn't really a good DX on dev, they should be generated only if the sources/targets/transformers have changed.

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.