mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 21:32:14 +02:00
Use "egulias/emailvalidator" for email validation
This commit is contained in:
parent
5c6f736937
commit
13c3c92ae6
|
@ -15,14 +15,16 @@
|
|||
"php": ">=5.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"egulias/email-validator": "~1.2",
|
||||
"malkusch/bav": "~1.0",
|
||||
"phpunit/phpunit": "~4.0",
|
||||
"symfony/validator": "~2.6",
|
||||
"malkusch/bav": "~1.0",
|
||||
"zendframework/zend-validator": "~2.3"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-bcmath": "Arbitrary Precision Mathematics",
|
||||
"ext-mbstring": "Multibyte String Functions",
|
||||
"egulias/email-validator": "Strict (RFC compliant) email validation",
|
||||
"malkusch/bav": "German bank account validation",
|
||||
"symfony/validator": "Use Symfony validator through Respect\\Validation",
|
||||
"zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation"
|
||||
|
|
|
@ -1,10 +1,32 @@
|
|||
<?php
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Egulias\EmailValidator\EmailValidator;
|
||||
|
||||
class Email extends AbstractRule
|
||||
{
|
||||
public function __construct(EmailValidator $emailValidator = null)
|
||||
{
|
||||
$this->emailValidator = $emailValidator;
|
||||
}
|
||||
|
||||
public function getEmailValidator()
|
||||
{
|
||||
if (!$this->emailValidator instanceof EmailValidator
|
||||
&& class_exists('Egulias\EmailValidator\EmailValidator')) {
|
||||
$this->emailValidator = new EmailValidator();
|
||||
}
|
||||
|
||||
return $this->emailValidator;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$emailValidator = $this->getEmailValidator();
|
||||
if (null !== $emailValidator) {
|
||||
return $emailValidator->isValid($input);
|
||||
}
|
||||
|
||||
return is_string($input) && filter_var($input, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,85 @@
|
|||
<?php
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
function class_exists($className)
|
||||
{
|
||||
if (isset($GLOBALS['class_exists'][$className])) {
|
||||
return $GLOBALS['class_exists'][$className];
|
||||
}
|
||||
|
||||
return \class_exists($className);
|
||||
}
|
||||
|
||||
class EmailTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private function setEmailValidatorExists($value)
|
||||
{
|
||||
$GLOBALS['class_exists']['Egulias\EmailValidator\EmailValidator'] = (bool) $value;
|
||||
}
|
||||
|
||||
private function resetClassExists()
|
||||
{
|
||||
unset($GLOBALS['class_exists']);
|
||||
}
|
||||
|
||||
private function getEmailValidatorMock()
|
||||
{
|
||||
$emailValidatorMock = $this
|
||||
->getMockBuilder('Egulias\\EmailValidator\\EmailValidator')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
return $emailValidatorMock;
|
||||
}
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->setEmailValidatorExists(false);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->resetClassExists();
|
||||
}
|
||||
|
||||
public function testShouldAcceptInstanceOfEmailValidatorOnConstructor()
|
||||
{
|
||||
$this->resetClassExists();
|
||||
|
||||
$emailValidator = $this->getEmailValidatorMock();
|
||||
|
||||
$rule = new Email($emailValidator);
|
||||
|
||||
$this->assertSame($emailValidator, $rule->getEmailValidator());
|
||||
}
|
||||
|
||||
public function testShouldHaveADefaultInstanceOfEmailValidator()
|
||||
{
|
||||
$this->resetClassExists();
|
||||
|
||||
$rule = new Email();
|
||||
|
||||
$this->assertInstanceOf('Egulias\\EmailValidator\\EmailValidator', $rule->getEmailValidator());
|
||||
}
|
||||
|
||||
public function testShouldUseEmailValidatorWhenDefined()
|
||||
{
|
||||
$this->resetClassExists();
|
||||
|
||||
$input = 'example@example.com';
|
||||
|
||||
$emailValidator = $this->getEmailValidatorMock();
|
||||
$emailValidator
|
||||
->expects($this->once())
|
||||
->method('isValid')
|
||||
->with($input)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$rule = new Email($emailValidator);
|
||||
|
||||
$this->assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidEmail
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue