3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
15 class AddressList
implements Countable
, Iterator
18 * List of Address objects we're managing
22 protected $addresses = array();
25 * Add an address to the list
27 * @param string|Address\AddressInterface $emailOrAddress
28 * @param null|string $name
29 * @throws Exception\InvalidArgumentException
32 public function add($emailOrAddress, $name = null)
34 if (is_string($emailOrAddress)) {
35 $emailOrAddress = $this->createAddress($emailOrAddress, $name);
36 } elseif (!$emailOrAddress instanceof Address\AddressInterface
) {
37 throw new Exception\
InvalidArgumentException(sprintf(
38 '%s expects an email address or %s\Address object as its first argument; received "%s"',
41 (is_object($emailOrAddress) ?
get_class($emailOrAddress) : gettype($emailOrAddress))
45 $email = strtolower($emailOrAddress->getEmail());
46 if ($this->has($email)) {
50 $this->addresses
[$email] = $emailOrAddress;
55 * Add many addresses at once
57 * If an email key is provided, it will be used as the email, and the value
58 * as the name. Otherwise, the value is passed as the sole argument to add(),
59 * and, as such, can be either email strings or Address\AddressInterface objects.
61 * @param array $addresses
62 * @throws Exception\RuntimeException
65 public function addMany(array $addresses)
67 foreach ($addresses as $key => $value) {
68 if (is_int($key) ||
is_numeric($key)) {
70 } elseif (is_string($key)) {
71 $this->add($key, $value);
73 throw new Exception\
RuntimeException(sprintf(
74 'Invalid key type in provided addresses array ("%s")',
75 (is_object($key) ?
get_class($key) : var_export($key, 1))
83 * Add an address to the list from any valid string format, such as
84 * - "ZF Dev" <dev@zf.com>
87 * @param string $address
88 * @throws Exception\InvalidArgumentException
91 public function addFromString($address)
93 if (!preg_match('/^((?P<name>.*?)<(?P<namedEmail>[^>]+)>|(?P<email>.+))$/', $address, $matches)) {
94 throw new Exception\
InvalidArgumentException('Invalid address format');
98 if (isset($matches['name'])) {
99 $name = trim($matches['name']);
105 if (isset($matches['namedEmail'])) {
106 $email = $matches['namedEmail'];
108 if (isset($matches['email'])) {
109 $email = $matches['email'];
111 $email = trim($email);
113 return $this->add($email, $name);
117 * Merge another address list into this one
119 * @param AddressList $addressList
120 * @return AddressList
122 public function merge(AddressList
$addressList)
124 foreach ($addressList as $address) {
125 $this->add($address);
131 * Does the email exist in this list?
133 * @param string $email
136 public function has($email)
138 $email = strtolower($email);
139 return isset($this->addresses
[$email]);
143 * Get an address by email
145 * @param string $email
146 * @return bool|Address\AddressInterface
148 public function get($email)
150 $email = strtolower($email);
151 if (!isset($this->addresses
[$email])) {
155 return $this->addresses
[$email];
159 * Delete an address from the list
161 * @param string $email
164 public function delete($email)
166 $email = strtolower($email);
167 if (!isset($this->addresses
[$email])) {
171 unset($this->addresses
[$email]);
176 * Return count of addresses
180 public function count()
182 return count($this->addresses
);
188 * @return mixed the value of the first addresses element, or false if the addresses is
192 public function rewind()
194 return reset($this->addresses
);
198 * Return current item in iteration
202 public function current()
204 return current($this->addresses
);
208 * Return key of current item of iteration
212 public function key()
214 return key($this->addresses
);
220 * @return mixed the addresses value in the next place that's pointed to by the
221 * internal array pointer, or false if there are no more elements.
224 public function next()
226 return next($this->addresses
);
230 * Is the current item of iteration valid?
234 public function valid()
236 $key = key($this->addresses
);
237 return ($key !== null && $key !== false);
241 * Create an address object
243 * @param string $email
244 * @param string|null $name
247 protected function createAddress($email, $name)
249 return new Address($email, $name);