<?php

namespace App\Repository;

use App\Entity\Zone;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class ZoneRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Zone::class);
    }

    public function save(Zone $zone, bool $flush = false): void
    {
        $this->getEntityManager()->persist($zone);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function remove(Zone $zone, bool $flush = false): void
    {
        $this->getEntityManager()->remove($zone);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function findByName(string $name): ?Zone
    {
        return $this->createQueryBuilder('z')
            ->andWhere('z.name = :name')
            ->setParameter('name', $name)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /**
     * @return Zone[]
     */
    public function findAllOrderedByName(): array
    {
        return $this->createQueryBuilder('z')
            ->orderBy('z.name', 'ASC')
            ->getQuery()
            ->getResult();
    }
}