chore: move additions/changes to package

This commit is contained in:
Sami Mazouz 2024-02-17 11:31:26 +01:00
parent bc5bac0ac4
commit dc71b82e3e
No known key found for this signature in database
9 changed files with 2 additions and 144 deletions

View File

@ -1,42 +0,0 @@
<?php
namespace Flarum\Api\Endpoint\Concerns;
use Closure;
use Flarum\Api\Context;
trait HasHooks
{
protected ?Closure $before = null;
protected ?Closure $after = null;
public function before(Closure $callback): static
{
$this->before = $callback;
return $this;
}
public function after(Closure $callback): static
{
$this->after = $callback;
return $this;
}
protected function callBeforeHook(Context $context): void
{
if ($this->before) {
($this->before)($context);
}
}
protected function callAfterHook(Context $context, mixed $data): mixed
{
if ($this->after) {
return ($this->after)($context, $data);
}
return $data;
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace Flarum\Api\Endpoint\Concerns;
use Tobyz\JsonApiServer\Context;
trait SavesData
{
private function mutateDataBeforeValidation(Context $context, array $data, bool $validateAll): array
{
if (method_exists($context->resource, 'mutateDataBeforeValidation')) {
return $context->resource->mutateDataBeforeValidation($context, $data, $validateAll);
}
return $data;
}
}

View File

@ -5,30 +5,20 @@ namespace Flarum\Api\Endpoint;
use Flarum\Api\Endpoint\Concerns\HasAuthorization;
use Flarum\Api\Endpoint\Concerns\HasCustomRoute;
use Flarum\Api\Endpoint\Concerns\HasEagerLoading;
use Flarum\Api\Endpoint\Concerns\HasHooks;
use Flarum\Api\Endpoint\Concerns\SavesData;
use Illuminate\Database\Eloquent\Collection;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Endpoint\Concerns\SavesData as BaseSavesData;
use Tobyz\JsonApiServer\Endpoint\Concerns\ShowsResources;
use Tobyz\JsonApiServer\Endpoint\Create as BaseCreate;
use Tobyz\JsonApiServer\Exception\ForbiddenException;
use Tobyz\JsonApiServer\Resource\Creatable;
use function Tobyz\JsonApiServer\has_value;
use function Tobyz\JsonApiServer\json_api_response;
use function Tobyz\JsonApiServer\set_value;
class Create extends BaseCreate implements Endpoint
{
use BaseSavesData;
use ShowsResources;
use SavesData;
use HasAuthorization;
use HasEagerLoading;
use HasCustomRoute;
use HasHooks;
public function handle(Context $context): ?ResponseInterface
{
@ -64,7 +54,6 @@ class Create extends BaseCreate implements Endpoint
$this->assertFieldsValid($context, $data);
$this->fillDefaultValues($context, $data);
$this->deserializeValues($context, $data);
$this->mutateDataBeforeValidation($context, $data, true);
$this->assertDataValid($context, $data);
$this->setValues($context, $data);
@ -80,15 +69,6 @@ class Create extends BaseCreate implements Endpoint
return $model;
}
private function fillDefaultValues(Context $context, array &$data): void
{
foreach ($context->fields($context->resource) as $field) {
if (!has_value($data, $field) && ($default = $field->default)) {
set_value($data, $field, $default($context->withField($field)));
}
}
}
public function route(): EndpointRoute
{
return new EndpointRoute(

View File

@ -8,7 +8,6 @@ use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Endpoint\Concerns\FindsResources;
use Tobyz\JsonApiServer\Endpoint\Delete as BaseDelete;
use Tobyz\JsonApiServer\Exception\ForbiddenException;
use Tobyz\JsonApiServer\Resource\Deletable;
@ -17,7 +16,6 @@ use function Tobyz\JsonApiServer\json_api_response;
class Delete extends BaseDelete implements Endpoint
{
use HasAuthorization;
use FindsResources;
use HasCustomRoute;
/** {@inheritdoc} */

View File

@ -2,12 +2,10 @@
namespace Flarum\Api\Endpoint;
use Closure;
use Flarum\Api\Endpoint\Concerns\ExtractsListingParams;
use Flarum\Api\Endpoint\Concerns\HasAuthorization;
use Flarum\Api\Endpoint\Concerns\HasCustomRoute;
use Flarum\Api\Endpoint\Concerns\HasEagerLoading;
use Flarum\Api\Endpoint\Concerns\HasHooks;
use Flarum\Http\RequestUtil;
use Flarum\Search\SearchCriteria;
use Flarum\Search\SearchManager;
@ -16,27 +14,20 @@ use Illuminate\Database\Eloquent\Collection;
use Psr\Http\Message\ResponseInterface as Response;
use RuntimeException;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Endpoint\Concerns\IncludesData;
use Tobyz\JsonApiServer\Endpoint\Index as BaseIndex;
use Tobyz\JsonApiServer\Exception\BadRequestException;
use Tobyz\JsonApiServer\Exception\ForbiddenException;
use Tobyz\JsonApiServer\Exception\Sourceable;
use Tobyz\JsonApiServer\Pagination\OffsetPagination;
use Tobyz\JsonApiServer\Resource\Countable;
use Tobyz\JsonApiServer\Resource\Listable;
use Tobyz\JsonApiServer\Serializer;
use function Tobyz\JsonApiServer\apply_filters;
use function Tobyz\JsonApiServer\json_api_response;
use function Tobyz\JsonApiServer\parse_sort_string;
class Index extends BaseIndex implements Endpoint
{
use HasAuthorization;
use IncludesData;
use HasEagerLoading;
use HasCustomRoute;
use ExtractsListingParams;
use HasHooks;
public function paginate(int $defaultLimit = 20, int $maxLimit = 50): static
{
@ -151,47 +142,6 @@ class Index extends BaseIndex implements Endpoint
return json_api_response(compact('data', 'included', 'meta', 'links'));
}
private function applySorts($query, Context $context): void
{
if (!($sortString = $context->queryParam('sort', $this->defaultSort))) {
return;
}
$sorts = $context->collection->sorts();
foreach (parse_sort_string($sortString) as [$name, $direction]) {
foreach ($sorts as $field) {
if ($field->name === $name && $field->isVisible($context)) {
$field->apply($query, $direction, $context);
continue 2;
}
}
throw (new BadRequestException("Invalid sort: $name"))->setSource([
'parameter' => 'sort',
]);
}
}
private function applyFilters($query, Context $context): void
{
if (!($filters = $context->queryParam('filter'))) {
return;
}
if (!is_array($filters)) {
throw (new BadRequestException('filter must be an array'))->setSource([
'parameter' => 'filter',
]);
}
try {
apply_filters($query, $filters, $context->collection, $context);
} catch (Sourceable $e) {
throw $e->prependSource(['parameter' => 'filter']);
}
}
public function route(): EndpointRoute
{
return new EndpointRoute(

View File

@ -6,7 +6,6 @@ use Flarum\Api\Endpoint\Concerns\ExtractsListingParams;
use Flarum\Api\Endpoint\Concerns\HasAuthorization;
use Flarum\Api\Endpoint\Concerns\HasCustomRoute;
use Flarum\Api\Endpoint\Concerns\HasEagerLoading;
use Flarum\Api\Endpoint\Concerns\HasHooks;
use Illuminate\Database\Eloquent\Collection;
use Psr\Http\Message\ResponseInterface;
use Tobyz\JsonApiServer\Context;
@ -24,7 +23,6 @@ class Show extends BaseShow implements Endpoint
use HasEagerLoading;
use HasCustomRoute;
use ExtractsListingParams;
use HasHooks;
public function handle(Context $context): ?ResponseInterface
{

View File

@ -5,14 +5,10 @@ namespace Flarum\Api\Endpoint;
use Flarum\Api\Endpoint\Concerns\HasAuthorization;
use Flarum\Api\Endpoint\Concerns\HasCustomRoute;
use Flarum\Api\Endpoint\Concerns\HasEagerLoading;
use Flarum\Api\Endpoint\Concerns\HasHooks;
use Flarum\Api\Endpoint\Concerns\SavesData;
use Illuminate\Database\Eloquent\Collection;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Endpoint\Concerns\SavesData as BaseSavesData;
use Tobyz\JsonApiServer\Endpoint\Concerns\ShowsResources;
use Tobyz\JsonApiServer\Endpoint\Update as BaseUpdate;
use Tobyz\JsonApiServer\Exception\ForbiddenException;
use Tobyz\JsonApiServer\Resource\Updatable;
@ -20,13 +16,9 @@ use function Tobyz\JsonApiServer\json_api_response;
class Update extends BaseUpdate implements Endpoint
{
use BaseSavesData;
use ShowsResources;
use SavesData;
use HasAuthorization;
use HasEagerLoading;
use HasCustomRoute;
use HasHooks;
public function handle(Context $context): ?ResponseInterface
{
@ -67,7 +59,6 @@ class Update extends BaseUpdate implements Endpoint
$this->assertFieldsValid($context, $data);
$this->deserializeValues($context, $data);
$this->mutateDataBeforeValidation($context, $data, false);
$this->assertDataValid($context, $data);
$this->setValues($context, $data);

View File

@ -130,7 +130,7 @@ abstract class AbstractDatabaseResource extends BaseResource implements
return null;
}
public function mutateDataBeforeValidation(Context $context, array $data, bool $validateAll): array
public function mutateDataBeforeValidation(Context $context, array $data): array
{
$dirty = $context->model->getDirty();

View File

@ -6,5 +6,5 @@ use Tobyz\JsonApiServer\Resource\Resource as BaseResource;
interface Resource extends BaseResource
{
//
}