<?php
namespace App\Repository;

use App\Entity\ActorEntity;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

/**
 * Repository for workspaces
 */
class ActorRepository extends EntityRepository
{

    /** @var array Default sorting */
    public const DEFAULT_ORDER = [
        'followed' => 'desc',
        'active' => 'desc',
        'firstName' => 'asc',
        'lastName' => 'asc'
    ];

    /**
     * {@inheritdoc}
     */
    public function findAll($onlyActive = false): array
    {
        $critera = [];
        if ($onlyActive) {
            $critera['active'] = True;
        }
        return $this->findBy($critera);
    }
    
    /**
     * {@inheritdoc}
     */
    public function findBy(array $criteria, ?array $orderBy = self::DEFAULT_ORDER, $limit = null, $offset = null)
    {
        return parent::findBy($criteria, $orderBy, $limit, $offset);
    }

    /**
     * {@inheritdoc}
     */
    public function createQueryBuilder($alias, $indexBy = null): QueryBuilder
    {
        $queryBuilder = parent::createQueryBuilder($alias, $indexBy);
        foreach ($this::DEFAULT_ORDER as $field => $order) {
            $queryBuilder->addOrderBy($alias . '.' . $field, $order);
        }

        return $queryBuilder;
    }

    /**
     * {@inheritdoc}
     */
    public function find($id, $lockMode = null, $lockVersion = null): ?ActorEntity
    {
        return parent::find($id, $lockMode = null, $lockVersion = null);
    }
}