2018-12-20 10:25:24 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-12-20 10:25:24 +00:00
|
|
|
namespace App\Controller\Api;
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
use App\Entity\Interfaces\IdentifiableEntityInterface;
|
2019-09-10 16:40:31 +00:00
|
|
|
use App\Exception\ValidationException;
|
2020-02-22 20:15:02 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2020-12-20 01:51:53 +00:00
|
|
|
use App\Paginator;
|
2020-02-22 20:15:02 +00:00
|
|
|
use App\Utilities;
|
2020-06-26 20:22:53 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-02-22 20:15:02 +00:00
|
|
|
use Doctrine\ORM\Query;
|
2019-09-04 18:00:51 +00:00
|
|
|
use InvalidArgumentException;
|
2020-02-22 20:15:02 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2018-12-22 00:01:04 +00:00
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
2018-12-20 10:25:24 +00:00
|
|
|
use Symfony\Component\Serializer\Serializer;
|
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
/**
|
|
|
|
* @template TEntity as object
|
|
|
|
*/
|
2019-04-14 03:19:24 +00:00
|
|
|
abstract class AbstractApiCrudController
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
2021-07-19 05:53:45 +00:00
|
|
|
/** @var class-string<TEntity> The fully-qualified (::class) class name of the entity being managed. */
|
2019-12-07 12:19:49 +00:00
|
|
|
protected string $entityClass;
|
2018-12-20 10:25:24 +00:00
|
|
|
|
|
|
|
/** @var string The route name used to generate the "self" links for each record. */
|
2019-12-07 12:19:49 +00:00
|
|
|
protected string $resourceRouteName;
|
2018-12-20 10:25:24 +00:00
|
|
|
|
2021-04-23 05:24:12 +00:00
|
|
|
public function __construct(
|
|
|
|
protected EntityManagerInterface $em,
|
|
|
|
protected Serializer $serializer,
|
|
|
|
protected ValidatorInterface $validator
|
|
|
|
) {
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
2020-02-22 20:15:02 +00:00
|
|
|
|
2020-02-22 20:27:14 +00:00
|
|
|
protected function listPaginatedFromQuery(
|
2020-02-22 20:15:02 +00:00
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
|
|
|
Query $query,
|
|
|
|
callable $postProcessor = null
|
|
|
|
): ResponseInterface {
|
2020-12-20 01:51:53 +00:00
|
|
|
$paginator = Paginator::fromQuery($query, $request);
|
2020-02-22 20:15:02 +00:00
|
|
|
|
|
|
|
$is_bootgrid = $paginator->isFromBootgrid();
|
|
|
|
$is_internal = ('true' === $request->getParam('internal', 'false'));
|
|
|
|
|
|
|
|
$postProcessor ??= function ($row) use ($is_bootgrid, $is_internal, $request) {
|
2020-02-22 20:27:14 +00:00
|
|
|
$return = $this->viewRecord($row, $request);
|
2020-02-22 20:15:02 +00:00
|
|
|
|
|
|
|
// Older jQuery Bootgrid requests should be "flattened".
|
|
|
|
if ($is_bootgrid && !$is_internal) {
|
2020-12-11 02:43:58 +00:00
|
|
|
return Utilities\Arrays::flattenArray($return, '_');
|
2020-02-22 20:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
};
|
|
|
|
$paginator->setPostprocessor($postProcessor);
|
|
|
|
|
|
|
|
return $paginator->write($response);
|
|
|
|
}
|
|
|
|
|
2020-10-14 22:19:31 +00:00
|
|
|
/**
|
2021-07-19 05:53:45 +00:00
|
|
|
* @param TEntity $record
|
2020-11-10 03:06:48 +00:00
|
|
|
* @param ServerRequest $request
|
|
|
|
*
|
2020-10-14 22:19:31 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
protected function viewRecord(object $record, ServerRequest $request): mixed
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
|
|
|
if (!($record instanceof $this->entityClass)) {
|
2019-09-04 18:00:51 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Record must be an instance of %s.', $this->entityClass));
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 20:27:14 +00:00
|
|
|
$return = $this->toArray($record);
|
2019-01-05 12:02:49 +00:00
|
|
|
|
2020-02-22 20:15:02 +00:00
|
|
|
$isInternal = ('true' === $request->getParam('internal', 'false'));
|
|
|
|
$router = $request->getRouter();
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
if ($record instanceof IdentifiableEntityInterface) {
|
|
|
|
$return['links'] = [
|
|
|
|
'self' => (string)$router->fromHere(
|
|
|
|
$this->resourceRouteName,
|
|
|
|
['id' => $record->getIdRequired()],
|
|
|
|
[],
|
|
|
|
!$isInternal
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
2019-01-05 12:02:49 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:21:24 +00:00
|
|
|
/**
|
2021-07-19 05:53:45 +00:00
|
|
|
* @param TEntity $record
|
|
|
|
* @param array<string, mixed> $context
|
2019-09-20 16:44:38 +00:00
|
|
|
*
|
2021-07-19 05:53:45 +00:00
|
|
|
* @return array<mixed>
|
2019-03-16 09:21:24 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
protected function toArray(object $record, array $context = []): array
|
2019-01-05 12:02:49 +00:00
|
|
|
{
|
2021-07-19 05:53:45 +00:00
|
|
|
return (array)$this->serializer->normalize(
|
2020-12-20 01:51:53 +00:00
|
|
|
$record,
|
|
|
|
null,
|
|
|
|
array_merge(
|
|
|
|
$context,
|
|
|
|
[
|
|
|
|
ObjectNormalizer::ENABLE_MAX_DEPTH => true,
|
|
|
|
ObjectNormalizer::MAX_DEPTH_HANDLER => function (
|
|
|
|
$innerObject,
|
|
|
|
$outerObject,
|
|
|
|
string $attributeName,
|
|
|
|
string $format = null,
|
|
|
|
array $context = []
|
|
|
|
) {
|
|
|
|
return $this->displayShortenedObject($innerObject);
|
|
|
|
},
|
|
|
|
ObjectNormalizer::CIRCULAR_REFERENCE_HANDLER => function (
|
|
|
|
$object,
|
|
|
|
string $format = null,
|
|
|
|
array $context = []
|
|
|
|
) {
|
|
|
|
return $this->displayShortenedObject($object);
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2018-12-22 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-31 05:25:55 +00:00
|
|
|
* @param object $object
|
2019-09-20 16:44:38 +00:00
|
|
|
*
|
2018-12-22 00:01:04 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
protected function displayShortenedObject(object $object): mixed
|
2018-12-22 00:01:04 +00:00
|
|
|
{
|
|
|
|
if (method_exists($object, 'getName')) {
|
|
|
|
return $object->getName();
|
|
|
|
}
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
if ($object instanceof IdentifiableEntityInterface) {
|
|
|
|
return $object->getIdRequired();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($object instanceof \Stringable) {
|
|
|
|
return (string)$object;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_class($object) . ': ' . spl_object_hash($object);
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-19 05:53:45 +00:00
|
|
|
* @param array<mixed>|null $data
|
|
|
|
* @param TEntity|null $record
|
|
|
|
* @param array<string, mixed> $context
|
|
|
|
*
|
|
|
|
* @return TEntity
|
2018-12-20 10:25:24 +00:00
|
|
|
*/
|
2021-07-19 05:53:45 +00:00
|
|
|
protected function editRecord(?array $data, ?object $record = null, array $context = []): object
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
2019-03-16 09:21:24 +00:00
|
|
|
if (null === $data) {
|
2019-09-04 18:00:51 +00:00
|
|
|
throw new InvalidArgumentException('Could not parse input data.');
|
2019-03-16 09:21:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 20:27:14 +00:00
|
|
|
$record = $this->fromArray($data, $record, $context);
|
2018-12-20 10:25:24 +00:00
|
|
|
|
|
|
|
$errors = $this->validator->validate($record);
|
|
|
|
if (count($errors) > 0) {
|
2019-09-10 16:40:31 +00:00
|
|
|
$e = new ValidationException((string)$errors);
|
2019-01-09 18:57:23 +00:00
|
|
|
$e->setDetailedErrors($errors);
|
|
|
|
throw $e;
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->em->persist($record);
|
2020-06-25 18:44:26 +00:00
|
|
|
$this->em->flush();
|
2018-12-20 10:25:24 +00:00
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
2019-01-04 17:32:11 +00:00
|
|
|
/**
|
2021-07-19 05:53:45 +00:00
|
|
|
* @param array<mixed> $data
|
|
|
|
* @param TEntity|null $record
|
|
|
|
* @param array<string, mixed> $context
|
|
|
|
*
|
|
|
|
* @return TEntity
|
2019-01-04 17:32:11 +00:00
|
|
|
*/
|
2021-07-19 05:53:45 +00:00
|
|
|
protected function fromArray(array $data, ?object $record = null, array $context = []): object
|
2019-01-04 17:32:11 +00:00
|
|
|
{
|
2019-02-17 06:58:48 +00:00
|
|
|
if (null !== $record) {
|
|
|
|
$context[ObjectNormalizer::OBJECT_TO_POPULATE] = $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->serializer->denormalize($data, $this->entityClass, null, $context);
|
2019-01-04 17:32:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 10:25:24 +00:00
|
|
|
/**
|
2021-07-19 05:53:45 +00:00
|
|
|
* @param TEntity $record
|
2018-12-20 10:25:24 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
protected function deleteRecord(object $record): void
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
|
|
|
if (!($record instanceof $this->entityClass)) {
|
2019-09-04 18:00:51 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Record must be an instance of %s.', $this->entityClass));
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->em->remove($record);
|
2020-06-25 18:44:26 +00:00
|
|
|
$this->em->flush();
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
}
|