<commit_before><?php

/**
 * This file is part of the Valit package.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright 2017
 * @license MIT
 */

namespace Valit\Validators;

use BadMethodCallException;
use Valit\Contracts\CheckManager;
use Valit\Result\AssertionResultBag;
use Valit\Exceptions\InvalidValueException;

class ValueValidator extends AssertionResultBag
{
    /**
     * @var CheckManager
     */
    protected $manager;

    /**
     * @var bool
     *
     * @internal
     */
    public $throwOnFailure;

    /**
     * Constructor.
     *
     * @param CheckManager $manager        The manager that contains all our checks
     * @param mixed        $value          The value we are validating
     * @param bool         $throwOnFailure Should we throw an exception as soon as we encounter a failed result
     */
    public function __construct(CheckManager $manager, $value, $throwOnFailure = false)
    {
        $this->manager = $manager;
        $this->throwOnFailure = (bool) $throwOnFailure;
        parent::__construct($value, 'value');
    }

    /**
     * Execute checks by "calling" them.
     *
     * @param string $methodName
     * @param array  $args
     *
     * @return $this
     */
    public function __call($methodName, $args)
    {
        if ($methodName === 'as') {
            $this->alias($args[0]);

            return $this;
        }

        if (!$this->manager->hasCheck($methodName)) {
            throw new BadMethodCallException(sprintf(
                'Unknown method name "%s"',
                $methodName
            ));
        }

        $this->executeCheck($methodName, $args);

        return $this;
    }

    /**
     * Execute a check.
     *
     * @param string $checkName
     * @param array  $args
     *
     * @return $this
     */
    public function executeCheck($checkName, array $args)
    {
        $result = $this->manager->executeCheck($checkName, $this->value, $args);

        $this->addAssertionResult($result);

        return $this->throwOnFailure
            ? $this->orThrowException()
            : $this;
    }

    /**
     * Throw exceptions if any failures has occurred or occur later in the execution stream.
     *
     * @return $this
     *
     * @throws InvalidValueException if any failures have occurred
     */
    public function orThrowException()
    {
        if ($this->failures) {
            throw new InvalidValueException(
                sprintf('Failed %d out of %d validation checks', $this->failures, count($this->results)),
                $this->varName,
                $this->value,
                $this->results
            );
        }

        return $this;
    }

    /**
     * Throw exceptions if any failures has occurred or occur later in the execution stream.
     *
     * Alias of orThrowException()
     *
     * @see self::orThrowException()
     *
     * @return $this
     *
     * @throws InvalidValueException if any failures have occurred
     */
    public function throwExceptionIfNotSuccessful()
    {
        return $this->orThrowException();
    }

    /**
     * Check container against a number of assertions.
     *
     * @param array|Traversable $containerAssertionMap
     *
     * @return ContainerResultBag
     */
    public function contains($containerAssertionMap)
    {
        return (new ContainerValidator($this->manager, $this->value, $this->throwOnFailure))
            ->passes($containerAssertionMap);
    }
}<commit_msg>FIX pass on the alias<commit_after><?php

/**
 * This file is part of the Valit package.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright 2017
 * @license MIT
 */

namespace Valit\Validators;

use BadMethodCallException;
use Valit\Contracts\CheckManager;
use Valit\Result\AssertionResultBag;
use Valit\Exceptions\InvalidValueException;

class ValueValidator extends AssertionResultBag
{
    /**
     * @var CheckManager
     */
    protected $manager;

    /**
     * @var bool
     *
     * @internal
     */
    public $throwOnFailure;

    /**
     * Constructor.
     *
     * @param CheckManager $manager        The manager that contains all our checks
     * @param mixed        $value          The value we are validating
     * @param bool         $throwOnFailure Should we throw an exception as soon as we encounter a failed result
     */
    public function __construct(CheckManager $manager, $value, $throwOnFailure = false)
    {
        $this->manager = $manager;
        $this->throwOnFailure = (bool) $throwOnFailure;
        parent::__construct($value, 'value');
    }

    /**
     * Execute checks by "calling" them.
     *
     * @param string $methodName
     * @param array  $args
     *
     * @return $this
     */
    public function __call($methodName, $args)
    {
        if ($methodName === 'as') {
            $this->alias($args[0]);

            return $this;
        }

        if (!$this->manager->hasCheck($methodName)) {
            throw new BadMethodCallException(sprintf(
                'Unknown method name "%s"',
                $methodName
            ));
        }

        $this->executeCheck($methodName, $args);

        return $this;
    }

    /**
     * Execute a check.
     *
     * @param string $checkName
     * @param array  $args
     *
     * @return $this
     */
    public function executeCheck($checkName, array $args)
    {
        $result = $this->manager->executeCheck($checkName, $this->value, $args);

        $this->addAssertionResult($result);

        return $this->throwOnFailure
            ? $this->orThrowException()
            : $this;
    }

    /**
     * Throw exceptions if any failures has occurred or occur later in the execution stream.
     *
     * @return $this
     *
     * @throws InvalidValueException if any failures have occurred
     */
    public function orThrowException()
    {
        if ($this->failures) {
            throw new InvalidValueException(
                sprintf('Failed %d out of %d validation checks', $this->failures, count($this->results)),
                $this->varName,
                $this->value,
                $this->results
            );
        }

        return $this;
    }

    /**
     * Throw exceptions if any failures has occurred or occur later in the execution stream.
     *
     * Alias of orThrowException()
     *
     * @see self::orThrowException()
     *
     * @return $this
     *
     * @throws InvalidValueException if any failures have occurred
     */
    public function throwExceptionIfNotSuccessful()
    {
        return $this->orThrowException();
    }

    /**
     * Check container against a number of assertions.
     *
     * @param array|Traversable $containerAssertionMap
     *
     * @return ContainerResultBag
     */
    public function contains($containerAssertionMap)
    {
        $containerValidator = new ContainerValidator($this->manager, $this->value, $this->throwOnFailure);

        return $containerValidator
            ->alias($this->varName)
            ->passes($containerAssertionMap);
    }
}
{'fraction_non_alphanumeric': 0.11172629187961386, 'fraction_numerical': 0.0014196479273140261, 'mean_word_length': 2.0366379310344827, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 57, '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': '7038277', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1903, 'n_words': 505}