Skip to content

Commit

Permalink
Function documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jdw5 committed Apr 25, 2024
1 parent 1eb566a commit 938190f
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 28 deletions.
31 changes: 30 additions & 1 deletion src/Concerns/HasActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use Jdw5\Vanguard\Table\Actions\InlineAction;
use Illuminate\Support\Collection;

/**
* Define a class as having actions.
*/
trait HasActions
{
protected mixed $actions = null;

/**
* Define the actions for the table.
* Define the actions for the class.
*
* @return array
*/
Expand All @@ -22,27 +25,53 @@ protected function defineActions(): array
return [];
}

/**
* Retrieve the actions for the class.
*
* @return Collection
*/
public function getActions(): Collection
{
return $this->actions ??= collect($this->defineActions())
->filter(static fn (BaseAction $action): bool => !$action->isExcluded());
}

/**
* Retrieve the inline actions for the class.
*
* @return Collection
*/
public function getInlineActions(): Collection
{
return $this->getActions()->filter(static fn (BaseAction $action): bool => $action instanceof InlineAction)->values();
}

/**
* Retrieve the bulk actions for the class.
*
* @return Collection
*/
public function getBulkActions(): Collection
{
return $this->getActions()->filter(static fn (BaseAction $action): bool => $action instanceof BulkAction)->values();
}

/**
* Retrieve the page actions for the class.
*
* @return Collection
*/
public function getPageActions(): Collection
{
return $this->getActions()->filter(static fn (BaseAction $action): bool => $action instanceof PageAction)->values();
}

/**
* Retrieve the default action for the class.
*
* @return BaseAction if a default is defined
* @return null if no default is defined
*/
public function getDefaultAction(): ?BaseAction
{
return $this->getActions()->first(static fn (BaseAction $action): bool => $action instanceof InlineAction && $action->isDefault());
Expand Down
14 changes: 11 additions & 3 deletions src/Concerns/HasLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Jdw5\Vanguard\Concerns;

/**
* Set a label for a class.
*/
trait HasLabel
{
protected mixed $label;

/**
* Chainable method for setting the label.
* Set the label, chainable.
*
* @param mixed $label
* @return static
Expand All @@ -19,12 +22,12 @@ public function label(mixed $label): static
}

/**
* Set the label.
* Set the label quietly.
*
* @param mixed $label
* @return void
*/
public function setLabel(mixed $label): void
protected function setLabel(mixed $label): void
{
$this->label = $label;
}
Expand All @@ -38,4 +41,9 @@ public function getLabel(): mixed
{
return $this->evaluate($this->label);
}

protected function labelise(string $name): string
{
return str($name)->headline()->lower()->ucfirst();
}
}
21 changes: 21 additions & 0 deletions src/Concerns/HasMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@

namespace Jdw5\Vanguard\Concerns;

/**
* Set metadata properties on a class
*/
trait HasMetadata
{
/** Metadata are non-uniform properties for the class */
protected array|\Closure $metadata = [];

/**
* Set the metadata, chainable.
*
* @param array|\Closure $metadata
* @return static
*/
public function metadata(array|\Closure $metadata): static
{
$this->setMetadata($metadata);
return $this;
}

/**
* Set the metadata quietly.
*
* @param array|\Closure $metadata
* @return void
*/
protected function setMetadata(array|\Closure $metadata): void
{
$this->metadata = $metadata;
}

/**
* Get the metadata.
*
* @return array
*/
public function getMetadata(): array
{
return $this->evaluate($this->metadata);
Expand Down
17 changes: 17 additions & 0 deletions src/Concerns/HasName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,34 @@ trait HasName
{
protected string|\Closure $name;

/**
* Set the name, chainable.
*
* @param string|\Closure $name
* @return static
*/
public function name(string|\Closure $name): static
{
$this->setName($name);
return $this;
}

/**
* Set the name quietly.
*
* @param string|\Closure $name
* @return void
*/
protected function setName(string|\Closure $name): void
{
$this->name = $name;
}

/**
* Get the name
*
* @return string
*/
public function getName(): string
{
return $this->evaluate($this->name);
Expand Down
25 changes: 24 additions & 1 deletion src/Concerns/HasRefinements.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,40 @@
use Jdw5\Vanguard\Refining\Sorts\BaseSort;
use Jdw5\Vanguard\Refining\Filters\BaseFilter;

/**
* Define a class as having a list of refinement options.
*/
trait HasRefinements
{
/** Caching the refinements */
private mixed $refinements = null;

/**
* Define the refinements for the class.
*
* @return array
*/
protected function defineRefinements(): array
{
return [];
}

/**
* Retrieve the refinements for the class.
*
* @return Collection
*/
protected function getRefinements(): Collection
{
return $this->refinements ??= collect($this->defineRefinements())
->filter(static fn (Refinement $refinement): bool => !$refinement->isExcluded());
}

/**
* Retrieve an associative array of filters keyed to their name.
*
* @return array
*/
protected function getFilters(): array
{
return $this->getRefinements()->mapWithKeys(function (Refinement $refinement) {
Expand All @@ -32,7 +51,11 @@ protected function getFilters(): array
})->toArray();
}


/**
* Retrieve an associative array of sorts keyed to their name.
*
* @return array
*/
protected function getSorts(): array
{
return $this->getRefinements()->mapWithKeys(function (Refinement $refinement) {
Expand Down
14 changes: 12 additions & 2 deletions src/Table/Actions/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public function defineActions(): array
}

/**
* Serialize the actions.
* Retrieve the actions as an array.
*
* @return array
*/
public function jsonSerialize(): array
public function toArray(): array
{
return [
'inline' => $this->getInlineActions(),
Expand All @@ -59,4 +59,14 @@ public function jsonSerialize(): array
'default' => $this->getDefaultAction(),
];
}

/**
* Serialize the actions.
*
* @return array
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}
17 changes: 12 additions & 5 deletions src/Table/Actions/BaseAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Jdw5\Vanguard\Concerns\HasLabel;
use Jdw5\Vanguard\Concerns\IsIncludable;
use Jdw5\Vanguard\Concerns\HasMetadata;
use Jdw5\Vanguard\Concerns\SetsLabel;
use Jdw5\Vanguard\Table\Actions\Concerns\DependsOn;
use Jdw5\Vanguard\Table\Actions\Concerns\HasEndpoint;

abstract class BaseAction extends Primitive
Expand All @@ -18,12 +16,11 @@ abstract class BaseAction extends Primitive
use HasName;
use IsIncludable;
use HasEndpoint;
use SetsLabel;

final public function __construct(string $name)
{
$this->name($name);
$this->label($this->nameToLabel($name));
$this->setName($name);
$this->setLabel($this->labelise($name));
$this->setUp();
}

Expand All @@ -32,6 +29,11 @@ public static function make(string $name): static
return resolve(static::class, compact('name'));
}

/**
* Retrieve the action as an array.
*
* @return array
*/
public function toArray(): array
{
return [
Expand All @@ -41,6 +43,11 @@ public function toArray(): array
];
}

/**
* Serialize the action to JSON.
*
* @return array
*/
public function jsonSerialize(): array
{
return $this->toArray();
Expand Down
2 changes: 1 addition & 1 deletion src/Table/Actions/Concerns/HasMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getMethod(): string
protected function setMethod(string|\Closure $method): void
{
if (\is_string($method) && ! \in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
throw InvalidEndpointMethod::with($method);
throw InvalidEndpointMethod::make($method);
}
$this->method = $method;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Table/Actions/Exceptions/InvalidEndpointMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class InvalidEndpointMethod extends \Exception
{
public static function with(string $method): self
public static function make(string $method): self
{
return new static("The method {$method} is not a valid endpoint method");
}
Expand Down
Loading

0 comments on commit 938190f

Please sign in to comment.