<commit_before><?php

namespace Cascade\Mapper\Map;

use Cascade\Mapper\Field\Reference\ArrayReference;
use Cascade\Mapper\Field\Reference\MutatorReference;
use Cascade\Mapper\Field\Reference\PropertyReference;
use Cascade\Mapper\Field\Reference\ReferenceInterface;
use Cascade\Mapper\Map\Exception\InvalidReferenceTypeException;
use Cascade\Mapper\Mapping\EmbeddedMapping;
use Cascade\Mapper\Mapping\Mapping;
use Cascade\Mapper\Mapping\MappingInterface;
use Cascade\Mapper\Mapping\ResolverMapping;
use Cascade\Mapper\Value\Resolver\ValueResolverInterface;

class Map implements MapInterface
{
    const REF_ARRAY = 'REF_ARRAY';
    const REF_CLASS_PROPERTIES = 'REF_CLASS_PROPERTIES';
    const REF_CLASS_MUTATORS = 'REF_CLASS_MUTATORS';

    /**
     * @var string
     */
    private $from = self::REF_ARRAY;

    /**
     * @var MappingInterface[]
     */
    private $mappings = [];

    /**
     * @var string
     */
    private $to = self::REF_ARRAY;

    public function getMappings()
    {
        return $this->mappings;
    }

    /**
     * @param string $from
     * @return $this
     * @throws InvalidReferenceTypeException
     */
    public function from($from)
    {
        return $this->setReference('from', $from);
    }

    /**
     * @param string $to
     * @return $this
     * @throws InvalidReferenceTypeException
     */
    public function to($to)
    {
        return $this->setReference('to', $to);
    }

    /**
     * @param string $toField
     * @param string $fromField
     * @return $this
     */
    public function add($toField, $fromField)
    {
        $destinationReference = $this->resolveToRef($toField);
        $sourceReference = $this->resolveFromRef($fromField);

        $this->addMapping(new Mapping($destinationReference, $sourceReference));

        return $this;
    }

    public function addEmbedded($fromField, MapInterface $map)
    {
        $this->addMapping(new EmbeddedMapping($this->resolveFromRef($fromField), $map));
        return $this;
    }

    public function addResolver($toField, ValueResolverInterface $resolver)
    {
        $this->addMapping(new ResolverMapping($this->resolveToRef($toField), $resolver));
        return $this;
    }

    /**
     * @param MappingInterface $mapping
     * @return $this
     */
    public function addMapping(MappingInterface $mapping)
    {
        $this->mappings[] = $mapping;
        return $this;
    }

    /**
     * @param string $field
     * @return ReferenceInterface
     */
    protected function resolveFromRef($field)
    {
        return $this->referenceField($this->from, $field);
    }

    /**
     * @param string $field
     * @return ReferenceInterface
     */
    protected function resolveToRef($field)
    {
        return $this->referenceField($this->to, $field);
    }

    /**
     * @param string $refType
     * @return bool
     */
    private function isValidRefType($refType)
    {
        $validRefTypes = [self::REF_ARRAY, self::REF_CLASS_PROPERTIES, self::REF_CLASS_MUTATORS];

        if (in_array($refType, $validRefTypes, true)) {
            return true;
        }

        return false;
    }

    /**
     * @param string $refType
     * @param string $field
     * @return ReferenceInterface
     */
    private function referenceField($refType, $field)
    {
        switch ($refType) {
            case self::REF_CLASS_PROPERTIES:
                return new PropertyReference($field);
            case self::REF_CLASS_MUTATORS:
                return new MutatorReference($field);
            default:
                return new ArrayReference($field);
        }
    }

    /**
     * @param string $target
     * @param string $reference
     * @return $this
     * @throws InvalidReferenceTypeException
     */
    private function setReference($target, $reference)
    {
        if ($this->isValidRefType($reference)) {
            $this->{$target} = $reference;
            return $this;
        }

        throw new InvalidReferenceTypeException(sprintf(
            'Reference type "%s" is not valid',
            $reference
        ));
    }
}<commit_msg>refactor private Map methods to more accurately describe their intended usage.<commit_after><?php

namespace Cascade\Mapper\Map;

use Cascade\Mapper\Field\Reference\ArrayReference;
use Cascade\Mapper\Field\Reference\MutatorReference;
use Cascade\Mapper\Field\Reference\PropertyReference;
use Cascade\Mapper\Field\Reference\ReferenceInterface;
use Cascade\Mapper\Map\Exception\InvalidReferenceTypeException;
use Cascade\Mapper\Mapping\EmbeddedMapping;
use Cascade\Mapper\Mapping\Mapping;
use Cascade\Mapper\Mapping\MappingInterface;
use Cascade\Mapper\Mapping\ResolverMapping;
use Cascade\Mapper\Value\Resolver\ValueResolverInterface;

class Map implements MapInterface
{
    const REF_ARRAY = 'REF_ARRAY';
    const REF_CLASS_PROPERTIES = 'REF_CLASS_PROPERTIES';
    const REF_CLASS_MUTATORS = 'REF_CLASS_MUTATORS';

    /**
     * @var string
     */
    private $from = self::REF_ARRAY;

    /**
     * @var MappingInterface[]
     */
    private $mappings = [];

    /**
     * @var string
     */
    private $to = self::REF_ARRAY;

    public function getMappings()
    {
        return $this->mappings;
    }

    /**
     * @param string $from
     * @return $this
     * @throws InvalidReferenceTypeException
     */
    public function from($from)
    {
        return $this->setReferenceType('from', $from);
    }

    /**
     * @param string $to
     * @return $this
     * @throws InvalidReferenceTypeException
     */
    public function to($to)
    {
        return $this->setReferenceType('to', $to);
    }

    /**
     * @param string $toField
     * @param string $fromField
     * @return $this
     */
    public function add($toField, $fromField)
    {
        $destinationReference = $this->resolveToRef($toField);
        $sourceReference = $this->resolveFromRef($fromField);

        $this->addMapping(new Mapping($destinationReference, $sourceReference));

        return $this;
    }

    public function addEmbedded($fromField, MapInterface $map)
    {
        $this->addMapping(new EmbeddedMapping($this->resolveFromRef($fromField), $map));
        return $this;
    }

    public function addResolver($toField, ValueResolverInterface $resolver)
    {
        $this->addMapping(new ResolverMapping($this->resolveToRef($toField), $resolver));
        return $this;
    }

    /**
     * @param MappingInterface $mapping
     * @return $this
     */
    public function addMapping(MappingInterface $mapping)
    {
        $this->mappings[] = $mapping;
        return $this;
    }

    /**
     * @param string $field
     * @return ReferenceInterface
     */
    protected function resolveFromRef($field)
    {
        return $this->resolveFieldReference($this->from, $field);
    }

    /**
     * @param string $field
     * @return ReferenceInterface
     */
    protected function resolveToRef($field)
    {
        return $this->resolveFieldReference($this->to, $field);
    }

    /**
     * @param string $referenceType
     * @return bool
     */
    private function isValidReferenceType($referenceType)
    {
        $validReferenceTypes = [self::REF_ARRAY, self::REF_CLASS_PROPERTIES, self::REF_CLASS_MUTATORS];

        if (in_array($referenceType, $validReferenceTypes, true)) {
            return true;
        }

        return false;
    }

    /**
     * @param string $refType
     * @param string $field
     * @return ReferenceInterface
     */
    private function resolveFieldReference($refType, $field)
    {
        switch ($refType) {
            case self::REF_CLASS_PROPERTIES:
                return new PropertyReference($field);
            case self::REF_CLASS_MUTATORS:
                return new MutatorReference($field);
            default:
                return new ArrayReference($field);
        }
    }

    /**
     * @param string $target
     * @param string $reference
     * @return $this
     * @throws InvalidReferenceTypeException
     */
    private function setReferenceType($target, $reference)
    {
        if ($this->isValidReferenceType($reference)) {
            $this->{$target} = $reference;
            return $this;
        }

        throw new InvalidReferenceTypeException(sprintf(
            'Reference type "%s" is not valid',
            $reference
        ));
    }
}
{'fraction_non_alphanumeric': 0.11703650680028632, 'fraction_numerical': 0.0, 'mean_word_length': 2.513411567476949, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '2433734', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2342, 'n_words': 480}