<?php

namespace Vipa\JournalBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Query;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;

/**
 * IssueRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SubjectRepository extends NestedTreeRepository
{
    /**
     * @param $publisher
     * @return ArrayCollection|Subject[]
     */
    public function getByPublisher($publisher)
    {
        $qb = $this->createQueryBuilder('s');
        $qb
            ->join('s.journals', 'j', 'WITH')
            ->join('j.publisher', 'i', 'WITH', 'i.slug=:publisher')
            ->setParameter('publisher', $publisher);

        return $qb->setCacheable(true)->getQuery()->getResult();
    }

    /**
     * @param  array     $data
     * @return ArrayCollection|Subject[]
     */
    public function getByIds(array $data)
    {
        $qb = $this->createQueryBuilder('j');
        $qb->where(
            $qb->expr()->in('j.id', ':data')
        )
            ->setParameter('data', $data);

        return $qb->setCacheable(true)->getQuery()->getResult();
    }

    /**
     * @return array
     */
    public function getAllSubjects()
    {
        $result = $this->createQueryBuilder('subject')
            ->select('subject.subject')->setCacheable(true)->getQuery()
            ->getResult(Query::HYDRATE_ARRAY);

        return $result;
    }
}
