*/ class CaptchaValidator implements FormValidatorInterface { /** * Session */ private $session; /** * Session key to store the code */ private $key; /** * Error message text for non-matching submissions */ private $invalidMessage; public function __construct(Session $session, $key, $invalidMessage) { $this->session = $session; $this->key = $key; $this->invalidMessage = $invalidMessage; } public function validate(FormInterface $form) { $code = $form->getData(); $excepted_code = $this->getExceptedCode(); if (!($code && $excepted_code && is_string($code) && is_string($excepted_code) && $this->niceize($code) == $this->niceize($excepted_code))) { $form->addError(new FormError($this->invalidMessage)); } $this->session->remove($this->key); if ($this->session->has($this->key.'_fingerprint')) { $this->session->remove($this->key.'_fingerprint'); } } /** * Retrieve the excepted CAPTCHA code */ private function getExceptedCode() { if ($this->session->has($this->key)) { return $this->session->get($this->key); } return null; } /** * Process the codes */ private function niceize($code) { return strtr(strtolower($code), 'oil', '01l'); } }