<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * County
 *
 * @ORM\Table(name="county", indexes={@ORM\Index(name="state_id", columns={"state_id"}), @ORM\Index(name="country_id", columns={"country_id"})})
 * @Gedmo\TranslationEntity(class="Entity\CountyTranslations")
 * @ORM\Entity
 */
class County
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="smallint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="name", type="string", length=20, nullable=null)
     */
    protected $name;

    /**
     * @var \State
     *
     * @ORM\ManyToOne(targetEntity="State", inversedBy="county", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="state_id", referencedColumnName="id", onDelete="CASCADE")
     * })
     */
    protected $state;

    /**
     * @ORM\ManyToOne(targetEntity="Country", cascade={"persist"})
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $country;

    /**
     * @ORM\OneToMany(
     *   targetEntity="CountyTranslations",
     *   mappedBy="object",
     *   cascade={"persist", "remove"}
     * )
     */
    protected $translations;

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

    /**
     * Set name
     *
     * @param string $name
     * @return County
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set state
     *
     * @param State $state
     * @return County
     */
    public function setState(State $state = null)
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get state
     *
     * @return State
     */
    public function getState()
    {
        return $this->state;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->translations = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set country
     *
     * @param Country $country
     * @return County
     */
    public function setCountry(Country $country = null)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return Country
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Add translations
     *
     * @param CountyTranslations $translations
     * @return County
     */
    public function addTranslation(CountyTranslations $translations)
    {
        if (!$this->translations->contains($translations)) {
            $this->translations[] = $translations;
            $translations->setObject($this);
        }

        return $this;
    }

    /**
     * Remove translations
     *
     * @param CountyTranslations $translations
     */
    public function removeTranslation(CountyTranslations $translations)
    {
        $this->translations->removeElement($translations);
    }

    /**
     * Get translations
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTranslations()
    {
        return $this->translations;
    }

    protected $transArr = array();

    /**
     * method to verify and manage locales
     *
     * @return multitype:NULL
     */
    public function getTranslationsArr()
    {
        if (!$this->transArr || empty($this->transArr)) {
            foreach ($this->translations as $tras) {
                $this->transArr[$tras->getLocale()] = $tras->getContent();
            }
        }

        return $this->transArr;
    }

    /**
     * get tranlated name by given locale
     *
     * @param string $locale
     *
     * @return string
     */
    public function getTransName($locale)
    {
        $arrTrans = $this->getTranslationsArr();

        return isset($arrTrans[$locale]) ? $arrTrans[$locale] : '';
    }

    public function __toString()
    {
        return $this->getName();
    }
}
