KinMeister
KinMeister

Reputation: 3

API-Platform - Iterating Resource Via IRI

In my Symfony app, I have a subscriber wherein I need to iterate over IRIs stored in an array and access their entity's methods. How can I do this?

For instance:

function sendMail ($event) {
    ...
    $instance = $event->getControllerResult();
    ...
    $recipients = $instance->getRecipients(); // returns array of IRIs
    foreach ($recipients as $recipient) {
        $r = // instance of IRI-associated entity
        if ($r instanceof User) {
            // send to user
            $email = $r->getEmail();
            // send an email
        } else if ($r instanceof Group) {
            // send to group
            foreach ($r->getUsers() as $user) {
                $email = $user->getEmail();
                // send an email
            }
        }
    }
    ...
}

Although I have likely overlooked it, I have not found a method of doing so in the documentation and my knowledge of Symfony is still growing.

Upvotes: 0

Views: 1152

Answers (1)

Kate Syrotchuk
Kate Syrotchuk

Reputation: 116

You can try to pass IriConverterInterface $iriConverter to __constructor. And convert your Iri to an entity, like:

private $iriConverter;

public function __construct(IriConverterInterface $iriConverter)
{
    $this->iriConverter = $iriConverter;
}

public function sendMail ($event) {
   foreach ($recipients as $recipientIri) {
        $recipient = $iriConverter->getItemFromIri($recipientIri)
        if ($recipient instanceof User) {
            $email = $recipient->getEmail();
            ...
        } 
    ...
    }
}

Upvotes: 2

Related Questions