How to use Doctrine's ArrayCollection::exists method

Of course, in PHP a Closure is a simple Anonymous functions. You could rewrite your code as follow:

$exists =  $this->emails->exists(function($key, $element) use ($email){
    return $email->getEmail() === $element->getEmail()->getEmail();
});

Hope this help


Thank you @Matteo!

Just for completeness, this is the code with which I came up:

public function addEmail(Email $email)
{
    $predictate = function($key, $element) use ($email) {
        /** @var  Email $element If the two email compared as strings are equals, return true. */
        return $element->getEmail()->getEmail() === $email->getEmail();
    };

    // Create a new Email object and add it to the collection
    if (false === $this->emails->exists($predictate)) {
        $this->emails->add($email);
    }

    // Anyway set the email for this store
    $email->setForStore($this);

    return $this;
}