Add emailRegex property to CakeEmail

This commit is contained in:
nojimage 2013-06-29 02:44:55 +09:00
parent 6fdbdf508f
commit 202b753c63
2 changed files with 48 additions and 1 deletions

View file

@ -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
*
@ -521,6 +529,20 @@ class CakeEmail {
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
*
@ -1161,7 +1183,7 @@ class CakeEmail {
$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])) {
@ -1218,6 +1240,7 @@ class CakeEmail {
$this->headerCharset = null;
$this->_attachments = array();
$this->_config = array();
$this->_emailRegex = null;
return $this;
}

View file

@ -264,6 +264,28 @@ class CakeEmailTest extends CakeTestCase {
$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
*
@ -1427,11 +1449,13 @@ class CakeEmailTest extends CakeTestCase {
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());
}
/**