Fix filter
continuous-integration/drone/push Build is passing Details

This commit is contained in:
philipp lang 2023-03-23 00:00:41 +01:00
parent 85a3abcc82
commit 6a9f3e5cec
2 changed files with 1 additions and 73 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Views;
use App\Activity;
use App\Lib\Filter;
use Illuminate\Database\Eloquent\Builder;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\MapOutputName;
@ -42,4 +43,3 @@ class ActivityFilterScope extends Filter
return $query;
}
}

View File

@ -1,72 +0,0 @@
<?php
namespace App\Http\Views;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Spatie\LaravelData\Data;
/**
* @template T of Model
*/
abstract class Filter extends Data
{
public string $unsetReplacer = 'yoNee3ainge4eetiier9ogaiChoe0ahcaR3Hu1uzah8xaiv7ael7yahphai7ruG9';
/**
* @return array<string, mixed>
*/
abstract protected function locks(): array;
public static function fromRequest(?string $request = null): static
{
$parameters = json_decode(base64_decode($request), true);
return static::from($parameters ?: [])->parseLocks();
}
public function parseLocks(): static
{
foreach ($this->locks() as $key => $value) {
if ($value === $this->unsetReplacer) {
continue;
}
$this->{$key} = $value;
}
return $this;
}
/**
* @param mixed $value
*
* @return mixed
*/
public function when(bool $when, $value)
{
return $when ? $value : $this->unsetReplacer;
}
/**
* @param Builder<T> $query
*
* @return Builder<T>
*/
protected function applyOwnOthers(Builder $query, bool $own, bool $others): Builder
{
if ($own && !$others) {
$query->where('user_id', auth()->id());
}
if (!$own && $others) {
$query->where('user_id', '!=', auth()->id());
}
if (!$own && !$others) {
$query->where('id', -1);
}
return $query;
}
}