<commit_before><?php

namespace Kenjis\Validation;

use Sirius\Validation\RuleFactory;

class ValidatorEx extends \Sirius\Validation\Validator
{
    protected $validatedData = [];

    public function __construct(RuleFactory $ruleFactory = null, ErrorMessage $errorMessagePrototype = null)
    {
        parent::__construct($ruleFactory, $errorMessagePrototype);

        // register rules [Added by kenjis]
        $rulesClasses = array(
            'IsString',
            'NoControl',
            'NoTabAndNewLine',
            'ValidUtf8',
            'Url'   // overwrite
        );
        foreach ($rulesClasses as $class) {
            $fullClassName = '\\' . __NAMESPACE__ . '\Rule\\' . $class;
            $name = strtolower(str_replace('\\', '', $class));
            $errorMessage = constant($fullClassName . '::MESSAGE');
            $labeledErrorMessage = constant($fullClassName . '::LABELED_MESSAGE');
            $this->ruleFactory->register($name, $fullClassName, $errorMessage, $labeledErrorMessage);
        }
    }

    public function add($selector, $name = null, $options = null, $messageTemplate = null, $label = null)
    {
        // the $selector is an associative array with $selector => $rules
        if (func_num_args() == 1) {
            if (!is_array($selector)) {
                throw new \InvalidArgumentException('If $selector is the only argument it must be an array');
            }

            return $this->addMultiple($selector);
        }

        // check if the selector is in the form of 'selector:Label'
        if (strpos($selector, ':') !== false) {
            list($selector, $label) = explode(':', $selector, 2);
        }

        $this->ensureSelectorRulesExist($selector);
        
        // remove existing rule [Added by kenjis]
        call_user_func(array($this->rules[$selector], 'remove'), $name, $options);
        
        call_user_func(array($this->rules[$selector], 'add'), $name, $options, $messageTemplate, $label);

        return $this;
    }

    public function validate($data = null)
    {
        if ($data !== null) {
            $this->setData($data);
        }
        // data was already validated, return the results immediately
        if ($this->wasValidated === true) {
            return $this->wasValidated && count($this->messages) === 0;
        }
        foreach ($this->rules as $selector => $valueValidator) {
            foreach ($this->getDataWrapper()->getItemsBySelector($selector) as $valueIdentifier => $value) {
                /* @var $valueValidator \Sirius\Validation\ValueValidator */
                if (!$valueValidator->validate($value, $valueIdentifier, $this->getDataWrapper())) {
                    foreach ($valueValidator->getMessages() as $message) {
                        $this->addMessage($valueIdentifier, $message);
                    }
                } else {
                    $this->setValidatedData($valueIdentifier, $value);
                }
            }
        }
        $this->wasValidated = true;

        return $this->wasValidated && count($this->messages) === 0;
    }

    protected function setValidatedData($valueIdentifier, $value)
    {
        // handle array
        if (preg_match('/(.+)\[(.+)\]\[(.+)\]/i', $valueIdentifier, $matches)) {
            $name = $matches[1];
            $key1 = $matches[2];
            $key2 = $matches[3];
            $this->validatedData[$name][$key1][$key2] = $value;
        } else {
            $this->validatedData[$valueIdentifier] = $value;
        }
    }

    protected function ensureSelectorRulesExist($selector)
    {
        if (!isset($this->rules[$selector])) {
            $this->rules[$selector] = new ValueValidator($this->getRuleFactory(), $this->getErroMessagePrototype());
        }
    }

    public function getValidated($item = null)
    {
        if ($item) {
            return isset($this->validatedData[$item]) ? $this->validatedData[$item] : null;
        }
        return $this->validatedData;
    }<commit_msg>Refactor<commit_after><?php

namespace Kenjis\Validation;

use Sirius\Validation\RuleFactory;

class ValidatorEx extends \Sirius\Validation\Validator
{
    protected $validatedData = [];

    public function __construct(RuleFactory $ruleFactory = null, ErrorMessage $errorMessagePrototype = null)
    {
        parent::__construct($ruleFactory, $errorMessagePrototype);

        // register rules [Added by kenjis]
        $rulesClasses = array(
            'IsString',
            'NoControl',
            'NoTabAndNewLine',
            'ValidUtf8',
            'Url'   // overwrite
        );
        foreach ($rulesClasses as $class) {
            $fullClassName = '\\' . __NAMESPACE__ . '\Rule\\' . $class;
            $name = strtolower(str_replace('\\', '', $class));
            $errorMessage = constant($fullClassName . '::MESSAGE');
            $labeledErrorMessage = constant($fullClassName . '::LABELED_MESSAGE');
            $this->ruleFactory->register($name, $fullClassName, $errorMessage, $labeledErrorMessage);
        }
    }

    public function add($selector, $name = null, $options = null, $messageTemplate = null, $label = null)
    {
        // the $selector is an associative array with $selector => $rules
        if (func_num_args() == 1) {
            if (!is_array($selector)) {
                throw new \InvalidArgumentException('If $selector is the only argument it must be an array');
            }

            return $this->addMultiple($selector);
        }

        // check if the selector is in the form of 'selector:Label'
        if (strpos($selector, ':') !== false) {
            list($selector, $label) = explode(':', $selector, 2);
        }

        $this->ensureSelectorRulesExist($selector);
        
        // remove existing rule [Added by kenjis]
        call_user_func(array($this->rules[$selector], 'remove'), $name, $options);
        
        call_user_func(array($this->rules[$selector], 'add'), $name, $options, $messageTemplate, $label);

        return $this;
    }

    public function validate($data = null)
    {
        if ($data !== null) {
            $this->setData($data);
        }

        // data was already validated, return the results immediately
        if ($this->wasValidated === true) {
            return $this->wasValidated && count($this->messages) === 0;
        }

        foreach ($this->rules as $selector => $valueValidator) {
            foreach ($this->getDataWrapper()->getItemsBySelector($selector) as $valueIdentifier => $value) {
                /* @var $valueValidator \Kenjis\Validation\ValueValidator */
                if (!$valueValidator->validate($value, $valueIdentifier, $this->getDataWrapper())) {
                    $this->addErrorMessages($valueValidator, $valueIdentifier);
                } else {
                    $this->setValidatedData($valueIdentifier, $value);
                }
            }
        }
        $this->wasValidated = true;

        return $this->wasValidated && count($this->messages) === 0;
    }

    protected function addErrorMessages($valueValidator, $valueIdentifier)
    {
        foreach ($valueValidator->getMessages() as $message) {
            $this->addMessage($valueIdentifier, $message);
        }
    }

    protected function setValidatedData($valueIdentifier, $value)
    {
        // handle array
        if (preg_match('/(.+)\[(.+)\]\[(.+)\]/i', $valueIdentifier, $matches)) {
            $name = $matches[1];
            $key1 = $matches[2];
            $key2 = $matches[3];
            $this->validatedData[$name][$key1][$key2] = $value;
        } else {
            $this->validatedData[$valueIdentifier] = $value;
        }
    }

    protected function ensureSelectorRulesExist($selector)
    {
        if (!isset($this->rules[$selector])) {
            $this->rules[$selector] = new ValueValidator($this->getRuleFactory(), $this->getErroMessagePrototype());
        }
    }

    public function getValidated($item = null)
    {
        if ($item) {
            return isset($this->validatedData[$item]) ? $this->validatedData[$item] : null;
        }
        return $this->validatedData;
    }
{'fraction_non_alphanumeric': 0.1462339252908757, 'fraction_numerical': 0.00293937538273117, 'mean_word_length': 2.2366230677764567, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, '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': '3175826', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2170, 'n_words': 464}