4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-18 06:57:05 +00:00
AzuraCast/src/Doctrine/Repository.php

165 lines
4.4 KiB
PHP
Raw Normal View History

<?php
2021-07-19 05:53:45 +00:00
declare(strict_types=1);
namespace App\Doctrine;
use App\Environment;
use Azura\Normalizer\DoctrineEntityNormalizer;
use Closure;
use Doctrine\Persistence\ObjectRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Serializer;
2021-07-19 05:53:45 +00:00
/**
* @template TEntity as object
*/
class Repository
{
2021-07-19 05:53:45 +00:00
/** @var class-string<TEntity> */
2020-07-08 07:03:50 +00:00
protected string $entityClass;
2021-07-19 05:53:45 +00:00
/** @var ObjectRepository<TEntity> */
protected ObjectRepository $repository;
public function __construct(
2021-04-23 05:24:12 +00:00
protected ReloadableEntityManagerInterface $em,
protected Serializer $serializer,
protected Environment $environment,
protected LoggerInterface $logger
) {
2020-07-08 07:03:50 +00:00
if (!isset($this->entityClass)) {
2021-07-19 05:53:45 +00:00
/** @var class-string<TEntity> $defaultClass */
$defaultClass = str_replace(['Repository', '\\\\'], ['', '\\'], static::class);
$this->entityClass = $defaultClass;
}
2021-07-19 05:53:45 +00:00
2020-07-08 07:03:50 +00:00
if (!isset($this->repository)) {
$this->repository = $em->getRepository($this->entityClass);
}
}
public function getRepository(): ObjectRepository
{
return $this->repository;
}
public function getEntityManager(): ReloadableEntityManagerInterface
{
return $this->em;
}
/**
* @param int|string $id
* @return TEntity|null
*/
public function find(int|string $id): ?object
{
return $this->em->find($this->entityClass, $id);
}
/**
* Generate an array result of all records.
*
* @param bool $cached
* @param string|null $order_by
* @param string $order_dir
*
* @return mixed[]
*/
2021-07-19 05:53:45 +00:00
public function fetchArray(bool $cached = true, ?string $order_by = null, string $order_dir = 'ASC'): array
{
$qb = $this->em->createQueryBuilder()
->select('e')
->from($this->entityClass, 'e');
if ($order_by) {
$qb->orderBy('e.' . str_replace('e.', '', $order_by), $order_dir);
}
return $qb->getQuery()->getArrayResult();
}
/**
* Generic dropdown builder function (can be overridden for specialized use cases).
*
* @param bool|string $add_blank
* @param Closure|NULL $display
* @param string $pk
* @param string $order_by
*
* @return mixed[]
*/
2021-07-19 05:53:45 +00:00
public function fetchSelect(
bool|string $add_blank = false,
Closure $display = null,
string $pk = 'id',
string $order_by = 'name'
): array {
$select = [];
// Specify custom text in the $add_blank parameter to override.
if ($add_blank !== false) {
$select[''] = ($add_blank === true) ? __('Select...') : $add_blank;
}
// Build query for records.
$qb = $this->em->createQueryBuilder()->from($this->entityClass, 'e');
if ($display === null) {
$qb->select('e.' . $pk)->addSelect('e.name')->orderBy('e.' . $order_by, 'ASC');
} else {
$qb->select('e')->orderBy('e.' . $order_by, 'ASC');
}
$results = $qb->getQuery()->getArrayResult();
// Assemble select values and, if necessary, call $display callback.
foreach ((array)$results as $result) {
$key = $result[$pk];
2021-06-08 06:40:49 +00:00
$select[$key] = ($display === null) ? $result['name'] : $display($result);
}
return $select;
}
/**
* FromArray (A Doctrine 1 Classic)
*
* @param object $entity
* @param array $source
*/
public function fromArray(object $entity, array $source): object
{
return $this->serializer->denormalize(
$source,
get_class($entity),
null,
[
AbstractNormalizer::OBJECT_TO_POPULATE => $entity,
]
);
}
/**
* ToArray (A Doctrine 1 Classic)
*
* @param object $entity
* @param bool $deep Iterate through collections associated with this item.
* @param bool $form_mode Return values in a format suitable for ZendForm setDefault function.
*
* @return mixed[]
*/
2021-07-19 05:53:45 +00:00
public function toArray(object $entity, bool $deep = false, bool $form_mode = false): array
{
2021-07-19 05:53:45 +00:00
return (array)$this->serializer->normalize(
$entity,
null,
[
DoctrineEntityNormalizer::NORMALIZE_TO_IDENTIFIERS => $form_mode,
]
);
}
}