<commit_before><?php

namespace UniMapper;

use UniMapper\EntityCollection,
    UniMapper\Exceptions\MapperException,
    UniMapper\Utils\Property,
    UniMapper\Utils\Validator,
    UniMapper\Reflection\EntityReflection;

/**
 * Mapper is ancestor for every new mapper. It defines common methods or
 * parameters used in its descendants.  Mappers are generally used to
 * communicate between repository and data source.
 */
abstract class Mapper implements Mapper\IMapper
{

    protected $name;

    public function __construct($name)
    {
        $this->name = (string) $name;
    }

    public function getName()
    {
        return $this->name;
    }

    final public function getResource(EntityReflection $entityReflection)
    {
        $mappers = $entityReflection->getMappers();
        if (!isset($mappers[$this->name])) {
            throw new MapperException("Entity does not define mapper with name " . $this->name . "!");
        }
        return $mappers[$this->name]->getResource();
    }

    protected function mapProperties(array $properties)
    {
        $output = array();
        foreach ($properties as $property) {

            if ($property->getMapping()) {

                $mapDefinition = $property->getMapping()->getName($this->name);
                if ($mapDefinition) {
                    $output[] = $mapDefinition;
                    continue;
                }
            }
            $output[] = $property->getName();
        }
        return $output;
    }

    /**
     * Convert entity to simple array
     *
     *  @param \UniMapper\Entity $entity Entity
     *
     *  @return array
     */
    public function entityToData(\UniMapper\Entity $entity)
    {
        $properties = $entity->getReflection()->getProperties();

        $output = array();
        foreach ($entity->toArray() as $name => $value) {

            // Property mapping definition required
            $mapping = $properties[$name]->getMapping();
            if ($mapping === false) {
                continue;
            }
            $mappedPropertyName = $mapping->getName($this->name);

            if ($value instanceof EntityCollection) {
                // Collection of entities
                foreach ($value as $subEntity) {
                    $output[$mappedPropertyName][] = $this->entityToData($subEntity);
                }
            } else {
                $output[$mappedPropertyName] = $value;
            }<commit_msg>mappers: fixed wrong mapping in entityToData()<commit_after><?php

namespace UniMapper;

use UniMapper\EntityCollection,
    UniMapper\Exceptions\MapperException,
    UniMapper\Utils\Property,
    UniMapper\Utils\Validator,
    UniMapper\Reflection\EntityReflection;

/**
 * Mapper is ancestor for every new mapper. It defines common methods or
 * parameters used in its descendants.  Mappers are generally used to
 * communicate between repository and data source.
 */
abstract class Mapper implements Mapper\IMapper
{

    protected $name;

    public function __construct($name)
    {
        $this->name = (string) $name;
    }

    public function getName()
    {
        return $this->name;
    }

    final public function getResource(EntityReflection $entityReflection)
    {
        $mappers = $entityReflection->getMappers();
        if (!isset($mappers[$this->name])) {
            throw new MapperException("Entity does not define mapper with name " . $this->name . "!");
        }
        return $mappers[$this->name]->getResource();
    }

    protected function mapProperties(array $properties)
    {
        $output = array();
        foreach ($properties as $property) {

            if ($property->getMapping()) {

                $mapDefinition = $property->getMapping()->getName($this->name);
                if ($mapDefinition) {
                    $output[] = $mapDefinition;
                    continue;
                }
            }
            $output[] = $property->getName();
        }
        return $output;
    }

    /**
     * Convert entity to simple array
     *
     *  @param \UniMapper\Entity $entity Entity
     *
     *  @return array
     */
    public function entityToData(\UniMapper\Entity $entity)
    {
        $properties = $entity->getReflection()->getProperties($this->name);

        $output = array();
        foreach ($entity->toArray() as $name => $value) {

            if (!isset($properties[$name])) {
                continue;
            }

            // Property mapping definition required
            $mapping = $properties[$name]->getMapping();
            if ($mapping === false) {
                continue;
            }
            $mappedPropertyName = $mapping->getName($this->name);

            if ($value instanceof EntityCollection) {
                // Collection of entities
                foreach ($value as $subEntity) {
                    $output[$mappedPropertyName][] = $this->entityToData($subEntity);
                }
            } else {
                $output[$mappedPropertyName] = $value;
            }
{'fraction_non_alphanumeric': 0.11785643662250346, 'fraction_numerical': 0.0, 'mean_word_length': 1.9805539186800236, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, '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': '2431745', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1251, 'n_words': 310}