Skip to content

Commit

Permalink
Add emailRegex property to CakeEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Jun 28, 2013
1 parent 6fdbdf5 commit 202b753
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/Cake/Network/Email/CakeEmail.php
Expand Up @@ -318,6 +318,14 @@ class CakeEmail {
'ISO-2022-JP-MS' => 'ISO-2022-JP'
);

/**
* Regex for email validation
* If null, it will use built in regex
*
* @var string
*/
protected $_emailRegex = null;

/**
* Constructor
*
Expand Down Expand Up @@ -521,6 +529,20 @@ public function headerCharset($charset = null) {
return $this->headerCharset = $charset;
}

/**
* EmailRegex setter/getter
*
* @param string $regexp
* @return string|CakeEmail
*/
public function emailRegex($regex = false) {
if ($regex === false) {
return $this->_emailRegex;
}
$this->_emailRegex = $regex;
return $this;
}

/**
* Set email
*
Expand Down Expand Up @@ -1161,7 +1183,7 @@ protected function _applyConfig($config) {
$simpleMethods = array(
'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
'transport', 'emailFormat', 'theme', 'helpers'
'transport', 'emailFormat', 'theme', 'helpers', 'emailRegex'
);
foreach ($simpleMethods as $method) {
if (isset($config[$method])) {
Expand Down Expand Up @@ -1218,6 +1240,7 @@ public function reset() {
$this->headerCharset = null;
$this->_attachments = array();
$this->_config = array();
$this->_emailRegex = null;
return $this;
}

Expand Down
24 changes: 24 additions & 0 deletions lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
Expand Up @@ -264,6 +264,28 @@ public function testInvalidEmailAdd($value) {
$this->CakeEmail->addTo($value);
}

/**
* test emailRegex method
*
* @return void
*/
public function testEmailRegex() {
$regex = '/.+@.+\..+/i';
$this->assertNull($this->CakeEmail->emailRegex());
$this->assertSame($regex, $this->CakeEmail->emailRegex($regex)->emailRegex());
}

/**
* Tests that it is possible to set email regex configuration to a CakeEmail object
*
* @return void
*/
public function testConfigEmailRegex() {
$regex = '/.+@.+\..+/i';
$email = new CakeEmail(array('emailRegex' => $regex));
$this->assertSame($regex, $email->emailRegex());
}

/**
* testFormatAddress method
*
Expand Down Expand Up @@ -1427,11 +1449,13 @@ public function testMessage() {
public function testReset() {
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->theme('TestTheme');
$this->CakeEmail->emailRegex('/.+@.+\..+/i');
$this->assertSame($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org'));

$this->CakeEmail->reset();
$this->assertSame($this->CakeEmail->to(), array());
$this->assertSame(null, $this->CakeEmail->theme());
$this->assertSame(null, $this->CakeEmail->emailRegex());
}

/**
Expand Down

0 comments on commit 202b753

Please sign in to comment.