Make sure that "Email" only validates strings

There shouldn't be possible to consider a non-string value as a valid
email anyways, but the real problem is that the "RFCValidation" from
"egulias/email-validator" casts the input as a string which makes PHP
trigger an error.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Konstantin 2018-10-04 10:02:54 +03:00 committed by Henrique Moody
parent 5a067faec7
commit c9850f4ec7
No known key found for this signature in database
GPG key ID: 221E9281655813A6
3 changed files with 12 additions and 2 deletions

View file

@ -16,7 +16,7 @@
"symfony/polyfill-mbstring": "^1.2"
},
"require-dev": {
"egulias/email-validator": "~1.2",
"egulias/email-validator": "~1.2 || ~2.1",
"mikey179/vfsStream": "^1.5",
"phpunit/phpunit": "~4.0",
"symfony/validator": "~2.6.9",

View file

@ -33,9 +33,13 @@ class Email extends AbstractRule
public function validate($input)
{
if (!is_string($input)) {
return false;
}
$emailValidator = $this->getEmailValidator();
if (!$emailValidator instanceof EmailValidator) {
return is_string($input) && filter_var($input, FILTER_VALIDATE_EMAIL);
return (bool) filter_var($input, FILTER_VALIDATE_EMAIL);
}
if (!class_exists('Egulias\\EmailValidator\\Validation\\RFCValidation')) {

View file

@ -11,6 +11,8 @@
namespace Respect\Validation\Rules;
use stdClass;
function class_exists($className)
{
if (isset($GLOBALS['class_exists'][$className])) {
@ -142,6 +144,10 @@ class EmailTest extends \PHPUnit_Framework_TestCase
['test@test..com'],
['test@test.com.'],
['.test@test.com'],
[[]],
[new stdClass()],
[null],
[tmpfile()],
];
}
}