fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Mail / AddressList.php
blob60a34d618d50b4b9d5724b43c7caea26f3d92ee8
1 <?php
2 /**
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
8 */
10 namespace Zend\Mail;
12 use Countable;
13 use Iterator;
15 class AddressList implements Countable, Iterator
17 /**
18 * List of Address objects we're managing
20 * @var array
22 protected $addresses = array();
24 /**
25 * Add an address to the list
27 * @param string|Address\AddressInterface $emailOrAddress
28 * @param null|string $name
29 * @throws Exception\InvalidArgumentException
30 * @return AddressList
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"',
39 __METHOD__,
40 __NAMESPACE__,
41 (is_object($emailOrAddress) ? get_class($emailOrAddress) : gettype($emailOrAddress))
42 ));
45 $email = strtolower($emailOrAddress->getEmail());
46 if ($this->has($email)) {
47 return $this;
50 $this->addresses[$email] = $emailOrAddress;
51 return $this;
54 /**
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
63 * @return AddressList
65 public function addMany(array $addresses)
67 foreach ($addresses as $key => $value) {
68 if (is_int($key) || is_numeric($key)) {
69 $this->add($value);
70 } elseif (is_string($key)) {
71 $this->add($key, $value);
72 } else {
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))
76 ));
79 return $this;
82 /**
83 * Add an address to the list from any valid string format, such as
84 * - "ZF Dev" <dev@zf.com>
85 * - dev@zf.com
87 * @param string $address
88 * @throws Exception\InvalidArgumentException
89 * @return AddressList
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');
97 $name = null;
98 if (isset($matches['name'])) {
99 $name = trim($matches['name']);
101 if (empty($name)) {
102 $name = null;
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);
127 return $this;
131 * Does the email exist in this list?
133 * @param string $email
134 * @return bool
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])) {
152 return false;
155 return $this->addresses[$email];
159 * Delete an address from the list
161 * @param string $email
162 * @return bool
164 public function delete($email)
166 $email = strtolower($email);
167 if (!isset($this->addresses[$email])) {
168 return false;
171 unset($this->addresses[$email]);
172 return true;
176 * Return count of addresses
178 * @return int
180 public function count()
182 return count($this->addresses);
186 * Rewind iterator
188 * @return mixed the value of the first addresses element, or false if the addresses is
189 * empty.
190 * @see addresses
192 public function rewind()
194 return reset($this->addresses);
198 * Return current item in iteration
200 * @return Address
202 public function current()
204 return current($this->addresses);
208 * Return key of current item of iteration
210 * @return string
212 public function key()
214 return key($this->addresses);
218 * Move to next item
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.
222 * @see addresses
224 public function next()
226 return next($this->addresses);
230 * Is the current item of iteration valid?
232 * @return bool
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
245 * @return Address
247 protected function createAddress($email, $name)
249 return new Address($email, $name);