<commit_before><?php
/**
 * @package shideon/data-mover
 * @copyright (c) 2015 <NAME>
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */

namespace Shideon\DataMover;

/**
 * Shideon\DataMover\DataMover
 *
 * @author <NAME> <<EMAIL>>
 */
class DataMover implements DataMoverInterface
{
    /**
     * @var ModelInterface
     */
    protected $model;

    /**
     * @var \Iterator
     */
    protected $data;

    /**
     * Constructor
     * @param ModelInterface|null $model
     * @param \Iterator|null $data
     */
    public function __construct(ModelInterface $model, \Iterator $data)
    {
        $this->setModel($model);
        $this->setData($data);
    }

    /**
     * @inheritDoc
     */
    public function setModel(ModelInterface $model)
    {
        $this->model = $model;
    }

    /**
     * @inheritDoc
     */
    public function setData(\Iterator $data)
    {
        $this->data = $data;
    }

    /**
     * @inheritDoc
     */
    public function run()
    {
        if (!$this->model) {
            throw new \LogicException('Must have model before running');
        }

        if (!$this->data) {
            throw new \LogicException('Must have data to iterate before running');
        }

        foreach ($this->data as $iterationInput) {
            $this->handleIteration($iterationInput);
        }
    }

    /**
     * @inheritDoc
     */
    protected function handleIteration($iterationInput)
    {
        $iterationInput = $this->model->createIterationInput($iterationInput);
        $iterationOutput = new \StdClass();

        $this->model->beginIteration($iterationInput, $iterationOutput);

        foreach ($this->model->getFields() as $field) {
            $iterationOutput->{$field->getName()} = $field->extractValue($iterationInput);
        }

        // @todo validation

        $this->model->endIteration($iterationOutput);
    }
}<commit_msg>Added precursory validation logic<commit_after><?php
/**
 * @package shideon/data-mover
 * @copyright (c) 2015 <NAME>
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */

namespace Shideon\DataMover;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator;

/**
 * Shideon\DataMover\DataMover
 *
 * @author <NAME> <<EMAIL>>
 */
class DataMover implements DataMoverInterface
{
    /**
     * @var ModelInterface
     */
    protected $model;

    /**
     * @var \Iterator
     */
    protected $data;

    /**
     * @var array
     */
    protected $validationErrors = [];

    /**
     * @var Validator
     */
    protected $validator;

    /**
     * Constructor
     * @param ModelInterface|null $model
     * @param \Iterator|null $data
     */
    public function __construct(ModelInterface $model, \Iterator $data)
    {
        $this->setModel($model);
        $this->setData($data);
    }

    /**
     * @inheritDoc
     */
    public function setModel(ModelInterface $model)
    {
        $this->model = $model;
    }

    /**
     * @inheritDoc
     */
    public function setData(\Iterator $data)
    {
        $this->data = $data;
    }

    /**
     * @inheritDoc
     */
    public function run()
    {
        if (!$this->model) {
            throw new \LogicException('Must have model before running');
        }

        if (!$this->data) {
            throw new \LogicException('Must have data to iterate before running');
        }

        if (!$this->validator) {
            $this->validator = Validation::createValidatorBuilder()
                ->addMethodMapping('loadValidatorMetadata')
                ->getValidator();
        }

        foreach ($this->data as $iterationInput) {
            $this->handleIteration($iterationInput);
        }
    }

    /**
     * @inheritDoc
     */
    protected function handleIteration($iterationInput)
    {
        $iterationInput = $this->model->createIterationInput($iterationInput);
        $iterationOutput = [];

        $this->model->beginIteration($iterationInput, $iterationOutput);

        foreach ($this->model->getFields() as $field) {
            $name = $field->getName();

            // set value of output field by extracting using field
            $iterationOutput[$name] = $field->extractValue($iterationInput);

            $errors = $this->validator->validateValue(
                $iterationOutput[$name],
                $field->getConstraints()
            );

            if (count($errors) > 0) {
                $this->validationErrors[$name] = $errors;
            }

            // @todo determine how validation errors are handled
        }

        $this->model->endIteration($iterationOutput);
    }
}
{'fraction_non_alphanumeric': 0.14162348877374784, 'fraction_numerical': 0.0019430051813471502, 'mean_word_length': 2.0661813368630044, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 63, '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': '6154831', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1311, 'n_words': 260}