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

namespace Pancoast\DataMigrator;

use Pancoast\DataMigrator\Exception\FieldViolationException;
use Pancoast\DataMigrator\Exception\SkippableModelIterationException;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator;

/**
 * Data migrator
 *
 * This is the core public interface to interact with
 *
 * @author <NAME> <<EMAIL>>
 */
class Migrator implements MigratorInterface
{
    /**
     * @var ModelInterface
     */
    protected $model;

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

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

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

    /**
     * @var \Exception[] Array of exceptions for an iteration
     */
    protected $iterationExceptions = [];

    /**
     * @var int Current iteration count
     */
    protected $iterationCount = 0;

    /**
     * Constructor
     * @param ModelInterface $model
     * @param \Iterator $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();
        }

        $this->model->begin();

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

        $this->model->end();
    }

    /**
     * @inheritDoc
     */
    protected function handleIteration($iterationInput)
    {
        try {
            $this->iterationCount ++;

            // input that gets passed to fields in user model
            $iterationInput = $this->model->createIterationInput($this->iterationCount, $iterationInput);

            // output that gets handled in user model
            $iterationOutput = [];

            // collection of field exceptions for this iteration
            $fieldViolationList = [];

            $this->model->beginIteration($this->iterationCount, $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);

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

                if (count($violations) > 0) {
                    $fieldErrorMessages = [];

                    foreach ($violations as $violation) {
                        $fieldErrorMessages[] = $violation->getMessage();
                    }

                    $fieldViolationList[] = FieldViolationException::build(
                        $name,
                        implode(', ', $fieldErrorMessages)
                    );
                }
            }

            if (!empty($fieldViolationList)) {
                $this->model->handleIterationConstraintViolations($this->iterationCount, $fieldViolationList);
            }

        // Skippable exceptions are caught, saved, then ignored.
        // All others are thrown up.
        } catch (SkippableModelIterationException $e) {
            $this->iterationExceptions[$this->iterationCount] = $e;
        }

        $this->model->endIteration($this->iterationCount, $iterationOutput);
    }
<commit_msg>Migrator calls on field constraint exception handler method<commit_after><?php
/**
 * @package johnpancoast/data-migrator
 * @copyright (c) 2015 <NAME>
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */

namespace Pancoast\DataMigrator;

use Pancoast\DataMigrator\Exception\FieldViolationException;
use Pancoast\DataMigrator\Exception\SkippableModelIterationException;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator;

/**
 * Data migrator
 *
 * This is the core public interface to interact with
 *
 * @author <NAME> <<EMAIL>>
 */
class Migrator implements MigratorInterface
{
    /**
     * @var ModelInterface
     */
    protected $model;

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

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

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

    /**
     * @var \Exception[] Array of exceptions for an iteration
     */
    protected $iterationExceptions = [];

    /**
     * @var int Current iteration count
     */
    protected $iterationCount = 0;

    /**
     * Constructor
     * @param ModelInterface $model
     * @param \Iterator $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();
        }

        $this->model->begin();

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

        $this->model->end();
    }

    /**
     * @inheritDoc
     */
    protected function handleIteration($iterationInput)
    {
        try {
            $this->iterationCount ++;

            // input that gets passed to fields in user model
            $iterationInput = $this->model->createIterationInput($this->iterationCount, $iterationInput);

            // output that gets handled in user model
            $iterationOutput = [];

            // collection of field exceptions for this iteration
            $fieldViolationList = [];

            $this->model->beginIteration($this->iterationCount, $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);

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

                if (count($violations) > 0) {
                    $field->handleConstraintViolations($violations);

                    $fieldErrorMessages = [];

                    foreach ($violations as $violation) {
                        $fieldErrorMessages[] = $violation->getMessage();
                    }

                    $fieldViolationList[] = FieldViolationException::build(
                        $name,
                        implode(', ', $fieldErrorMessages)
                    );
                }
            }

            if (!empty($fieldViolationList)) {
                $this->model->handleIterationConstraintViolations($this->iterationCount, $fieldViolationList);
            }

        // Skippable exceptions are caught, saved, then ignored.
        // All others are thrown up.
        } catch (SkippableModelIterationException $e) {
            $this->iterationExceptions[$this->iterationCount] = $e;
        }

        $this->model->endIteration($this->iterationCount, $iterationOutput);
    }

{'fraction_non_alphanumeric': 0.12284983728498373, 'fraction_numerical': 0.001394700139470014, 'mean_word_length': 1.8885532057737495, 'pattern_counts': {'":': 0, '<': 19, '<?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': '1498563', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2233, 'n_words': 480}