55
66namespace greeny \MailLibrary ;
77
8+ use greeny \MailLibrary \Structures \IStructure ;
9+ use Nette \Utils \Strings ;
10+
811class Mail {
912 const ANSWERED = 'ANSWERED ' ;
1013 const BCC = 'BCC ' ;
@@ -39,10 +42,10 @@ class Mail {
3942 const ORDER_CC = SORTCC ;
4043 const ORDER_SIZE = SORTSIZE ;
4144
42- /** @var \greeny\MailLibrary\ Connection */
45+ /** @var Connection */
4346 protected $ connection ;
4447
45- /** @var \greeny\MailLibrary\ Mailbox */
48+ /** @var Mailbox */
4649 protected $ mailbox ;
4750
4851 /** @var int */
@@ -51,7 +54,7 @@ class Mail {
5154 /** @var array */
5255 protected $ headers = NULL ;
5356
54- /** @var \greeny\MailLibrary\Structures\ IStructure */
57+ /** @var IStructure */
5558 protected $ structure = NULL ;
5659
5760 /** @var array */
@@ -78,7 +81,8 @@ public function __construct(Connection $connection, Mailbox $mailbox, $id)
7881 public function __isset ($ name )
7982 {
8083 $ this ->headers !== NULL || $ this ->initializeHeaders ();
81- return isset ($ this ->headers [$ this ->formatHeaderName ($ name )]);
84+ $ key = $ this ->normalizeHeaderName ($ this ->lowerCamelCaseToHeaderName ($ name ));
85+ return isset ($ this ->headers [$ key ]);
8286 }
8387
8488 /**
@@ -89,7 +93,9 @@ public function __isset($name)
8993 */
9094 public function __get ($ name )
9195 {
92- return $ this ->getHeader ($ name );
96+ return $ this ->getHeader (
97+ $ this ->normalizeHeaderName ($ this ->lowerCamelCaseToHeaderName ($ name ))
98+ );
9399 }
94100
95101 /**
@@ -124,7 +130,7 @@ public function getHeaders()
124130 public function getHeader ($ name )
125131 {
126132 $ this ->headers !== NULL || $ this ->initializeHeaders ();
127- $ index = $ this ->formatHeaderName ($ name );
133+ $ index = $ this ->normalizeHeaderName ($ name );
128134 if (isset ($ this ->headers [$ index ])) {
129135 return $ this ->headers [$ index ];
130136 } else {
@@ -235,7 +241,7 @@ protected function initializeHeaders()
235241 $ this ->headers = array ();
236242 $ this ->connection ->getDriver ()->switchMailbox ($ this ->mailbox ->getName ());
237243 foreach ($ this ->connection ->getDriver ()->getHeaders ($ this ->id ) as $ key => $ value ) {
238- $ this ->headers [$ this ->formatHeaderName ($ key )] = $ value ;
244+ $ this ->headers [$ this ->normalizeHeaderName ($ key )] = $ value ;
239245 }
240246 }
241247
@@ -252,15 +258,29 @@ protected function initializeFlags()
252258 }
253259
254260 /**
255- * Formats header name (X-Received-From => xReceivedFrom )
261+ * Formats header name (X-Received-From => x-recieved-from )
256262 *
257- * @param string $name
263+ * @param string $name Header name (with dashes, valid UTF-8 string)
258264 * @return string
259265 */
260- protected function formatHeaderName ($ name )
266+ protected function normalizeHeaderName ($ name )
261267 {
262- return lcfirst (preg_replace_callback ("~-.~ " , function ($ matches ){
268+ return Strings::normalize (Strings::lower ($ name ));
269+ }
270+
271+ /**
272+ * Converts camel cased name to normalized header name (xReceivedFrom => x-recieved-from)
273+ *
274+ * @param string $camelCasedName
275+ * @return string name with dashes
276+ */
277+ protected function lowerCamelCaseToHeaderName ($ camelCasedName ) {
278+ // todo: test this
279+ // todo: use something like this instead http://stackoverflow.com/a/1993772
280+ $ dashedName = lcfirst (preg_replace_callback ("~-.~ " , function ($ matches ){
263281 return ucfirst (substr ($ matches [0 ], 1 ));
264- }, $ name ));
282+ }, $ camelCasedName ));
283+
284+ return $ this ->normalizeHeaderName ($ dashedName );
265285 }
266286}
0 commit comments