<commit_before><?php

namespace Synapse\Validator;

use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Constraints as Assert;
use Synapse\Entity\AbstractEntity;

/**
 * Abstract class for validating arrays
 *
 * Simply extend this class and define getConstraints to create a concrete Validator
 */
abstract class AbstractArrayValidator
{
    /**
     * Symfony validator component
     *
     * @var Validator
     */
    protected $validator;

    /**
     * @var array
     */
    protected $contextData;

    /**
     * @var AbstractEntity
     */
    protected $contextEntity;

    /**
     * @param Validator $validator
     */
    public function __construct(Validator $validator)
    {
        $this->validator = $validator;

        return $this;
    }

    /**
     * Validate an associative array using the constraints defined in
     * getConstraints() and getOptionalConstraints().
     *
     * All constraints from getConstraints() are used.
     *
     * Constraints from getOptionalConstraints() are only used if the field exists in $values.
     *
     * @param  array                   $values Values to validate
     * @return ConstraintViolationList
     */
    public function validate(array $values)
    {
        $constraints = $this->getConstraints();

        $arrayConstraint = new Assert\Collection($constraints);

        return $this->validator->validateValue(
            $values,
            $arrayConstraint
        );
    }

    /**
     * Set the data being validated so that it can be passed to
     * validators that need it.
     *
     * @param array $contextData
     */
    public function setContextData(array $contextData)
    {
        $this->contextData = $contextData;
    }

    /**
     * Set the entity that will be updated with the data being validated so
     * that it can be passed to validators that need it.
     *
     * @param AbstractEntity $contextEntity
     */
    public function setContextEntity(AbstractEntity $contextEntity)
    {
        $this->contextEntity = $contextEntity;
    }

    /**
     * Return an array of validation rules for use with Symfony Validator
     *
     * @link http://silex.sensiolabs.org/doc/providers/validator.html#validating-associative-arrays
     *
     * In order to make a field optional, simply use the Optional constraint.
     *
     * If a field should be optional, but should have constraints if it exists,
     * simply provide the constraints to the constructor of the Optional constraint as such:
     *
     *     'first_name' => new Assert\Optional(new Assert\NotBlank())
     *
     * To add multiple constraints, provide an array:
     *
     *     'first_name' => new Assert\Optional([
     *         new Assert\NotBlank(),
     *         new Assert\NotNull(),
     *     ])<commit_msg>Refs #80 - Set the contextData in the validate method instead of in its own setter.<commit_after><?php

namespace Synapse\Validator;

use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Constraints as Assert;
use Synapse\Entity\AbstractEntity;

/**
 * Abstract class for validating arrays
 *
 * Simply extend this class and define getConstraints to create a concrete Validator
 */
abstract class AbstractArrayValidator
{
    /**
     * Symfony validator component
     *
     * @var Validator
     */
    protected $validator;

    /**
     * @var array
     */
    protected $contextData;

    /**
     * @var AbstractEntity
     */
    protected $contextEntity;

    /**
     * @param Validator $validator
     */
    public function __construct(Validator $validator)
    {
        $this->validator = $validator;

        return $this;
    }

    /**
     * Validate an associative array using the constraints defined in
     * getConstraints() and getOptionalConstraints().
     *
     * All constraints from getConstraints() are used.
     *
     * Constraints from getOptionalConstraints() are only used if the field exists in $values.
     *
     * @param  array                   $values Values to validate
     * @return ConstraintViolationList
     */
    public function validate(array $values)
    {
        $this->contextData = $values;

        $constraints = $this->getConstraints();

        $arrayConstraint = new Assert\Collection($constraints);

        return $this->validator->validateValue(
            $values,
            $arrayConstraint
        );
    }

    /**
     * Set the entity that will be updated with the data being validated so
     * that it can be passed to validators that need it.
     *
     * @param AbstractEntity $contextEntity
     */
    public function setContextEntity(AbstractEntity $contextEntity)
    {
        $this->contextEntity = $contextEntity;
    }

    /**
     * Return an array of validation rules for use with Symfony Validator
     *
     * @link http://silex.sensiolabs.org/doc/providers/validator.html#validating-associative-arrays
     *
     * In order to make a field optional, simply use the Optional constraint.
     *
     * If a field should be optional, but should have constraints if it exists,
     * simply provide the constraints to the constructor of the Optional constraint as such:
     *
     *     'first_name' => new Assert\Optional(new Assert\NotBlank())
     *
     * To add multiple constraints, provide an array:
     *
     *     'first_name' => new Assert\Optional([
     *         new Assert\NotBlank(),
     *         new Assert\NotNull(),
     *     ])
{'fraction_non_alphanumeric': 0.08818279186004999, 'fraction_numerical': 0.0003570153516601214, 'mean_word_length': 2.4143814747105425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '3826439', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1445, 'n_words': 463}