3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mail\Header
;
12 use Zend\Mail\AddressList
;
13 use Zend\Mail\Headers
;
16 * Base class for headers composing address lists (to, from, cc, bcc, reply-to)
18 abstract class AbstractAddressList
implements HeaderInterface
23 protected $addressList;
26 * @var string Normalized field name
35 protected $encoding = 'ASCII';
38 * @var string lower case field name
40 protected static $type;
42 public static function fromString($headerLine)
44 $decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR
, 'UTF-8');
45 // split into name/value
46 list($fieldName, $fieldValue) = GenericHeader
::splitHeaderLine($decodedLine);
48 if (strtolower($fieldName) !== static::$type) {
49 throw new Exception\
InvalidArgumentException(sprintf(
50 'Invalid header line for "%s" string',
54 $header = new static();
55 if ($decodedLine != $headerLine) {
56 $header->setEncoding('UTF-8');
59 $fieldValue = str_replace(Headers
::FOLDING
, ' ', $fieldValue);
60 $values = explode(',', $fieldValue);
61 array_walk($values, 'trim');
63 $addressList = $header->getAddressList();
64 foreach ($values as $address) {
65 // split values into name/email
66 if (!preg_match('/^((?P<name>.*?)<(?P<namedEmail>[^>]+)>|(?P<email>.+))$/', $address, $matches)) {
67 // Should we raise an exception here?
71 if (isset($matches['name'])) {
72 $name = trim($matches['name']);
78 if (isset($matches['namedEmail'])) {
79 $email = $matches['namedEmail'];
81 if (isset($matches['email'])) {
82 $email = $matches['email'];
84 $email = trim($email); // we may have leading whitespace
86 // populate address list
87 $addressList->add($email, $name);
92 public function getFieldName()
94 return $this->fieldName
;
97 public function getFieldValue($format = HeaderInterface
::FORMAT_RAW
)
100 $encoding = $this->getEncoding();
101 foreach ($this->getAddressList() as $address) {
102 $email = $address->getEmail();
103 $name = $address->getName();
107 if (false !== strstr($name, ',')) {
108 $name = sprintf('"%s"', $name);
111 if ($format == HeaderInterface
::FORMAT_ENCODED
112 && 'ASCII' !== $encoding
114 $name = HeaderWrap
::mimeEncodeValue($name, $encoding);
116 $emails[] = sprintf('%s <%s>', $name, $email);
120 return implode(',' . Headers
::FOLDING
, $emails);
123 public function setEncoding($encoding)
125 $this->encoding
= $encoding;
129 public function getEncoding()
131 return $this->encoding
;
135 * Set address list for this header
137 * @param AddressList $addressList
139 public function setAddressList(AddressList
$addressList)
141 $this->addressList
= $addressList;
145 * Get address list managed by this header
147 * @return AddressList
149 public function getAddressList()
151 if (null === $this->addressList
) {
152 $this->setAddressList(new AddressList());
154 return $this->addressList
;
157 public function toString()
159 $name = $this->getFieldName();
160 $value = $this->getFieldValue(HeaderInterface
::FORMAT_ENCODED
);
161 return (empty($value)) ?
'' : sprintf('%s: %s', $name, $value);