<?php

namespace App\Service;

use App\Entity\JudgeTitle;
use Doctrine\ORM\EntityManager;

class YearManager
{
    /** @var EntityManager */
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function getYears()
    {
        $qb = $this->entityManager->createQueryBuilder();
        $years = $qb->select('jt.year')
            ->from(JudgeTitle::class, 'jt')
            ->orderBy('jt.year', 'DESC')
            ->distinct()
            ->getQuery()
            ->getArrayResult();

        $filteredYears = array_map(function($year) {
            return $year['year'];
        }, $years);

        return $filteredYears;
    }
}
