<commit_before><?php

namespace Seferov\RequestValidatorBundle\Validator;

use Seferov\RequestValidatorBundle\Annotation\Validator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Class RequestValidator.
 *
 * @author <NAME> <<EMAIL>>
 */
class RequestValidator implements RequestValidatorInterface
{
    /**
     * @var Validator[]
     */
    private $annotations;

    /**
     * @var ConstraintViolationList
     */
    private $errors;

    /**
     * @var ValidatorInterface
     */
    private $validator;

    /**
     * @var Request
     */
    private $request;

    public function __construct(Request $request, ValidatorInterface $validator, array $annotations)
    {
        $this->request = $request;
        $this->validator = $validator;
        $this->annotations = $annotations;
    }

    /**
     * @return ConstraintViolationList
     */
    public function getErrors()
    {
        $this->errors = new ConstraintViolationList();
        foreach ($this->annotations as $annotation) {
            if (!$this->getParameterBag()->has($annotation->getName()) && $annotation->isRequired()) {
                $this->errors->set($annotation->getName(), $this->validator->validate(null, new Assert\NotNull())->get(0));
                continue;
            }

            if (!$this->getParameterBag()->has($annotation->getName()) && $annotation->isOptional()) {
                continue;
            }

            $requestValue = $this->getParameterBag()->get($annotation->getName());

            // Fix for Assert\All constraint
            foreach ($annotation->getConstraints() as $constraint) {
                if ($constraint instanceof Assert\All && !is_array($requestValue)) {
                    $this->errors->set($annotation->getName(), $this->validator->validate($requestValue, new Assert\Type(['type' => 'array']))->get(0));
                    continue 2;
                }
            }

            // Validate the value with all the constraints defined
            $violationList = $this->validator->validate($requestValue, $annotation->getConstraints());
            foreach ($violationList as $violation) {
                $this->errors->set($annotation->getName(), $violation);
            }
        }

        return $this->errors;
    }

    /**
     * @param $path
     *
     * @return mixed
     */
    public function get($path)
    {
        if (!$this->getParameterBag()->has($path)) {
            return $this->getAnnotation($path)->getDefault();
        }

        return $this->getParameterBag()->get($path);<commit_msg>fix null value for arrays<commit_after><?php

namespace Seferov\RequestValidatorBundle\Validator;

use Seferov\RequestValidatorBundle\Annotation\Validator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Class RequestValidator.
 *
 * @author <NAME> <<EMAIL>>
 */
class RequestValidator implements RequestValidatorInterface
{
    /**
     * @var Validator[]
     */
    private $annotations;

    /**
     * @var ConstraintViolationList
     */
    private $errors;

    /**
     * @var ValidatorInterface
     */
    private $validator;

    /**
     * @var Request
     */
    private $request;

    public function __construct(Request $request, ValidatorInterface $validator, array $annotations)
    {
        $this->request = $request;
        $this->validator = $validator;
        $this->annotations = $annotations;
    }

    /**
     * @return ConstraintViolationList
     */
    public function getErrors()
    {
        $this->errors = new ConstraintViolationList();
        foreach ($this->annotations as $annotation) {
            if (!$this->getParameterBag()->has($annotation->getName()) && $annotation->isRequired()) {
                $this->errors->set($annotation->getName(), $this->validator->validate(null, new Assert\NotNull())->get(0));
                continue;
            }

            if (!$this->getParameterBag()->has($annotation->getName()) && $annotation->isOptional()) {
                continue;
            }

            $requestValue = $this->getParameterBag()->get($annotation->getName());

            // Fix for Assert\All constraint
            foreach ($annotation->getConstraints() as $constraint) {
                if ($constraint instanceof Assert\All) {
                    if ($requestValue === null) {
                        $error = $this->validator->validate(null, new Assert\NotNull())->get(0);
                    } elseif (!is_array($requestValue)) {
                        $error = $this->validator->validate($requestValue, new Assert\Type(['type' => 'array']))->get(0);
                    } else {
                        continue;
                    }

                    $this->errors->set($annotation->getName(), $error);

                    continue 2;
                }
            }

            // Validate the value with all the constraints defined
            $violationList = $this->validator->validate($requestValue, $annotation->getConstraints());
            foreach ($violationList as $violation) {
                $this->errors->set($annotation->getName(), $violation);
            }
        }

        return $this->errors;
    }

    /**
     * @param $path
     *
     * @return mixed
     */
    public function get($path)
    {
        if (!$this->getParameterBag()->has($path)) {
            return $this->getAnnotation($path)->getDefault();
        }

        return $this->getParameterBag()->get($path);
{'fraction_non_alphanumeric': 0.14266047151953193, 'fraction_numerical': 0.0012046119428669765, 'mean_word_length': 2.2451144611948632, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 98, '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': '751659', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1457, 'n_words': 279}