Created
May 17, 2014 15:00
-
-
Save evaldobarbosa/c302b2d1c327a0708735 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DomainCheck { | |
private $email; | |
function __construct($email_a_verificar) { | |
$this->email = $email_a_verificar; | |
} | |
private function getDominio($email) { | |
$email = explode('@',$email); | |
return $email[ count( $email ) - 1 ]; | |
} | |
function check($lista_de_emails) { | |
$dominio_a_verificar = $this->getDominio( $this->email ); | |
foreach( $lista_de_emails['email'] as $email ) { | |
$dominio_atual = $this->getDominio( $email ); | |
if ( $dominio_a_verificar == $dominio_atual ) { | |
throw new Exception("Domínio existente na lista", 1); | |
} | |
} | |
} | |
} | |
//a lista simula o seu recordset | |
//usando um fetchAll do PDO | |
$lista_emails_do_banco = array( | |
'email'=>array( | |
'evaldobarbosa@gmail.com', | |
'evaldobarbosa@yahoo.com', | |
'evaldobarbosa@facebook.com', | |
'evaldobarbosa@linkedin.com', | |
'evaldobarbosa@phpmaranhao.com.br' | |
) | |
); | |
$email_recebido_do_form = 'usuario@gmail.com'; | |
$check = new DomainCheck($email_recebido_do_form); | |
try { | |
$check->check( $lista_emails_do_banco ); | |
echo "Sem duplicações de e-mail"; | |
//Rotina de salvamento do registro pode vir aqui | |
} catch( Exception $e ) { | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment