Skip to content

Commit

Permalink
Showing 12 changed files with 121 additions and 200 deletions.
30 changes: 3 additions & 27 deletions src/Actions/Actions.php
Original file line number Diff line number Diff line change
@@ -5,32 +5,16 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;

class Actions
class Actions extends Collection
{
use ForwardsCalls;

/**
* The actions collection.
*/
protected Collection $actions;

/**
* Create a new actions instance.
*/
public function __construct(array $actions = [])
{
$this->actions = new Collection($actions);
}

/**
* Register the given actions.
*/
public function register(array|Action $actions): static
{
foreach (Arr::wrap($actions) as $action) {
$this->actions->push($action);
$this->push($action);
}

return $this;
@@ -41,14 +25,6 @@ public function register(array|Action $actions): static
*/
public function mapToTableComponents(Request $request): array
{
return $this->actions->map->toTableComponent($request)->all();
}

/**
* Handle the dynamic method call.
*/
public function __call($method, $parameters): mixed
{
return $this->forwardCallTo($this->actions, $method, $parameters);
return $this->map->toTableComponent($request)->all();
}
}
36 changes: 6 additions & 30 deletions src/Columns/Columns.php
Original file line number Diff line number Diff line change
@@ -6,32 +6,16 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;

class Columns
class Columns extends Collection
{
use ForwardsCalls;

/**
* The columns collection.
*/
protected Collection $columns;

/**
* Create a new table instance.
*/
public function __construct(array $columns = [])
{
$this->columns = new Collection($columns);
}

/**
* Register the given columns.
*/
public function register(array|Column $columns): static
{
foreach (Arr::wrap($columns) as $column) {
$this->columns->push($column);
$this->push($column);
}

return $this;
@@ -42,38 +26,30 @@ public function register(array|Column $columns): static
*/
public function searchable(): Collection
{
return $this->columns->filter->isSearchable();
return $this->filter->isSearchable();
}

/**
* Filter the sortable columns.
*/
public function sortable(): Collection
{
return $this->columns->filter->isSortable();
return $this->filter->isSortable();
}

/**
* Map the columns to cells for the given model.
*/
public function mapToHeads(Request $request): array
{
return $this->columns->map->toHead($request)->all();
return $this->map->toHead($request)->all();
}

/**
* Map the columns to cells for the given model.
*/
public function mapToCells(Request $request, Model $model): array
{
return $this->columns->map->toCell($request, $model)->all();
}

/**
* Handle the dynamic method call.
*/
public function __call($method, $parameters): mixed
{
return $this->forwardCallTo($this->columns, $method, $parameters);
return $this->map->toCell($request, $model)->all();
}
}
8 changes: 8 additions & 0 deletions src/Columns/Relation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cone\Root\Columns;

class Relation extends Column
{
//
}
34 changes: 5 additions & 29 deletions src/Fields/Fields.php
Original file line number Diff line number Diff line change
@@ -6,32 +6,16 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;

class Fields
class Fields extends Collection
{
use ForwardsCalls;

/**
* The fields collection.
*/
protected Collection $fields;

/**
* Create a new fields instance.
*/
public function __construct(array $fields = [])
{
$this->fields = new Collection($fields);
}

/**
* Register the given fields.
*/
public function register(array|Field $fields): static
{
foreach (Arr::wrap($fields) as $field) {
$this->fields->push($field);
$this->push($field);
}

return $this;
@@ -42,7 +26,7 @@ public function register(array|Field $fields): static
*/
public function persist(Request $request, Model $model): void
{
$this->fields->each(static function (Field $field) use ($request, $model): void {
$this->each(static function (Field $field) use ($request, $model): void {
$field->persist(
$request, $model, $field->getValueForHydrate($request)
);
@@ -54,7 +38,7 @@ public function persist(Request $request, Model $model): void
*/
public function mapToValidate(Request $request, Model $model): array
{
return $this->fields->reduce(static function (array $rules, Field $field) use ($request, $model): array {
return $this->reduce(static function (array $rules, Field $field) use ($request, $model): array {
return array_merge_recursive($rules, $field->toValidate($request, $model));
}, []);
}
@@ -64,14 +48,6 @@ public function mapToValidate(Request $request, Model $model): array
*/
public function mapToFormComponents(Request $request, Model $model): array
{
return $this->fields->map->toFormComponent($request, $model)->all();
}

/**
* Handle the dynamic method call.
*/
public function __call($method, $parameters): mixed
{
return $this->forwardCallTo($this->fields, $method, $parameters);
return $this->map->toFormComponent($request, $model)->all();
}
}
19 changes: 0 additions & 19 deletions src/Filters/Filter.php
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@

namespace Cone\Root\Filters;

use Cone\Root\Fields\Field;
use Cone\Root\Traits\Authorizable;
use Cone\Root\Traits\Makeable;
use Illuminate\Database\Eloquent\Builder;
@@ -14,21 +13,11 @@ abstract class Filter
use Authorizable;
use Makeable;

/**
* Indicates whether the filter is functional.
*/
protected bool $functional = false;

/**
* Apply the filter on the query.
*/
abstract public function apply(Request $request, Builder $query, mixed $value): Builder;

/**
* Convert the filter to a form field.
*/
abstract public function toField(): Field;

/**
* Get the key.
*/
@@ -68,12 +57,4 @@ public function isActive(Request $request): bool
{
return $request->filled($this->getRequestKey());
}

/**
* Determine whether the filter is functional.
*/
public function isFunctional(): bool
{
return $this->functional;
}
}
54 changes: 16 additions & 38 deletions src/Filters/Filters.php
Original file line number Diff line number Diff line change
@@ -6,32 +6,16 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;

class Filters
class Filters extends Collection
{
use ForwardsCalls;

/**
* The filters collection.
*/
protected Collection $filters;

/**
* Create a new filters instance.
*/
public function __construct(array $filters = [])
{
$this->filters = new Collection($filters);
}

/**
* Register the given filters.
*/
public function register(array|Filter $filters): static
{
foreach (Arr::wrap($filters) as $filter) {
$this->filters->push($filter);
$this->push($filter);
}

return $this;
@@ -42,7 +26,7 @@ public function register(array|Filter $filters): static
*/
public function apply(Request $request, Builder $query): Builder
{
$this->filters->filter(static function (Filter $filter) use ($request): bool {
$this->filter(static function (Filter $filter) use ($request): bool {
return $request->has($filter->getRequestKey());
})->each(static function (Filter $filter) use ($query, $request): void {
$filter->apply($request, $query, $filter->getValue($request));
@@ -54,44 +38,38 @@ public function apply(Request $request, Builder $query): Builder
/**
* Filter the renderable filters.
*/
public function renderable(): Collection
public function renderable(): static
{
return $this->filters->reject->isFunctional();
return $this->filter(static function (Filter $filter): bool {
return $filter instanceof RenderableFilter;
});
}

/**
* Filter the functional filters.
*/
public function functional(): Collection
public function functional(): static
{
return $this->filters->filter->isFunctional();
return $this->reject(static function (Filter $filter): bool {
return $filter instanceof RenderableFilter;
});
}

/**
* Filter the active filters.
*/
public function active(Request $request): Collection
public function active(Request $request): static
{
return $this->filters->filter->isActive($request);
return $this->filter->isActive($request);
}

/**
* Map the filters to an array.
*/
public function mapToData(Request $request): array
{
return $this->filters
->mapWithKeys(static function (Filter $filter) use ($request): array {
return [$filter->getKey() => $filter->getValue($request)];
})
->toArray();
}

/**
* Handle the dynamic method call.
*/
public function __call($method, $parameters): mixed
{
return $this->forwardCallTo($this->filters, $method, $parameters);
return $this->mapWithKeys(static function (Filter $filter) use ($request): array {
return [$filter->getKey() => $filter->getValue($request)];
})->toArray();
}
}
13 changes: 13 additions & 0 deletions src/Filters/RenderableFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Cone\Root\Filters;

use Cone\Root\Fields\Field;

abstract class RenderableFilter extends Filter
{
/**
* Convert the filter to a form field.
*/
abstract public function toField(): Field;
}
Loading

0 comments on commit fd15910

Please sign in to comment.