2020-02-06 02:35:13 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-02-06 02:35:13 +00:00
|
|
|
namespace App\Doctrine;
|
|
|
|
|
2020-12-03 04:18:06 +00:00
|
|
|
use App\Environment;
|
2021-11-07 08:20:52 +00:00
|
|
|
use Azura\Normalizer\DoctrineEntityNormalizer;
|
2020-02-06 02:35:13 +00:00
|
|
|
use Closure;
|
2020-08-31 22:32:30 +00:00
|
|
|
use Doctrine\Persistence\ObjectRepository;
|
2020-02-06 02:35:13 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2021-11-07 08:20:52 +00:00
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
2020-02-06 02:35:13 +00:00
|
|
|
use Symfony\Component\Serializer\Serializer;
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
/**
|
|
|
|
* @template TEntity as object
|
|
|
|
*/
|
2020-02-06 02:35:13 +00:00
|
|
|
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;
|
2020-02-06 02:35:13 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
/** @var ObjectRepository<TEntity> */
|
2020-08-31 22:32:30 +00:00
|
|
|
protected ObjectRepository $repository;
|
2020-02-06 02:35:13 +00:00
|
|
|
|
2020-06-26 20:22:53 +00:00
|
|
|
public function __construct(
|
2021-04-23 05:24:12 +00:00
|
|
|
protected ReloadableEntityManagerInterface $em,
|
|
|
|
protected Serializer $serializer,
|
|
|
|
protected Environment $environment,
|
|
|
|
protected LoggerInterface $logger
|
2020-06-26 20:22:53 +00:00
|
|
|
) {
|
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;
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
2021-07-19 05:53:45 +00:00
|
|
|
|
2020-07-08 07:03:50 +00:00
|
|
|
if (!isset($this->repository)) {
|
2020-02-06 02:35:13 +00:00
|
|
|
$this->repository = $em->getRepository($this->entityClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 22:32:30 +00:00
|
|
|
public function getRepository(): ObjectRepository
|
2020-02-06 02:35:13 +00:00
|
|
|
{
|
|
|
|
return $this->repository;
|
|
|
|
}
|
|
|
|
|
2022-05-30 06:25:35 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-06 02:35:13 +00:00
|
|
|
/**
|
|
|
|
* Generate an array result of all records.
|
|
|
|
*
|
|
|
|
* @param bool $cached
|
2020-08-31 22:32:30 +00:00
|
|
|
* @param string|null $order_by
|
2020-02-06 02:35:13 +00:00
|
|
|
* @param string $order_dir
|
|
|
|
*
|
2020-10-14 22:19:31 +00:00
|
|
|
* @return mixed[]
|
2020-02-06 02:35:13 +00:00
|
|
|
*/
|
2021-07-19 05:53:45 +00:00
|
|
|
public function fetchArray(bool $cached = true, ?string $order_by = null, string $order_dir = 'ASC'): array
|
2020-02-06 02:35:13 +00:00
|
|
|
{
|
|
|
|
$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).
|
|
|
|
*
|
2020-08-31 22:32:30 +00:00
|
|
|
* @param bool|string $add_blank
|
2020-02-06 02:35:13 +00:00
|
|
|
* @param Closure|NULL $display
|
|
|
|
* @param string $pk
|
|
|
|
* @param string $order_by
|
|
|
|
*
|
2020-10-14 22:19:31 +00:00
|
|
|
* @return mixed[]
|
2020-02-06 02:35:13 +00:00
|
|
|
*/
|
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 {
|
2020-02-06 02:35:13 +00:00
|
|
|
$select = [];
|
|
|
|
|
|
|
|
// Specify custom text in the $add_blank parameter to override.
|
|
|
|
if ($add_blank !== false) {
|
2020-11-10 03:06:48 +00:00
|
|
|
$select[''] = ($add_blank === true) ? __('Select...') : $add_blank;
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $select;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FromArray (A Doctrine 1 Classic)
|
|
|
|
*
|
|
|
|
* @param object $entity
|
|
|
|
* @param array $source
|
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
public function fromArray(object $entity, array $source): object
|
2020-02-06 02:35:13 +00:00
|
|
|
{
|
2020-12-22 07:10:03 +00:00
|
|
|
return $this->serializer->denormalize(
|
|
|
|
$source,
|
|
|
|
get_class($entity),
|
|
|
|
null,
|
|
|
|
[
|
2021-11-07 08:20:52 +00:00
|
|
|
AbstractNormalizer::OBJECT_TO_POPULATE => $entity,
|
2020-12-22 07:10:03 +00:00
|
|
|
]
|
|
|
|
);
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2020-10-14 22:19:31 +00:00
|
|
|
* @return mixed[]
|
2020-02-06 02:35:13 +00:00
|
|
|
*/
|
2021-07-19 05:53:45 +00:00
|
|
|
public function toArray(object $entity, bool $deep = false, bool $form_mode = false): array
|
2020-02-06 02:35:13 +00:00
|
|
|
{
|
2021-07-19 05:53:45 +00:00
|
|
|
return (array)$this->serializer->normalize(
|
2020-12-22 07:10:03 +00:00
|
|
|
$entity,
|
|
|
|
null,
|
|
|
|
[
|
|
|
|
DoctrineEntityNormalizer::NORMALIZE_TO_IDENTIFIERS => $form_mode,
|
|
|
|
]
|
|
|
|
);
|
2020-02-06 02:35:13 +00:00
|
|
|
}
|
2020-10-14 22:19:31 +00:00
|
|
|
}
|