Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mail / Header / AbstractAddressList.php
blob0a34619e58792536b80f43bac7bbb6b9ab981c5c
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-2013 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\Header;
12 use Zend\Mail\AddressList;
13 use Zend\Mail\Headers;
15 /**
16 * Base class for headers composing address lists (to, from, cc, bcc, reply-to)
18 abstract class AbstractAddressList implements HeaderInterface
20 /**
21 * @var AddressList
23 protected $addressList;
25 /**
26 * @var string Normalized field name
28 protected $fieldName;
30 /**
31 * Header encoding
33 * @var string
35 protected $encoding = 'ASCII';
37 /**
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',
51 __CLASS__
52 ));
54 $header = new static();
55 if ($decodedLine != $headerLine) {
56 $header->setEncoding('UTF-8');
58 // split value on ","
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?
68 continue;
70 $name = null;
71 if (isset($matches['name'])) {
72 $name = trim($matches['name']);
74 if (empty($name)) {
75 $name = null;
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);
89 return $header;
92 public function getFieldName()
94 return $this->fieldName;
97 public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
99 $emails = array();
100 $encoding = $this->getEncoding();
101 foreach ($this->getAddressList() as $address) {
102 $email = $address->getEmail();
103 $name = $address->getName();
104 if (empty($name)) {
105 $emails[] = $email;
106 } else {
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;
126 return $this;
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);