|
<?php |
|
|
|
namespace SimpleValidator\Validators; |
|
|
|
class Email extends Base |
|
{ |
|
public function execute(array $data) |
|
{ |
|
if ($this->isFieldNotEmpty($data)) { |
|
|
|
|
|
|
|
|
|
$value = $data[$this->field]; |
|
$length = strlen($value); |
|
|
|
|
|
|
|
if ($value[0] === '@' || $value[$length - 1] === '.') { |
|
|
|
return false; |
|
} |
|
|
|
|
|
for ($i = 0; $i < $length && $value[$i] !== '@'; ++$i) { |
|
|
|
$c = $value[$i]; |
|
|
|
if (! (ctype_alnum($c) || $c === '.' || $c === '!' || $c === '#' || $c === '$' || |
|
$c === '%' || $c === '&' || $c === '\'' || $c === '*' || $c === '+' || |
|
$c === '-' || $c === '/' || $c === '=' || $c === '?' || $c === '^' || |
|
$c === '_' || $c === '`' || $c === '{' || $c === '|' || $c === '}' || |
|
$c === '~')) { |
|
|
|
return false; |
|
} |
|
} |
|
|
|
|
|
|
|
if (++$i >= $length) return false; |
|
if (($i + 1) === $length) return false; |
|
|
|
|
|
if ($value[$i] === '.') return false; |
|
|
|
|
|
for (; $i < $length; ++$i) { |
|
|
|
$c = $value[$i]; |
|
|
|
if ($c === '.') { |
|
|
|
|
|
if ($value[$i - 1] === '.') return false; |
|
} |
|
elseif (! (ctype_alnum($c) || $c === '-')) { |
|
|
|
|
|
return false; |
|
} |
|
} |
|
} |
|
|
|
return true; |
|
} |
|
} |
|
|