1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17: 18:
19: class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet
20: {
21:
22: private $_factory;
23:
24:
25: private $_headers = array();
26:
27:
28: private $_order = array();
29:
30:
31: private $_required = array();
32:
33:
34: private $_charset;
35:
36: 37: 38: 39: 40: 41:
42: public function __construct(Swift_Mime_HeaderFactory $factory, $charset = null)
43: {
44: $this->_factory = $factory;
45: if (isset($charset)) {
46: $this->setCharset($charset);
47: }
48: }
49:
50: 51: 52: 53: 54:
55: public function setCharset($charset)
56: {
57: $this->_charset = $charset;
58: $this->_factory->charsetChanged($charset);
59: $this->_notifyHeadersOfCharset($charset);
60: }
61:
62: 63: 64: 65: 66: 67:
68: public function addMailboxHeader($name, $addresses = null)
69: {
70: $this->_storeHeader($name,
71: $this->_factory->createMailboxHeader($name, $addresses));
72: }
73:
74: 75: 76: 77: 78: 79:
80: public function addDateHeader($name, $timestamp = null)
81: {
82: $this->_storeHeader($name,
83: $this->_factory->createDateHeader($name, $timestamp));
84: }
85:
86: 87: 88: 89: 90: 91:
92: public function addTextHeader($name, $value = null)
93: {
94: $this->_storeHeader($name,
95: $this->_factory->createTextHeader($name, $value));
96: }
97:
98: 99: 100: 101: 102: 103: 104:
105: public function addParameterizedHeader($name, $value = null, $params = array())
106: {
107: $this->_storeHeader($name,
108: $this->_factory->createParameterizedHeader($name, $value,
109: $params));
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function addIdHeader($name, $ids = null)
119: {
120: $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
121: }
122:
123: 124: 125: 126: 127: 128:
129: public function addPathHeader($name, $path = null)
130: {
131: $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
132: }
133:
134: 135: 136: 137: 138: 139: 140: 141: 142: 143:
144: public function has($name, $index = 0)
145: {
146: $lowerName = strtolower($name);
147:
148: return array_key_exists($lowerName, $this->_headers)
149: && array_key_exists($index, $this->_headers[$lowerName]);
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163:
164: public function set(Swift_Mime_Header $header, $index = 0)
165: {
166: $this->_storeHeader($header->getFieldName(), $header, $index);
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
180: public function get($name, $index = 0)
181: {
182: if ($this->has($name, $index)) {
183: $lowerName = strtolower($name);
184:
185: return $this->_headers[$lowerName][$index];
186: }
187: }
188:
189: 190: 191: 192: 193: 194: 195:
196: public function getAll($name = null)
197: {
198: if (!isset($name)) {
199: $headers = array();
200: foreach ($this->_headers as $collection) {
201: $headers = array_merge($headers, $collection);
202: }
203:
204: return $headers;
205: }
206:
207: $lowerName = strtolower($name);
208: if (!array_key_exists($lowerName, $this->_headers)) {
209: return array();
210: }
211:
212: return $this->_headers[$lowerName];
213: }
214:
215: 216: 217: 218: 219: 220: 221: 222:
223: public function remove($name, $index = 0)
224: {
225: $lowerName = strtolower($name);
226: unset($this->_headers[$lowerName][$index]);
227: }
228:
229: 230: 231: 232: 233:
234: public function removeAll($name)
235: {
236: $lowerName = strtolower($name);
237: unset($this->_headers[$lowerName]);
238: }
239:
240: 241: 242: 243: 244:
245: public function newInstance()
246: {
247: return new self($this->_factory);
248: }
249:
250: 251: 252: 253: 254: 255: 256:
257: public function defineOrdering(array $sequence)
258: {
259: $this->_order = array_flip(array_map('strtolower', $sequence));
260: }
261:
262: 263: 264: 265: 266: 267: 268:
269: public function setAlwaysDisplayed(array $names)
270: {
271: $this->_required = array_flip(array_map('strtolower', $names));
272: }
273:
274: 275: 276: 277: 278:
279: public function charsetChanged($charset)
280: {
281: $this->setCharset($charset);
282: }
283:
284: 285: 286: 287: 288:
289: public function toString()
290: {
291: $string = '';
292: $headers = $this->_headers;
293: if ($this->_canSort()) {
294: uksort($headers, array($this, '_sortHeaders'));
295: }
296: foreach ($headers as $collection) {
297: foreach ($collection as $header) {
298: if ($this->_isDisplayed($header) || $header->getFieldBody() != '') {
299: $string .= $header->toString();
300: }
301: }
302: }
303:
304: return $string;
305: }
306:
307: 308: 309: 310: 311: 312: 313:
314: public function __toString()
315: {
316: return $this->toString();
317: }
318:
319:
320:
321:
322: private function _storeHeader($name, Swift_Mime_Header $header, $offset = null)
323: {
324: if (!isset($this->_headers[strtolower($name)])) {
325: $this->_headers[strtolower($name)] = array();
326: }
327: if (!isset($offset)) {
328: $this->_headers[strtolower($name)][] = $header;
329: } else {
330: $this->_headers[strtolower($name)][$offset] = $header;
331: }
332: }
333:
334:
335: private function _canSort()
336: {
337: return count($this->_order) > 0;
338: }
339:
340:
341: private function _sortHeaders($a, $b)
342: {
343: $lowerA = strtolower($a);
344: $lowerB = strtolower($b);
345: $aPos = array_key_exists($lowerA, $this->_order)
346: ? $this->_order[$lowerA]
347: : -1;
348: $bPos = array_key_exists($lowerB, $this->_order)
349: ? $this->_order[$lowerB]
350: : -1;
351:
352: if ($aPos == -1) {
353: return 1;
354: } elseif ($bPos == -1) {
355: return -1;
356: }
357:
358: return ($aPos < $bPos) ? -1 : 1;
359: }
360:
361:
362: private function _isDisplayed(Swift_Mime_Header $header)
363: {
364: return array_key_exists(strtolower($header->getFieldName()), $this->_required);
365: }
366:
367:
368: private function _notifyHeadersOfCharset($charset)
369: {
370: foreach ($this->_headers as $headerGroup) {
371: foreach ($headerGroup as $header) {
372: $header->setCharset($charset);
373: }
374: }
375: }
376: }
377: