Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Builder to closure #246

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Dashboard/ResourcePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MoonShine\Dashboard;

use Closure;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Support\Collection;
use MoonShine\Resources\Resource;
Expand All @@ -13,10 +14,13 @@ final class ResourcePreview extends DashboardItem
{
protected static string $view = 'moonshine::blocks.resource_preview';

/**
* @deprecated Builder $query, use Closure $query
*/
public function __construct(
protected Resource $resource,
string $label = '',
protected ?Builder $query = null,
protected Builder|Closure|null $query = null,
) {
$this->setLabel($label);
}
Expand All @@ -34,13 +38,20 @@ public function items(): Collection
{
$collections = $this->resource()
->resolveQuery()
->when($this->query, fn () => $this->query)
->when($this->getQuery(), fn () => $this->getQuery())
->get();

return $this->resource()
->transformToResources($collections);
}

protected function getQuery(): ?Builder
{
return is_callable($this->query)
? call_user_func($this->query)
: $this->query;
}

public function id(string $index = null): string
{
return str($this->resource()->routeNameAlias())
Expand Down
10 changes: 7 additions & 3 deletions src/Metrics/DonutChartMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MoonShine\Metrics;

use Closure;

class DonutChartMetric extends Metric
{
protected static string $view = 'moonshine::metrics.donut-chart';
Expand All @@ -16,11 +18,13 @@ class DonutChartMetric extends Metric
];

/**
* @param $values array<string, int|float>
* @param $values array<string, int|float>|Closure
*/
public function values(array $values): self
public function values(array|Closure $values): self
{
$this->values = $values;
$this->values = is_callable($values)
? $values()
: $values;

return $this;
}
Expand Down
10 changes: 7 additions & 3 deletions src/Metrics/LineChartMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MoonShine\Metrics;

use Closure;

class LineChartMetric extends Metric
{
protected static string $view = 'moonshine::metrics.line-chart';
Expand All @@ -22,11 +24,13 @@ public function lines(): array
return $this->lines;
}

public function line(array $line, string|array $color = '#7843E9'): static
public function line(array|Closure $line, string|array|Closure $color = '#7843E9'): static
{
$this->lines[] = $line;
$this->lines[] = is_callable($line) ? $line() : $line;

$color = is_callable($color) ? $color() : $color;

if(is_string($color)) {
if (is_string($color)) {
$this->colors[] = $color;
} else {
$this->colors = $color;
Expand Down
14 changes: 8 additions & 6 deletions src/Metrics/ValueMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MoonShine\Metrics;

use Closure;

class ValueMetric extends Metric
{
protected static string $view = 'moonshine::metrics.value';
Expand All @@ -16,9 +18,9 @@ class ValueMetric extends Metric

public int|float $target = 0;

public function valueFormat(string $value): static
public function valueFormat(string|Closure $value): static
{
$this->valueFormat = $value;
$this->valueFormat = is_callable($value) ? $value() : $value;

return $this;
}
Expand All @@ -37,17 +39,17 @@ public function simpleValue(): string|float
return str_replace('{value}', (string) $this->value, $this->valueFormat);
}

public function value(int|float $value): static
public function value(int|float|Closure $value): static
{
$this->value = $value;
$this->value = is_callable($value) ? $value() : $value;

return $this;
}

public function progress(int|float $target): static
public function progress(int|float|Closure $target): static
{
$this->progress = true;
$this->target = $target;
$this->target = is_callable($target) ? $target() : $target;

return $this;
}
Expand Down
3 changes: 3 additions & 0 deletions src/QueryTags/QueryTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ final class QueryTag
use HasCanSee;
use WithLabel;

/**
* @deprecated Builder $builder, use Closure $builder
*/
public function __construct(
string $label,
protected Builder|Closure $builder,
Expand Down