Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 15, 2023
1 parent 5865edd commit e84527b
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 56 deletions.
3 changes: 0 additions & 3 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Traits\ResolvesFields;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentRelation;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class BelongsToMany extends Relation
{
use ResolvesFields;

/**
* The pivot fields resolver callback.
*/
Expand Down
26 changes: 24 additions & 2 deletions src/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ public function sortable(): static
return $this->filter->isSortable();
}

/**
* Filter the relation fields.
*/
public function relation(): static
{
return $this->filter(static function (Field $field): bool {
return $field instanceof Relation;
});
}

/**
* Filter the subresource fields.
*/
public function subResource(bool $value = true): static
{
return $this->filter(static function (Field $field) use ($value): bool {
return $value
? $field instanceof Relation && $field->isSubResource()
: ! $field instanceof Relation || ! $field->isSubResource();
});
}

/**
* Map the fields to validate.
*/
Expand All @@ -82,15 +104,15 @@ public function mapToValidate(Request $request, Model $model): array
*/
public function mapToDisplay(Request $request, Model $model): array
{
return $this->map->toDisplay($request, $model)->all();
return $this->map->toDisplay($request, $model)->filter()->values()->all();
}

/**
* Map the fields to form inputs.
*/
public function mapToInputs(Request $request, Model $model): array
{
return $this->map->toInput($request, $model)->all();
return $this->map->toInput($request, $model)->filter()->values()->all();
}

/**
Expand Down
27 changes: 0 additions & 27 deletions src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Cone\Root\Http\Controllers\MediaController;
use Cone\Root\Models\Medium;
use Cone\Root\Traits\HasMedia;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesFilters;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
Expand All @@ -24,11 +22,6 @@

class Media extends File
{
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
}
use ResolvesFilters;

/**
* Indicates if the component is multiple.
*/
Expand All @@ -44,14 +37,6 @@ class Media extends File
*/
protected ?Closure $filtersResolver = null;

/**
* Get the URI key.
*/
public function getUriKey(): string
{
return str_replace('.', '-', $this->getRequestKey());
}

/**
* Get the route parameter name.
*/
Expand Down Expand Up @@ -218,18 +203,6 @@ public function buildUri(Request $request, Model $model): ?string
return rtrim($uri, '?');
}

/**
* Register the routes using the given router.
*/
public function registerRoutes(Request $request, Router $router): void
{
$this->__registerRoutes($request, $router);

$router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void {
$this->resolveFields($request)->registerRoutes($request, $router);
});
}

/**
* The routes that should be registered.
*/
Expand Down
106 changes: 106 additions & 0 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Filters\RenderableFilter;
use Cone\Root\Root;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesActions;
use Cone\Root\Traits\ResolvesFields;
use Cone\Root\Traits\ResolvesFilters;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

abstract class Relation extends Field
{
use ResolvesActions;
use ResolvesFilters;
use ResolvesFields;
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
}

/**
* The relation name on the model.
*/
Expand Down Expand Up @@ -53,6 +66,11 @@ abstract class Relation extends Field
*/
protected string|Closure|null $groupResolver = null;

/**
* Indicates wheter the relation is a sub resource.

Check warning on line 70 in src/Fields/Relation.php

View workflow job for this annotation

GitHub Actions / 文A Typos check

"wheter" should be "whether".
*/
protected bool $asSubResource = false;

/**
* The query scopes.
*/
Expand Down Expand Up @@ -106,6 +124,32 @@ public function getRelationName(): string
: $this->relation;
}

/**
* Get the URI key.
*/
public function getUriKey(): string
{
return str_replace('.', '-', $this->getRequestKey());
}

/**
* Set the as subresource attribute.
*/
public function asSubResource(bool $value = true): static
{
$this->asSubResource = $value;

return $this;
}

/**
* Determine if the relation is a subresource.
*/
public function isSubResource(): bool
{
return $this->asSubResource;
}

/**
* Set the nullable attribute.
*/
Expand Down Expand Up @@ -299,6 +343,44 @@ public function newOption(Model $related, string $label): Option
return new Option($related->getKey(), $label);
}

/**
* Get the per page options.
*/
public function getPerPageOptions(): array
{
return [5, 10, 15, 25];
}

/**
* Paginate the results.
*/
public function paginate(Request $request, Model $model): array
{
return [
//
];
}

/**
* Register the routes using the given router.
*/
public function registerRoutes(Request $request, Router $router): void
{
$this->__registerRoutes($request, $router);

$router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void {
$this->resolveFields($request)->registerRoutes($request, $router);
});
}

/**
* Register the routes.
*/
public function routes(Router $router): void
{
//
}

/**
* Get the option representation of the model and the related model.
*/
Expand All @@ -321,4 +403,28 @@ public function toInput(Request $request, Model $model): array
'options' => $this->resolveOptions($request, $model),
]);
}

/**
* Get the index representation of the relation.
*/
public function toIndex(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'title' => $this->getRelatedName(),
'actions' => $this->resolveActions($request)
->authorized($request, $model)
->visible('index')
->mapToForms($request, $model),
'data' => $this->paginate($request, $model),
'perPageOptions' => $this->getPerPageOptions(),
'filters' => $this->resolveFilters($request)
->authorized($request)
->renderable()
->map(static function (RenderableFilter $filter) use ($request, $model): array {
return $filter->toField()->toInput($request, $model);
})
->all(),
'activeFilters' => $this->resolveFilters($request)->active($request)->count(),
]);
}
}
19 changes: 0 additions & 19 deletions src/Interfaces/Table.php

This file was deleted.

14 changes: 12 additions & 2 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Cone\Root\Actions\Action;
use Cone\Root\Fields\Field;
use Cone\Root\Fields\Relation;
use Cone\Root\Filters\RenderableFilter;
use Cone\Root\Interfaces\Form;
use Cone\Root\Interfaces\Table;
use Cone\Root\Root;
use Cone\Root\Traits\AsForm;
use Cone\Root\Traits\Authorizable;
Expand All @@ -26,7 +26,7 @@
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;

abstract class Resource implements Arrayable, Form, Table
abstract class Resource implements Arrayable, Form
{
use AsForm;
use Authorizable;
Expand Down Expand Up @@ -275,6 +275,7 @@ public function paginate(Request $request): LengthAwarePaginator
'url' => $this->modelUrl($model),
'model' => $model,
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $model)
->visible('index')
->mapToDisplay($request, $model),
Expand Down Expand Up @@ -365,6 +366,7 @@ public function toCreate(Request $request): array
'action' => $this->getUri(),
'method' => 'POST',
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $model)
->visible('create')
->mapToInputs($request, $model),
Expand All @@ -382,6 +384,7 @@ public function toShow(Request $request, Model $model): array
'action' => $this->modelUrl($model),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $model)
->visible('show')
->mapToDisplay($request, $model),
Expand All @@ -393,6 +396,12 @@ public function toShow(Request $request, Model $model): array
->authorized($request, $model)
->visible('show')
->toArray(),
'relations' => $this->resolveFields($request)
->subResource()
->authorized($request, $model)
->map(static function (Relation $relation) use ($request, $model): array {
return $relation->toIndex($request, $model);
}),
]);
}

Expand All @@ -407,6 +416,7 @@ public function toEdit(Request $request, Model $model): array
'action' => $this->modelUrl($model),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $model)
->visible('update')
->mapToInputs($request, $model),
Expand Down
Loading

0 comments on commit e84527b

Please sign in to comment.