Skip to content

Commit

Permalink
Cleanup - The aftermath
Browse files Browse the repository at this point in the history
Cleaning up the mess I made (and some other stuff). Also made sure
that unsetting the email pattern, respectively validating using
`filter_var()` works properly.
  • Loading branch information
ndm2 committed Nov 9, 2014
1 parent 72770e5 commit cf426dc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/Network/Email/Email.php
Expand Up @@ -579,11 +579,13 @@ public function headerCharset($charset = null) {
/**
* EmailPattern setter/getter
*
* @param string $regex for email address validation
* @param string|bool|null $regex The pattern to use for email address validation,
* null to unset the pattern and make use of filter_var() instead, false or
* nothing to return the current value
* @return string|$this
*/
public function emailPattern($regex = null) {
if ($regex === null) {
public function emailPattern($regex = false) {
if ($regex === false) {
return $this->_emailPattern;
}
$this->_emailPattern = $regex;
Expand Down Expand Up @@ -629,10 +631,11 @@ protected function _setEmail($varName, $email, $name) {
* @throws \InvalidArgumentException If email address does not validate
*/
protected function _validateEmail($email) {
if ($this->_emailPattern === null && filter_var($email, FILTER_VALIDATE_EMAIL)) {
return;
}
if (preg_match($this->_emailPattern, $email)) {
if ($this->_emailPattern === null) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return;
}
} elseif (preg_match($this->_emailPattern, $email)) {
return;
}
throw new InvalidArgumentException(sprintf('Invalid email: "%s"', $email));
Expand Down
3 changes: 2 additions & 1 deletion src/Network/Session/CacheSession.php
Expand Up @@ -104,10 +104,11 @@ public function destroy($id) {
* Helper function called on gc for cache sessions.
*
* @param string $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
* @return void
* @return bool True (irrespective of whether or not the garbage is being successfully collected)
*/
public function gc($maxlifetime) {
Cache::gc($this->_options['config'], time() - $maxlifetime);
return true;
}

}
2 changes: 1 addition & 1 deletion src/View/JsonView.php
Expand Up @@ -138,7 +138,7 @@ public function render($view = null, $layout = null) {
/**
* Serialize view vars
*
* @param array|string $serialize The viewVars that need to be serialized
* @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized
* @return string The serialized data
*/
protected function _serialize($serialize) {
Expand Down
2 changes: 1 addition & 1 deletion src/View/XmlView.php
Expand Up @@ -122,7 +122,7 @@ public function render($view = null, $layout = null) {
/**
* Serialize view vars.
*
* @param array|string $serialize The viewVars that need to be serialized.
* @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized
* @return string The serialized data
*/
protected function _serialize($serialize) {
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Network/Email/EmailTest.php
Expand Up @@ -333,6 +333,25 @@ public function testCustomEmailValidation() {
), $this->CakeEmail->to());
}

/**
* Tests that it is possible to unset the email pattern and make use of filter_var() instead.
*
* @return void
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid email: "fail.@example.com"
*/
public function testUnsetEmailPattern() {
$email = new Email();
$this->assertSame(Email::EMAIL_PATTERN, $email->emailPattern());

$email->emailPattern(null);
$this->assertNull($email->emailPattern());

$email->to('pass@example.com');
$email->to('fail.@example.com');
}

/**
* testFormatAddress method
*
Expand Down

0 comments on commit cf426dc

Please sign in to comment.