<commit_before><?php

namespace FOQ\ElasticaBundle\Transformer;

use FOQ\ElasticaBundle\HybridResult;

/**
 * Holds a collection of transformers for an index wide transformation.
 *
 * @author <NAME> <<EMAIL>>
 */
class ElasticaToModelTransformerCollection implements ElasticaToModelTransformerInterface
{
    protected $transformers = array();
    protected $options = array(
        'identifier' => 'id'
    );

    public function __construct(array $transformers, array $options)
    {
        $this->transformers = $transformers;
        $this->options = array_merge($this->options, $options);
    }

    public function getObjectClass()
    {
        return array_map(function ($transformer) {
            return $transformer->getObjectClass();
        }, $this->transformers);
    }

    public function transform(array $elasticaObjects)
    {
        $sorted = array();
        foreach ($elasticaObjects as $object) {
            $sorted[$object->getType()][] = $object;
        }

        $identifierGetter = 'get' . ucfirst($this->options['identifier']);

        $transformed = array();
        foreach ($sorted AS $type => $objects) {
            $transformed[$type] = $this->transformers[$type]->transform($objects);
            $transformed[$type] = array_combine(array_map(function($o) use ($identifierGetter) {return $o->$identifierGetter();},$transformed[$type]),$transformed[$type]);
        }

        $result = array();
        foreach ($elasticaObjects as $object) {
            $result[] = $transformed[$object->getType()][$object->getId()];
        }

        return $result;
    }

    public function hybridTransform(array $elasticaObjects)
    {
        $objects = $this->transform($elasticaObjects);

        $result = array();
        for ($i = 0; $i < count($elasticaObjects); $i++) {
            $result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
        }

        return $result;
    }

    protected function getTypeToClassMap()
    {
        return array_map(function ($transformer) {
            return $transformer->getObjectClass();
        }, $this->transformers);
    }<commit_msg>Minor improvement, using an intermediate variable to hold the transformed objects<commit_after><?php

namespace FOQ\ElasticaBundle\Transformer;

use FOQ\ElasticaBundle\HybridResult;

/**
 * Holds a collection of transformers for an index wide transformation.
 *
 * @author <NAME> <<EMAIL>>
 */
class ElasticaToModelTransformerCollection implements ElasticaToModelTransformerInterface
{
    protected $transformers = array();
    protected $options = array(
        'identifier' => 'id'
    );

    public function __construct(array $transformers, array $options)
    {
        $this->transformers = $transformers;
        $this->options = array_merge($this->options, $options);
    }

    public function getObjectClass()
    {
        return array_map(function ($transformer) {
            return $transformer->getObjectClass();
        }, $this->transformers);
    }

    public function transform(array $elasticaObjects)
    {
        $sorted = array();
        foreach ($elasticaObjects as $object) {
            $sorted[$object->getType()][] = $object;
        }

        $identifierGetter = 'get' . ucfirst($this->options['identifier']);

        $transformed = array();
        foreach ($sorted AS $type => $objects) {
            $transformedObjects = $this->transformers[$type]->transform($objects);
            $transformed[$type] = array_combine(array_map(function($o) use ($identifierGetter) {return $o->$identifierGetter();},$transformedObjects),$transformedObjects);
        }

        $result = array();
        foreach ($elasticaObjects as $object) {
            $result[] = $transformed[$object->getType()][$object->getId()];
        }

        return $result;
    }

    public function hybridTransform(array $elasticaObjects)
    {
        $objects = $this->transform($elasticaObjects);

        $result = array();
        for ($i = 0; $i < count($elasticaObjects); $i++) {
            $result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
        }

        return $result;
    }

    protected function getTypeToClassMap()
    {
        return array_map(function ($transformer) {
            return $transformer->getObjectClass();
        }, $this->transformers);
    }
{'fraction_non_alphanumeric': 0.14068265682656828, 'fraction_numerical': 0.00046125461254612545, 'mean_word_length': 2.917795844625113, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 43, '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': '5102298', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1166, 'n_words': 248}