This commit is contained in:
2024-09-01 18:54:23 +05:00
parent 76d18365a5
commit 061f09eca1
1597 changed files with 109451 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace Laravel\Nova\Filters;
use Laravel\Nova\Http\Requests\NovaRequest;
abstract class BooleanFilter extends Filter
{
/**
* The filter's component.
*
* @var string
*/
public $component = 'boolean-filter';
/**
* Set the default options for the filter.
*
* @return array
*/
public function default()
{
return collect($this->options(app(NovaRequest::class)))->values()->mapWithKeys(function ($option) {
return [$option => false];
})->all();
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Laravel\Nova\Filters;
abstract class DateFilter extends Filter
{
/**
* The filter's component.
*
* @var string
*/
public $component = 'date-filter';
/**
* Set the first day of the week.
*
* @param int $day
* @return $this
*/
public function firstDayOfWeek($day)
{
return $this->withMeta([__FUNCTION__ => $day]);
}
}

119
nova/src/Filters/Filter.php Normal file
View File

@@ -0,0 +1,119 @@
<?php
namespace Laravel\Nova\Filters;
use JsonSerializable;
use Laravel\Nova\AuthorizedToSee;
use Laravel\Nova\Contracts\Filter as FilterContract;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Makeable;
use Laravel\Nova\Metable;
use Laravel\Nova\Nova;
use Laravel\Nova\ProxiesCanSeeToGate;
abstract class Filter implements FilterContract, JsonSerializable
{
use AuthorizedToSee;
use Makeable;
use Metable;
use ProxiesCanSeeToGate;
/**
* The displayable name of the filter.
*
* @var string
*/
public $name;
/**
* The filter's component.
*
* @var string
*/
public $component = 'select-filter';
/**
* Apply the filter to the given query.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder
*/
abstract public function apply(NovaRequest $request, $query, $value);
/**
* Get the filter's available options.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function options(NovaRequest $request)
{
return [];
}
/**
* Get the component name for the filter.
*
* @return string
*/
public function component()
{
return $this->component;
}
/**
* Get the displayable name of the filter.
*
* @return string
*/
public function name()
{
return $this->name ?: Nova::humanize($this);
}
/**
* Get the key for the filter.
*
* @return string
*/
public function key()
{
return get_class($this);
}
/**
* Set the default options for the filter.
*
* @return array|mixed
*/
public function default()
{
return '';
}
/**
* Prepare the filter for JSON serialization.
*
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return array_merge([
'class' => $this->key(),
'name' => $this->name(),
'component' => $this->component(),
'options' => collect($this->options(app(NovaRequest::class)))->map(function ($value, $label) {
if (is_array($value)) {
return array_merge(['label' => $label], $value);
} elseif (is_string($label)) {
return ['label' => $label, 'value' => $value];
}
return ['label' => $value, 'value' => $value];
})->values()->all(),
'currentValue' => $this->default() ?? '',
], $this->meta());
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Laravel\Nova\Filters;
use Laravel\Nova\Query\ApplyFilter;
class FilterDecoder
{
/**
* The filter string to be decoded.
*
* @var string
*/
protected $filterString;
/**
* The filters available via the request.
*
* @var \Illuminate\Support\Collection
*/
protected $availableFilters;
/**
* Create a new FilterDecoder instance.
*
* @param string $filterString
* @param \Illuminate\Support\Collection|array|null $availableFilters
*/
public function __construct($filterString, $availableFilters = null)
{
$this->filterString = $filterString;
$this->availableFilters = collect($availableFilters);
}
/**
* Decode the given filters.
*
* @return \Illuminate\Support\Collection<int, \Laravel\Nova\Query\ApplyFilter>
*/
public function filters()
{
if (empty($filters = $this->decodeFromBase64String())) {
return collect();
}
return collect($filters)->map(function ($filter) {
$class = key($filter);
$value = $filter[$class];
$matchingFilter = $this->availableFilters->first(function ($availableFilter) use ($class) {
return $class === $availableFilter->key();
});
if ($matchingFilter) {
return ['filter' => $matchingFilter, 'value' => $value];
}
})
->filter()
->reject(function ($filter) {
if (is_array($filter['value'])) {
return count($filter['value']) < 1;
} elseif (is_string($filter['value'])) {
return trim($filter['value']) === '';
}
return is_null($filter['value']);
})->map(function ($filter) {
return new ApplyFilter($filter['filter'], $filter['value']);
})->values();
}
/**
* Decode the filter string from base64 encoding.
*
* @return array<int, array<class-string<\Laravel\Nova\Filters\Filter>|string, mixed>>
*/
public function decodeFromBase64String()
{
if (empty($this->filterString)) {
return [];
}
$filters = json_decode(base64_decode($this->filterString), true);
return is_array($filters) ? $filters : [];
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Laravel\Nova\Filters;
class FilterEncoder
{
/**
* @var array
*/
public $filters;
/**
* Create a new filter encoder instance.
*
* @param array $filters
*/
public function __construct($filters = [])
{
$this->filters = $filters;
}
/**
* Encode the filters into a query string.
*
* @return string
*/
public function encode()
{
return base64_encode(json_encode($this->filters));
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Laravel\Nova\Filters;
abstract class RangeFilter extends Filter
{
/**
* The filter's component.
*
* @var string
*/
public $component = 'range-filter';
}