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

How retrieve types mapped in TypeMapperServiceProvider? #65

Closed
V-Orbit opened this issue Oct 28, 2019 · 2 comments
Closed

How retrieve types mapped in TypeMapperServiceProvider? #65

V-Orbit opened this issue Oct 28, 2019 · 2 comments
Labels
question Further information is requested

Comments

@V-Orbit
Copy link

V-Orbit commented Oct 28, 2019

Hi Guys, this is driving me crazy.

    /**
     * A list of class names implementing \Swis\JsonApi\Client\Interfaces\ItemInterface.
     *
     * @var string[]
     */
    protected $items = [
        CampagneItem::class,
        OrdiniItem::class,
        GruppoOfferteItem::class,
        ClientiItem::class,
        AnagraficaClientiItem::class,
        UserItem::class,
        FormOfferteItem::class,
        OrderStatusItem::class
    ];

    /**
     * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface $typeMapper
     */
    public function boot(TypeMapperInterface $typeMapper) {

        foreach ($this->items as $class) {
            /** @var \Swis\JsonApi\Client\Interfaces\ItemInterface $item */
            $item = $this->app->make($class);


            $typeMapper->setMapping($item->getType(), $class);
        }
    }
}

in my custom controller how can i access to these mapped classes?
if i write $typeMapper = app(TypeMapper::class) is empty of course...
Sorry if question is stupid but I don't have noone to ask to...

@JaZo
Copy link
Member

JaZo commented Oct 29, 2019

Hi @V-Orbit, thanks for reaching out! You should use TypeMapperInterface, which is registered as singleton, instead of TypeMapper e.g.

$typeMapper = app(TypeMapperInterface::class);
$campagne = $typeMapper->getMapping('campagne'); // new instance of CampagneItem (assuming that's the type of the item...)

I'll leave this issue open to clarify this in the docs.

@JaZo JaZo added the question Further information is requested label Oct 29, 2019
@JaZo JaZo closed this as completed in ac2105b Nov 18, 2019
@JaZo
Copy link
Member

JaZo commented Nov 18, 2019

@V-Orbit, to make it less error prone, both TypeMapperInterface and TypeMapper are registered as one singleton in the next release.

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

No branches or pull requests

2 participants