src/Rest/Traits/Methods/FindMethod.php
<?php
declare(strict_types = 1);
/**
* /src/Rest/Traits/Methods/FindMethod.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
namespace App\Rest\Traits\Methods;
use App\Rest\RequestHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
/**
* @package App\Rest\Traits\Methods
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
trait FindMethod
{
/**
* Generic 'findMethod' method for REST resources.
*
* @param array<int, string>|null $allowedHttpMethods
*
* @throws Throwable
*/
public function findMethod(Request $request, ?array $allowedHttpMethods = null): Response
{
$resource = $this->getResourceForMethod($request, $allowedHttpMethods ?? [Request::METHOD_GET]);
// Determine used parameters
$orderBy = RequestHandler::getOrderBy($request);
$limit = RequestHandler::getLimit($request);
$offset = RequestHandler::getOffset($request);
$search = RequestHandler::getSearchTerms($request);
try {
$criteria = RequestHandler::getCriteria($request);
$this->processCriteria($criteria, $request, __METHOD__);
return $this
->getResponseHandler()
->createResponse($request, $resource->find($criteria, $orderBy, $limit, $offset, $search), $resource);
} catch (Throwable $exception) {
throw $this->handleRestMethodException($exception);
}
}
}