<?php


namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;

/**
 * @ORM\Entity
 * @ORM\Table(
 *     name="years",
 *     indexes={
 *         @ORM\Index(name="y_year_idx", columns={"year"})
 *     }
 * )
 */
class Year implements JsonSerializable
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @var int
     */
    private $id;

    /**
     * @ORM\Column(name="year", type="integer", nullable=false)
     * @var int
     */
    private $year;

    /**
     * @ORM\ManyToMany(targetEntity="Judge", inversedBy="years")
     * @ORM\JoinTable(
     *  name="years_judges",
     *  joinColumns={
     *      @ORM\JoinColumn(name="year_id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="judge_id", referencedColumnName="id")
     *  }
     * )
     * @var \Doctrine\Common\Collections\Collection|Judge[]
     */
    private $judges;

    public function __construct()
    {
        $this->judges = new ArrayCollection();
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     * @return Year
     */
    public function setId(int $id): Year
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getYear(): int
    {
        return $this->year;
    }

    /**
     * @param int $year
     * @return Year
     */
    public function setYear(int $year): Year
    {
        $this->year = $year;
        return $this;
    }

    /**
     * @return Judge[]|Collection
     */
    public function getJudges(): ?Collection
    {
        return $this->judges;
    }

    /**
     * @param Judge $judge
     * @return Year
     */
    public function addJudge(Judge $judge): Year
    {
        if ($this->judges->contains($judge)) {
            return $this;
        }
        $this->judges->add($judge);
        $judge->addYear($this);
        return $this;
    }

    /**
     * @param Judge $judge
     * @return Year
     */
    public function removeJudge(Judge $judge): Year
    {
        if (!$this->judges->contains($judge)) {
            return $this;
        }
        $this->judges->removeElement($judge);
        $judge->removeYear($this);
        return $this;
    }

    /**
     * @param Judge[]|Collection $judges
     * @return Year
     */
    public function setJudges(?Collection $judges): Year
    {
        $this->judges = $judges;
        return $this;
    }

    /**
     * @return array
     */
    public function jsonSerialize()
    {
        return [
            'id' => $this->getId(),
            'year' => $this->getYear(),
            'judges' => $this->getJudges()->getValues(),
        ];
    }
}
