Skip to content

Commit 37c93ab

Browse files
committed
Merge branch 'removed-magic-accessing-of-headers'
2 parents 4b4cf5c + 0dc7a90 commit 37c93ab

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

MailLibrary/Mail.php

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace greeny\MailLibrary;
77

8+
use greeny\MailLibrary\Structures\IStructure;
9+
use Nette\Utils\Strings;
10+
811
class 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,18 +81,27 @@ 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
/**
8589
* Header getter
8690
*
8791
* @param string $name
8892
* @return mixed
93+
* @deprecated
8994
*/
9095
public function __get($name)
9196
{
92-
return $this->getHeader($name);
97+
\trigger_error(\E_USER_DEPRECATED, 'use array access with execat header name instead');
98+
return $this->getHeader(
99+
$this->normalizeHeaderName($this->lowerCamelCaseToHeaderName($name))
100+
);
101+
}
102+
103+
public function __set($name, $value) {
104+
throw new \Exception('Mail headers are read-only.');
93105
}
94106

95107
/**
@@ -124,12 +136,12 @@ public function getHeaders()
124136
public function getHeader($name)
125137
{
126138
$this->headers !== NULL || $this->initializeHeaders();
127-
$index = $this->formatHeaderName($name);
139+
$index = $this->normalizeHeaderName($name);
128140
if(isset($this->headers[$index])) {
129141
return $this->headers[$index];
130-
} else {
131-
return NULL;
132142
}
143+
144+
return NULL;
133145
}
134146

135147
/**
@@ -140,9 +152,9 @@ public function getSender() {
140152
if($from) {
141153
$contacts = $from->getContactsObjects();
142154
return (count($contacts) ? $contacts[0] : NULL);
143-
} else {
144-
return NULL;
145155
}
156+
157+
return NULL;
146158
}
147159

148160
/**
@@ -235,7 +247,7 @@ protected function initializeHeaders()
235247
$this->headers = array();
236248
$this->connection->getDriver()->switchMailbox($this->mailbox->getName());
237249
foreach($this->connection->getDriver()->getHeaders($this->id) as $key => $value) {
238-
$this->headers[$this->formatHeaderName($key)] = $value;
250+
$this->headers[$this->normalizeHeaderName($key)] = $value;
239251
}
240252
}
241253

@@ -252,15 +264,29 @@ protected function initializeFlags()
252264
}
253265

254266
/**
255-
* Formats header name (X-Received-From => xReceivedFrom)
267+
* Formats header name (X-Received-From => x-recieved-from)
256268
*
257-
* @param string $name
269+
* @param string $name Header name (with dashes, valid UTF-8 string)
258270
* @return string
259271
*/
260-
protected function formatHeaderName($name)
272+
protected function normalizeHeaderName($name)
261273
{
262-
return lcfirst(preg_replace_callback("~-.~", function($matches){
274+
return Strings::normalize(Strings::lower($name));
275+
}
276+
277+
/**
278+
* Converts camel cased name to normalized header name (xReceivedFrom => x-recieved-from)
279+
*
280+
* @param string $camelCasedName
281+
* @return string name with dashes
282+
*/
283+
protected function lowerCamelCaseToHeaderName($camelCasedName) {
284+
// todo: test this
285+
// todo: use something like this instead http://stackoverflow.com/a/1993772
286+
$dashedName = lcfirst(preg_replace_callback("~-.~", function($matches){
263287
return ucfirst(substr($matches[0], 1));
264-
}, $name));
288+
}, $camelCasedName));
289+
290+
return $this->normalizeHeaderName($dashedName);
265291
}
266292
}

0 commit comments

Comments
 (0)