|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DeInternetJongens\LighthouseUtils\Directives; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Nuwave\Lighthouse\Schema\Directives\BaseDirective as LighthouseBaseDirective; |
|
7
|
|
|
use Nuwave\Lighthouse\Schema\Values\ArgumentValue; |
|
8
|
|
|
use Nuwave\Lighthouse\Support\Contracts\ArgMiddleware; |
|
9
|
|
|
use Nuwave\Lighthouse\Support\Traits\HandlesQueryFilter; |
|
10
|
|
|
|
|
11
|
|
|
abstract class BaseDirective extends LighthouseBaseDirective implements ArgMiddleware |
|
12
|
|
|
{ |
|
13
|
|
|
use HandlesQueryFilter; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
public function handleArgument(ArgumentValue $argument, \Closure $next): ArgumentValue |
|
16
|
|
|
{ |
|
17
|
|
|
$argument = $this->injectFilter( |
|
18
|
|
|
$argument, |
|
19
|
|
|
function (Builder $builder, string $key, array $arguments): Builder { |
|
20
|
|
|
$value = $arguments[$key]; |
|
21
|
|
|
$field = \preg_replace(sprintf('/%s$/', $this->getSuffix()), '', $key); |
|
22
|
|
|
|
|
23
|
|
|
return $this->handle($field, $value, $builder); |
|
24
|
|
|
} |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
return $next($argument); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get the suffix for this query, e.g. : foo_contains, _contains is the suffix here. |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function getSuffix(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return sprintf('_%s', $this->name()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Add query statement to the eloquent builder. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $fieldName |
|
43
|
|
|
* @param mixed $value The value can be a string, number, bool, etc… This mixed is on purpose! |
|
44
|
|
|
* @param Builder $builder |
|
45
|
|
|
* @return Builder |
|
46
|
|
|
*/ |
|
47
|
|
|
abstract public function handle(string $fieldName, $value, Builder $builder): Builder; |
|
48
|
|
|
} |
|
49
|
|
|
|