/framework/vendor/swift/lib/classes/Swift/Mime/SimpleHeaderSet.php

http://zoop.googlecode.com/ · PHP · 396 lines · 193 code · 38 blank · 165 comment · 14 complexity · 2e5c664af8c7faefdff5ef604dc62ca7 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. //@require 'Swift/Mime/HeaderSet.php';
  10. //@require 'Swift/Mime/HeaderFactory.php';
  11. /**
  12. * A collection of MIME headers.
  13. *
  14. * @package Swift
  15. * @subpackage Mime
  16. *
  17. * @author Chris Corbyn
  18. */
  19. class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet
  20. {
  21. /** HeaderFactory */
  22. private $_factory;
  23. /** Collection of set Headers */
  24. private $_headers = array();
  25. /** Field ordering details */
  26. private $_order = array();
  27. /** List of fields which are required to be displayed */
  28. private $_required = array();
  29. /** The charset used by Headers */
  30. private $_charset;
  31. /**
  32. * Create a new SimpleHeaderSet with the given $factory.
  33. *
  34. * @param Swift_Mime_HeaderFactory $factory
  35. * @param string $charset
  36. */
  37. public function __construct(Swift_Mime_HeaderFactory $factory,
  38. $charset = null)
  39. {
  40. $this->_factory = $factory;
  41. if (isset($charset))
  42. {
  43. $this->setCharset($charset);
  44. }
  45. }
  46. /**
  47. * Set the charset used by these headers.
  48. *
  49. * @param string $charset
  50. */
  51. public function setCharset($charset)
  52. {
  53. $this->_charset = $charset;
  54. $this->_factory->charsetChanged($charset);
  55. $this->_notifyHeadersOfCharset($charset);
  56. }
  57. /**
  58. * Add a new Mailbox Header with a list of $addresses.
  59. *
  60. * @param string $name
  61. * @param array|string $addresses
  62. */
  63. public function addMailboxHeader($name, $addresses = null)
  64. {
  65. $this->_storeHeader($name,
  66. $this->_factory->createMailboxHeader($name, $addresses));
  67. }
  68. /**
  69. * Add a new Date header using $timestamp (UNIX time).
  70. *
  71. * @param string $name
  72. * @param int $timestamp
  73. */
  74. public function addDateHeader($name, $timestamp = null)
  75. {
  76. $this->_storeHeader($name,
  77. $this->_factory->createDateHeader($name, $timestamp));
  78. }
  79. /**
  80. * Add a new basic text header with $name and $value.
  81. *
  82. * @param string $name
  83. * @param string $value
  84. */
  85. public function addTextHeader($name, $value = null)
  86. {
  87. $this->_storeHeader($name,
  88. $this->_factory->createTextHeader($name, $value));
  89. }
  90. /**
  91. * Add a new ParameterizedHeader with $name, $value and $params.
  92. *
  93. * @param string $name
  94. * @param string $value
  95. * @param array $params
  96. */
  97. public function addParameterizedHeader($name, $value = null,
  98. $params = array())
  99. {
  100. $this->_storeHeader($name,
  101. $this->_factory->createParameterizedHeader($name, $value,
  102. $params));
  103. }
  104. /**
  105. * Add a new ID header for Message-ID or Content-ID.
  106. *
  107. * @param string $name
  108. * @param string|array $ids
  109. */
  110. public function addIdHeader($name, $ids = null)
  111. {
  112. $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
  113. }
  114. /**
  115. * Add a new Path header with an address (path) in it.
  116. *
  117. * @param string $name
  118. * @param string $path
  119. */
  120. public function addPathHeader($name, $path = null)
  121. {
  122. $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
  123. }
  124. /**
  125. * Returns true if at least one header with the given $name exists.
  126. *
  127. * If multiple headers match, the actual one may be specified by $index.
  128. *
  129. * @param string $name
  130. * @param int $index
  131. *
  132. * @return boolean
  133. */
  134. public function has($name, $index = 0)
  135. {
  136. $lowerName = strtolower($name);
  137. return array_key_exists($lowerName, $this->_headers)
  138. && array_key_exists($index, $this->_headers[$lowerName]);
  139. }
  140. /**
  141. * Set a header in the HeaderSet.
  142. *
  143. * The header may be a previously fetched header via {@link get()} or it may
  144. * be one that has been created separately.
  145. *
  146. * If $index is specified, the header will be inserted into the set at this
  147. * offset.
  148. *
  149. * @param Swift_Mime_Header $header
  150. * @param int $index
  151. */
  152. public function set(Swift_Mime_Header $header, $index = 0)
  153. {
  154. $this->_storeHeader($header->getFieldName(), $header, $index);
  155. }
  156. /**
  157. * Get the header with the given $name.
  158. *
  159. * If multiple headers match, the actual one may be specified by $index.
  160. * Returns NULL if none present.
  161. *
  162. * @param string $name
  163. * @param int $index
  164. *
  165. * @return Swift_Mime_Header
  166. */
  167. public function get($name, $index = 0)
  168. {
  169. if ($this->has($name, $index))
  170. {
  171. $lowerName = strtolower($name);
  172. return $this->_headers[$lowerName][$index];
  173. }
  174. }
  175. /**
  176. * Get all headers with the given $name.
  177. *
  178. * @param string $name
  179. *
  180. * @return array
  181. */
  182. public function getAll($name = null)
  183. {
  184. if (!isset($name))
  185. {
  186. $headers = array();
  187. foreach ($this->_headers as $collection)
  188. {
  189. $headers = array_merge($headers, $collection);
  190. }
  191. return $headers;
  192. }
  193. $lowerName = strtolower($name);
  194. if (!array_key_exists($lowerName, $this->_headers))
  195. {
  196. return array();
  197. }
  198. return $this->_headers[$lowerName];
  199. }
  200. /**
  201. * Remove the header with the given $name if it's set.
  202. *
  203. * If multiple headers match, the actual one may be specified by $index.
  204. *
  205. * @param string $name
  206. * @param int $index
  207. */
  208. public function remove($name, $index = 0)
  209. {
  210. $lowerName = strtolower($name);
  211. unset($this->_headers[$lowerName][$index]);
  212. }
  213. /**
  214. * Remove all headers with the given $name.
  215. *
  216. * @param string $name
  217. */
  218. public function removeAll($name)
  219. {
  220. $lowerName = strtolower($name);
  221. unset($this->_headers[$lowerName]);
  222. }
  223. /**
  224. * Create a new instance of this HeaderSet.
  225. *
  226. * @return Swift_Mime_HeaderSet
  227. */
  228. public function newInstance()
  229. {
  230. return new self($this->_factory);
  231. }
  232. /**
  233. * Define a list of Header names as an array in the correct order.
  234. *
  235. * These Headers will be output in the given order where present.
  236. *
  237. * @param array $sequence
  238. */
  239. public function defineOrdering(array $sequence)
  240. {
  241. $this->_order = array_flip(array_map('strtolower', $sequence));
  242. }
  243. /**
  244. * Set a list of header names which must always be displayed when set.
  245. *
  246. * Usually headers without a field value won't be output unless set here.
  247. *
  248. * @param array $names
  249. */
  250. public function setAlwaysDisplayed(array $names)
  251. {
  252. $this->_required = array_flip(array_map('strtolower', $names));
  253. }
  254. /**
  255. * Notify this observer that the entity's charset has changed.
  256. *
  257. * @param string $charset
  258. */
  259. public function charsetChanged($charset)
  260. {
  261. $this->setCharset($charset);
  262. }
  263. /**
  264. * Returns a string with a representation of all headers.
  265. *
  266. * @return string
  267. */
  268. public function toString()
  269. {
  270. $string = '';
  271. $headers = $this->_headers;
  272. if ($this->_canSort())
  273. {
  274. uksort($headers, array($this, '_sortHeaders'));
  275. }
  276. foreach ($headers as $collection)
  277. {
  278. foreach ($collection as $header)
  279. {
  280. if ($this->_isDisplayed($header) || $header->getFieldBody() != '')
  281. {
  282. $string .= $header->toString();
  283. }
  284. }
  285. }
  286. return $string;
  287. }
  288. /**
  289. * Returns a string representation of this object.
  290. *
  291. * @return string
  292. *
  293. * @see toString()
  294. */
  295. public function __toString()
  296. {
  297. return $this->toString();
  298. }
  299. // -- Private methods
  300. /** Save a Header to the internal collection */
  301. private function _storeHeader($name, Swift_Mime_Header $header, $offset = null)
  302. {
  303. if (!isset($this->_headers[strtolower($name)]))
  304. {
  305. $this->_headers[strtolower($name)] = array();
  306. }
  307. if (!isset($offset))
  308. {
  309. $this->_headers[strtolower($name)][] = $header;
  310. }
  311. else
  312. {
  313. $this->_headers[strtolower($name)][$offset] = $header;
  314. }
  315. }
  316. /** Test if the headers can be sorted */
  317. private function _canSort()
  318. {
  319. return count($this->_order) > 0;
  320. }
  321. /** uksort() algorithm for Header ordering */
  322. private function _sortHeaders($a, $b)
  323. {
  324. $lowerA = strtolower($a);
  325. $lowerB = strtolower($b);
  326. $aPos = array_key_exists($lowerA, $this->_order)
  327. ? $this->_order[$lowerA]
  328. : -1;
  329. $bPos = array_key_exists($lowerB, $this->_order)
  330. ? $this->_order[$lowerB]
  331. : -1;
  332. if ($aPos == -1)
  333. {
  334. return 1;
  335. }
  336. elseif ($bPos == -1)
  337. {
  338. return -1;
  339. }
  340. return ($aPos < $bPos) ? -1 : 1;
  341. }
  342. /** Test if the given Header is always displayed */
  343. private function _isDisplayed(Swift_Mime_Header $header)
  344. {
  345. return array_key_exists(strtolower($header->getFieldName()), $this->_required);
  346. }
  347. /** Notify all Headers of the new charset */
  348. private function _notifyHeadersOfCharset($charset)
  349. {
  350. foreach ($this->_headers as $headerGroup)
  351. {
  352. foreach ($headerGroup as $header)
  353. {
  354. $header->setCharset($charset);
  355. }
  356. }
  357. }
  358. }