<commit_before><?php

namespace JaztecBase\Mapper;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;

use JaztecBase\Entity\AbstractEntity as Entity;

/**
 * Doctrine Mapper
 *
 * Provides common doctrine methods
 */
abstract class AbstractDoctrineMapper extends AbstractMapper
{
    const TYPE_SERIALIZEDARRAY = 0x1;
    const TYPE_ENTITYARRAY = 0x2;

    /**
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * @var string
     */
    protected $entityName;

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEntityManager()
    {
        return $this->entityManager;
    }

    /**
     * @param \Doctrine\ORM\EntityManager $entityManager
     */
    public function setEntityManager(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * @return \Doctrine\DBAL\Connection
     */
    public function getDatabase()
    {
        return $this->entityManager->getConnection();
    }

    /**
     * @return \Doctrine\Common\Persistence\ObjectRepository
     */
    public function getRepository()
    {
        return $this->entityManager->getRepository($this->entityName);
    }

    /**
     * @param  array|\Doctrine\Common\Persistence\ObjectRepository $repo
     * @param  int                                                 $type
     * @return array
     */
    protected function processResult($repo, $type)
    {
        // Testing on input parameters
        if (!$repo instanceof \Doctrine\Common\Persistence\ObjectRepository &&
            !is_array($repo)) {
            throw new Exception(__CLASS__ . ' expects an array or a \Doctrine\Common\Persistence\ObjectRepository. ' . class_name($repo) . ' given.');
        }
        switch ($type) {
            case AbstractDoctrineMapper::TYPE_SERIALIZEDARRAY:
                $result = array();
                foreach ($repo as $obj) {
                    /* @var $obj \JaztecBase\Entity\EntityInterface */
                    if($obj instanceof \JaztecBase\Entity\EntityInterface)
                        $result[] = $obj->toArray();
                    elseif (is_array($obj))
                        $result[] = $obj;
                }
                break;
            case AbstractDoctrineMapper::TYPE_ENTITYARRAY:
                if(!is_array($repo))
                    $repo = array($repo);
                $result = $repo;
                break;
        }

        return $result;
    }

    /**
     * Persists and saves an entity to the database.
     *
     * @param \JaztecBase\Entity\AbstractEntity $entity
     */
    public function save(Entity $entity)
    {
        $this->getEntityManager()->persist($entity);
        $this->getEntityManager()->flush();
    }
}<commit_msg>Shortcut function to remove entities from the database.<commit_after><?php

namespace JaztecBase\Mapper;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;

use JaztecBase\Entity\AbstractEntity as Entity;

/**
 * Doctrine Mapper
 *
 * Provides common doctrine methods
 */
abstract class AbstractDoctrineMapper extends AbstractMapper
{
    const TYPE_SERIALIZEDARRAY = 0x1;
    const TYPE_ENTITYARRAY = 0x2;

    /**
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * @var string
     */
    protected $entityName;

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEntityManager()
    {
        return $this->entityManager;
    }

    /**
     * @param \Doctrine\ORM\EntityManager $entityManager
     */
    public function setEntityManager(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * @return \Doctrine\DBAL\Connection
     */
    public function getDatabase()
    {
        return $this->entityManager->getConnection();
    }

    /**
     * @return \Doctrine\Common\Persistence\ObjectRepository
     */
    public function getRepository()
    {
        return $this->entityManager->getRepository($this->entityName);
    }

    /**
     * @param  array|\Doctrine\Common\Persistence\ObjectRepository $repo
     * @param  int                                                 $type
     * @return array
     */
    protected function processResult($repo, $type)
    {
        // Testing on input parameters
        if (!$repo instanceof \Doctrine\Common\Persistence\ObjectRepository &&
            !is_array($repo)) {
            throw new Exception(__CLASS__ . ' expects an array or a \Doctrine\Common\Persistence\ObjectRepository. ' . class_name($repo) . ' given.');
        }
        switch ($type) {
            case AbstractDoctrineMapper::TYPE_SERIALIZEDARRAY:
                $result = array();
                foreach ($repo as $obj) {
                    /* @var $obj \JaztecBase\Entity\EntityInterface */
                    if($obj instanceof \JaztecBase\Entity\EntityInterface)
                        $result[] = $obj->toArray();
                    elseif (is_array($obj))
                        $result[] = $obj;
                }
                break;
            case AbstractDoctrineMapper::TYPE_ENTITYARRAY:
                if(!is_array($repo))
                    $repo = array($repo);
                $result = $repo;
                break;
        }

        return $result;
    }

    /**
     * Persists and saves an entity to the database.
     *
     * @param \JaztecBase\Entity\AbstractEntity $entity
     */
    public function save(Entity $entity)
    {
        $this->getEntityManager()->persist($entity);
        $this->getEntityManager()->flush();
    }

    /**
     * Removes an entity from the database.
     * 
     * @param \JaztecBase\Entity\AbstractEntity $entity
     */
    public function remove(Entity $entity)
    {
        $this->getEntityManager()->remove($entity);
        $this->getEntityManager()->flush();
    }
}
{'fraction_non_alphanumeric': 0.11457269322437862, 'fraction_numerical': 0.001361933946203609, 'mean_word_length': 1.886977886977887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2552144', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1554, 'n_words': 318}