/** * Transforms a list of usernames into an array of UserInterface instances. * * @param string $value Usernames list * * @return UserInterface[] the corresponding UserInterface instances * * @throws UnexpectedTypeException if the given value is not a string */ public function reverseTransform($value) { if (null === $value || '' === $value) { return null; } if (!is_string($value)) { throw new UnexpectedTypeException($value, 'string'); } $usernames = explode(',', $value); $users = []; foreach ($usernames as $username) { $user = $this->userToUsernameTransformer->reverseTransform(trim($username)); if ($user) { $users[] = $user; } } return $users; }
/** * Transforms a string (usernames) to a Collection of UserInterface * * @param string $usernames * * @throws UnexpectedTypeException * @throws TransformationFailedException * @return Collection $recipients */ public function reverseTransform($usernames) { if (null === $usernames || '' === $usernames) { return null; } if (!is_string($usernames)) { throw new UnexpectedTypeException($usernames, 'string'); } $recipients = new ArrayCollection(); $transformer = $this->userToUsernameTransformer; $recipientsNames = array_filter(explode(',', $usernames)); foreach ($recipientsNames as $username) { $user = $this->userToUsernameTransformer->reverseTransform(trim($username)); if (!$user instanceof UserInterface) { throw new TransformationFailedException(sprintf('User "%s" does not exists', $username)); } $recipients->add($user); } return $recipients; }