<commit_before><?php
namespace Wa72\Formlib;
use Symfony\Component\DomCrawler\Crawler;
use Wa72\Formlib\Field\InputCheckbox;
use Wa72\Formlib\Field\InputCheckboxGroup;
use Wa72\Formlib\Field\InputEmail;
use Wa72\Formlib\Field\InputHidden;
use Wa72\Formlib\Field\InputHiddenCsrfprotection;
use Wa72\Formlib\Field\InputNumber;
use Wa72\Formlib\Field\InputRadioGroup;
use Wa72\Formlib\Field\InputTel;
use Wa72\Formlib\Field\InputText;
use Wa72\Formlib\Field\InputUrl;
use Wa72\Formlib\Field\Select;
use Wa72\Formlib\Field\Textarea;

/**
 * FormHandler handles a form through its complete lifecycle:
 * create a form, display it, process data
 */
class FormHandler
{
    /**
     * @var \Psr\Log\LoggerInterface|null
     */
    protected $logger;

    /**
     * @var \Swift_Mailer
     */
    protected $mailer;

    protected $is_valid = false;
    protected $is_processed = false;

    /**
     * @var Form
     */
    protected $form;

    /**
     * @var FormprocessorInterface[]
     */
    protected $processors = array();

    /**
     * @var FormRendererInterface
     */
    protected $renderer;

    public function __construct(\Psr\Log\LoggerInterface $logger = null, \Swift_Mailer $mailer = null)
    {
        $this->logger = $logger;
        $this->mailer = $mailer;
    }

    /**
     * Creates and returns a new, empty form object.
     * Fields must still be added.
     * A reference is kept in $this->form.
     *
     * @param $name
     * @return \Wa72\Formlib\Form
     *
     * @see createFormFromYamlFile()
     * @see createFormFromArray()
     */
    public function createForm($name)
    {
        $this->form = new Form($name);
        if (empty($this->renderer)) $this->renderer = new FormRendererGeneric();
        $this->renderer->setForm($this->form);
        return $this->form;
    }

    public function registerProcessor(FormprocessorInterface $processor)
    {
        $this->processors[] = $processor;
    }

    public function getProcessors()
    {
        return $this->processors;
    }

    /**
     * @return FormRendererGeneric
     */
    public function getRenderer()
    {
        return $this->renderer;
    }

    public function setRenderer(FormRendererInterface $renderer)
    {
        $this->renderer = $renderer;
        if ($this->form) $this->renderer->setForm($this->form);
    }

    /**
     * @return Form
     */
    public function getForm()
    {
        return $this->form;
    }

    /**
     * Get the HTML source code of the rendered form
     *
     * @throws \Exception
     * @return string HTML code of the rendered form
     */
    public function displayForm()
    {
        if (!($this->renderer instanceof FormRendererReturningRenderedFormInterface)) throw new \Exception('No FormRenderer implementing renderForm() set in FormHandler');
        return $this->renderer->renderForm();
    }


    /**
     * Bind data from an array (e.g. $_POST) to the form and check whether the form is valid
     *
     * @param array $data
     * @return bool
     */
    public function bindAndValidateForm(array $data)
    {
        $this->form->bind($data);
        $this->is_valid = $this->form->isValid();
        return $this->is_valid;
    }

    /**
     * Process the form using Formprocessors registered by registerProcessor()
     *
     * @return bool True if all processors succeed, false if at least one fails
     * @throws \LogicException
     */
    public function processForm()
    {
        if (!($this->is_valid)) throw new \LogicException('Form must be valid before processing');
        $this->logger->info('Form {form} on page {url} submitted from IP address {ip} using {ua}',
            array(
                'form' => $this->form->getName(),<commit_msg>added function getErrors() to FormHandler<commit_after><?php
namespace Wa72\Formlib;
use Symfony\Component\DomCrawler\Crawler;
use Wa72\Formlib\Field\InputCheckbox;
use Wa72\Formlib\Field\InputCheckboxGroup;
use Wa72\Formlib\Field\InputEmail;
use Wa72\Formlib\Field\InputHidden;
use Wa72\Formlib\Field\InputHiddenCsrfprotection;
use Wa72\Formlib\Field\InputNumber;
use Wa72\Formlib\Field\InputRadioGroup;
use Wa72\Formlib\Field\InputTel;
use Wa72\Formlib\Field\InputText;
use Wa72\Formlib\Field\InputUrl;
use Wa72\Formlib\Field\Select;
use Wa72\Formlib\Field\Textarea;

/**
 * FormHandler handles a form through its complete lifecycle:
 * create a form, display it, process data
 */
class FormHandler
{
    /**
     * @var \Psr\Log\LoggerInterface|null
     */
    protected $logger;

    /**
     * @var \Swift_Mailer
     */
    protected $mailer;

    protected $is_validated = false;
    protected $is_valid = false;
    protected $is_processed = false;

    /**
     * @var Form
     */
    protected $form;

    /**
     * @var FormprocessorInterface[]
     */
    protected $processors = array();

    /**
     * @var FormRendererInterface
     */
    protected $renderer;

    public function __construct(\Psr\Log\LoggerInterface $logger = null, \Swift_Mailer $mailer = null)
    {
        $this->logger = $logger;
        $this->mailer = $mailer;
    }

    /**
     * Creates and returns a new, empty form object.
     * Fields must still be added.
     * A reference is kept in $this->form.
     *
     * @param $name
     * @return \Wa72\Formlib\Form
     *
     * @see createFormFromYamlFile()
     * @see createFormFromArray()
     */
    public function createForm($name)
    {
        $this->form = new Form($name);
        if (empty($this->renderer)) $this->renderer = new FormRendererGeneric();
        $this->renderer->setForm($this->form);
        return $this->form;
    }

    public function registerProcessor(FormprocessorInterface $processor)
    {
        $this->processors[] = $processor;
    }

    public function getProcessors()
    {
        return $this->processors;
    }

    /**
     * @return FormRendererGeneric
     */
    public function getRenderer()
    {
        return $this->renderer;
    }

    public function setRenderer(FormRendererInterface $renderer)
    {
        $this->renderer = $renderer;
        if ($this->form) $this->renderer->setForm($this->form);
    }

    /**
     * @return Form
     */
    public function getForm()
    {
        return $this->form;
    }

    /**
     * Get the HTML source code of the rendered form
     *
     * @throws \Exception
     * @return string HTML code of the rendered form
     */
    public function displayForm()
    {
        if (!($this->renderer instanceof FormRendererReturningRenderedFormInterface)) throw new \Exception('No FormRenderer implementing renderForm() set in FormHandler');
        return $this->renderer->renderForm();
    }


    /**
     * Bind data from an array (e.g. $_POST) to the form and check whether the form is valid
     *
     * @param array $data
     * @return bool
     */
    public function bindAndValidateForm(array $data)
    {
        $this->form->bind($data);
        $this->is_validated = true;
        $this->is_valid = $this->form->isValid();
        return $this->is_valid;
    }

    /**
     * Get all validation error messages
     *
     * @return array
     */
    public function getErrors()
    {
        if (!($this->is_validated)) throw new \LogicException('Form is not validated yet');
        if ($this->is_valid) return array();
        $fields = $this->form->getFields();
        $field_errors = array();
        foreach($fields as $name => $field) {
            if ($field->getValidationStatus() === ValidatorInterface::STATUS_INVALID) {
                $field_errors[$field->getFullName()] = $field->getErrorMessages();
            }
        }
        return array(
            'global_errors' => $this->form->getErrorMessages(),
            'field_errors' => $field_errors
        );
    }

    /**
     * Process the form using Formprocessors registered by registerProcessor()
     *
     * @return bool True if all processors succeed, false if at least one fails
     * @throws \LogicException
     */
    public function processForm()
    {
        if (!($this->is_valid)) throw new \LogicException('Form must be valid before processing');
        $this->logger->info('Form {form} on page {url} submitted from IP address {ip} using {ua}',
            array(
                'form' => $this->form->getName(),
{'fraction_non_alphanumeric': 0.12444337465398965, 'fraction_numerical': 0.006739679865206402, 'mean_word_length': 2.4726284997910573, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7419132', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2426, 'n_words': 608}