<commit_before><?php

namespace Oro\Bundle\FormBundle\Form\Extension;

use Symfony\Component\Validator\MetadataFactoryInterface;
use Symfony\Component\Validator\MetadataInterface;
use Symfony\Component\Validator\PropertyMetadataInterface;
use Symfony\Component\Validator\Constraint;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class JsValidationExtension extends AbstractTypeExtension
{
    /**
     * @param MetadataFactoryInterface $metadataFactory
     */
    public function __construct(MetadataFactoryInterface $metadataFactory)
    {
        $this->metadataFactory = $metadataFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        $validationGroups = $this->getValidationGroups($view);

        if ($options['data_class']) {
            $metadata = $this->metadataFactory->getMetadataFor($options['data_class']);

            foreach ($this->getMappedChildren($view) as $child) {
                $constraints = $this->getFormViewConstraints($child, $metadata, $validationGroups);
                $this->addDataValidationAttribute($child, $constraints);
            }
        }
    }

    protected function addDataValidationAttribute(FormView $view, array $constraints)
    {
        $value = array();
        foreach ($constraints as $constraint) {
            $value[$this->getConstraintName($constraint)] = $this->getConstraintProperties($constraint);
        }
        if ($value) {
            $target = $view;
            if (isset($view->vars['type']) && $view->vars['type'] == 'repeated') {
                $repeatedNames = array_keys($view->vars['value']);
                $target = $view->children[$repeatedNames[0]];
                $value['Repeated'] = array(
                    'first_name' => $repeatedNames[0],
                    'second_name' => $repeatedNames[1],
                    'invalid_message' => $view->vars['invalid_message'],
                    'invalid_message_parameters' => $view->vars['invalid_message_parameters'],
                );
            }
            if (!isset($target->vars['attr'])) {
                $target->vars['attr'] = array();
            }
            $target->vars['attr']['data-validation'] = json_encode($value);
        }
    }

    protected function getConstraintName(Constraint $constraint)
    {
        $class = get_class($constraint);
        $defaultClassPrefix = 'Symfony\\Component\\Validator\\Constraints\\';
        if (0 === strpos($class, $defaultClassPrefix)) {
            return str_replace($defaultClassPrefix, '', $class);
        }
        return $class;
    }

    protected function getConstraintProperties(Constraint $constraint)
    {
        $result = get_object_vars($constraint);
        unset($result['groups']);
        foreach ($result as $key => $value) {
            if (is_object($value)) {
                unset($result[$key]);
            }
        }
        return $result;
    }<commit_msg>BAP-2337: Add form extension to provide value of data-validation attribute - updated repeated validation<commit_after><?php

namespace Oro\Bundle\FormBundle\Form\Extension;

use Symfony\Component\Validator\MetadataFactoryInterface;
use Symfony\Component\Validator\MetadataInterface;
use Symfony\Component\Validator\PropertyMetadataInterface;
use Symfony\Component\Validator\Constraint;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

class JsValidationExtension extends AbstractTypeExtension
{
    /**
     * @param MetadataFactoryInterface $metadataFactory
     */
    public function __construct(MetadataFactoryInterface $metadataFactory)
    {
        $this->metadataFactory = $metadataFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        $validationGroups = $this->getValidationGroups($view);

        if ($options['data_class']) {
            $metadata = $this->metadataFactory->getMetadataFor($options['data_class']);

            foreach ($this->getMappedChildren($view) as $child) {
                $constraints = $this->getFormViewConstraints($child, $metadata, $validationGroups);
                $this->addDataValidationAttribute($child, $constraints);
            }
        }
    }

    protected function addDataValidationAttribute(FormView $view, array $constraints)
    {
        $value = array();
        foreach ($constraints as $constraint) {
            $value[$this->getConstraintName($constraint)] = $this->getConstraintProperties($constraint);
        }
        if ($value) {
            $target = $view;
            if (isset($view->vars['type']) && $view->vars['type'] == 'repeated') {
                $repeatedNames = array_keys($view->vars['value']);
                $target = $view->children[$repeatedNames[0]];

                $secondValue = array();
                $secondValue['Repeated'] = array(
                    'first_name' => $repeatedNames[0],
                    'second_name' => $repeatedNames[1],
                    'invalid_message' => $view->vars['invalid_message'],
                    'invalid_message_parameters' => $view->vars['invalid_message_parameters'],
                );
                $second = $view->children[$repeatedNames[1]];

                if (!isset($second->vars['attr'])) {
                    $second->vars['attr'] = array();
                }
                $second->vars['attr']['data-validation'] = json_encode($secondValue);
            }
            if (!isset($target->vars['attr'])) {
                $target->vars['attr'] = array();
            }
            $target->vars['attr']['data-validation'] = json_encode($value);
        }
    }

    protected function getConstraintName(Constraint $constraint)
    {
        $class = get_class($constraint);
        $defaultClassPrefix = 'Symfony\\Component\\Validator\\Constraints\\';
        if (0 === strpos($class, $defaultClassPrefix)) {
            return str_replace($defaultClassPrefix, '', $class);
        }
        return $class;
    }

    protected function getConstraintProperties(Constraint $constraint)
    {
        $result = get_object_vars($constraint);
        unset($result['groups']);
        foreach ($result as $key => $value) {
            if (is_object($value)) {
                unset($result[$key]);
            }
        }
        return $result;
    }
{'fraction_non_alphanumeric': 0.13656454808718183, 'fraction_numerical': 0.0019814052735863436, 'mean_word_length': 2.5279569892473117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '6961073', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1651, 'n_words': 284}