<commit_before><?php

namespace FOQ\ElasticaBundle\Mapper;

use FOQ\ElasticaBundle\MapperInterface;
use Doctrine\Common\Persistence\ObjectRepository;

/**
 * Maps Elastica documents with Doctrine objects
 * This mapper assumes an exact match between
 * elastica documents ids and doctrine object ids
 */
class DoctrineMapper implements MapperInterface
{
    /**
     * Repository to fetch the objects from
     *
     * @var ObjectRepository
     */
    protected $objectRepository = null;

    /**
     * Doctrine identifier field
     *
     * @var string
     */
    protected $identifier = null;

    /**
     * Instanciates a new Mapper
     *
     * @param ObjectRepository objectRepository
     * @param string $identifier
     */
    public function __construct(ObjectRepository $objectRepository, $identifier = 'id')
    {
        $this->objectRepository = $objectRepository;
        $this->identifier       = $identifier;
    }

    /**
     * Transforms an array of elastica objects into an array of
     * model objects fetched from the doctrine repository
     *
     * @return array
     **/
    public function fromElasticaObjects(array $elasticaObjects)
    {
        $ids = array_map(function($elasticaObject) {
            return $elasticaObject->getId();
        }, $elasticaObjects);

        return $this->objectRepository
            ->createQueryBuilder()
            ->field($this->identifier)->in($ids)<commit_msg>Add 'hydrate' option to the doctrine mapper<commit_after><?php

namespace FOQ\ElasticaBundle\Mapper;

use FOQ\ElasticaBundle\MapperInterface;
use Doctrine\Common\Persistence\ObjectRepository;

/**
 * Maps Elastica documents with Doctrine objects
 * This mapper assumes an exact match between
 * elastica documents ids and doctrine object ids
 */
class DoctrineMapper implements MapperInterface
{
    /**
     * Repository to fetch the objects from
     *
     * @var ObjectRepository
     */
    protected $objectRepository = null;

    /**
     * Doctrine identifier field
     *
     * @var string
     */
    protected $identifier = null;

    /**
     * Optional parameters
     *
     * @var array
     */
    protected $options = array(
        'hydrate' => true
    );

    /**
     * Instanciates a new Mapper
     *
     * @param ObjectRepository objectRepository
     * @param string $identifier
     * @param array $options
     */
    public function __construct(ObjectRepository $objectRepository, $identifier = 'id', array $options = array())
    {
        $this->objectRepository = $objectRepository;
        $this->identifier       = $identifier;
        $this->options          = array_merge($this->options, $options);
    }

    /**
     * Transforms an array of elastica objects into an array of
     * model objects fetched from the doctrine repository
     *
     * @return array
     **/
    public function fromElasticaObjects(array $elasticaObjects)
    {
        $ids = array_map(function($elasticaObject) {
            return $elasticaObject->getId();
        }, $elasticaObjects);

        return $this->objectRepository
            ->createQueryBuilder()
            ->field($this->identifier)->in($ids)
            ->hydrate($this->options['hydrate'])
{'fraction_non_alphanumeric': 0.0990990990990991, 'fraction_numerical': 0.0, 'mean_word_length': 2.5698447893569845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '6926833', 'n_tokens_mistral': 861, 'n_tokens_neox': 834, 'n_words': 228}