2011-08-25 22:50:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gregwar\CaptchaBundle\Type;
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2011-08-25 22:50:59 +02:00
|
|
|
use Symfony\Component\Form\FormView;
|
|
|
|
use Symfony\Component\Form\FormInterface;
|
|
|
|
use Symfony\Component\Form\AbstractType;
|
2012-06-19 21:14:52 +02:00
|
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
2012-11-14 04:33:36 +01:00
|
|
|
use Symfony\Component\Form\FormEvents;
|
2011-08-25 22:50:59 +02:00
|
|
|
|
|
|
|
use Gregwar\CaptchaBundle\Validator\CaptchaValidator;
|
2011-08-25 23:10:24 +02:00
|
|
|
use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
|
2011-08-25 22:50:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Captcha type
|
|
|
|
*
|
|
|
|
* @author Gregwar <g.passault@gmail.com>
|
|
|
|
*/
|
|
|
|
class CaptchaType extends AbstractType
|
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
|
|
|
|
*/
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator
|
|
|
|
*/
|
|
|
|
protected $generator;
|
|
|
|
|
2011-09-08 17:07:04 +02:00
|
|
|
/**
|
2012-02-08 19:53:22 +01:00
|
|
|
* Options
|
|
|
|
* @var array
|
2011-09-08 17:07:04 +02:00
|
|
|
*/
|
2012-02-08 19:53:22 +01:00
|
|
|
private $options = array();
|
2012-06-29 16:23:05 +02:00
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
|
|
|
|
* @param \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator
|
|
|
|
* @param array $options
|
|
|
|
*/
|
|
|
|
public function __construct(SessionInterface $session, CaptchaGenerator $generator, $options)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
$this->session = $session;
|
|
|
|
$this->generator = $generator;
|
|
|
|
$this->options = $options;
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @param \Symfony\Component\Form\FormBuilderInterface $builder
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2012-06-19 21:14:52 +02:00
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2012-12-04 00:54:39 +01:00
|
|
|
$validator = new CaptchaValidator(
|
|
|
|
$this->session,
|
|
|
|
$builder->getForm()->getName(),
|
|
|
|
$options['invalid_message'],
|
|
|
|
$options['bypass_code']
|
|
|
|
);
|
2011-12-14 10:28:21 +01:00
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
$builder->addEventListener(FormEvents::POST_BIND, array($validator, 'validate'));
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @param \Symfony\Component\Form\FormView $view
|
|
|
|
* @param \Symfony\Component\Form\FormInterface $form
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2012-07-23 23:02:37 +02:00
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
$view->vars = array_merge($view->vars, array(
|
2012-06-19 21:14:52 +02:00
|
|
|
'captcha_width' => $options['width'],
|
|
|
|
'captcha_height' => $options['height'],
|
2012-12-04 00:54:39 +01:00
|
|
|
'captcha_code' => $this->generator->getCaptchaCode($form->getName(), $options),
|
2012-06-19 21:14:52 +02:00
|
|
|
'value' => '',
|
2012-11-14 04:33:36 +01:00
|
|
|
));
|
2011-09-09 19:46:12 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
|
|
|
|
*/
|
2012-06-19 21:14:52 +02:00
|
|
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
2011-09-09 19:46:12 +02:00
|
|
|
{
|
2012-06-29 16:23:05 +02:00
|
|
|
$this->options['property_path'] = false;
|
2012-06-19 21:14:52 +02:00
|
|
|
$resolver->setDefaults($this->options);
|
2011-09-09 19:46:12 +02:00
|
|
|
}
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-06-19 21:14:52 +02:00
|
|
|
public function getParent()
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-08-25 22:50:59 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'captcha';
|
|
|
|
}
|
2012-06-19 21:14:52 +02:00
|
|
|
}
|