mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-09-05 20:25:05 +02:00
56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Concerns\GraphQL;
|
|
|
|
use App\Contracts\GraphQL\FilterableField;
|
|
use App\GraphQL\Definition\Fields\Field;
|
|
use App\GraphQL\Definition\Directives\Filters\FilterDirective;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Trait ResolvesArguments.
|
|
*/
|
|
trait ResolvesArguments
|
|
{
|
|
/**
|
|
* Build the arguments array into string.
|
|
*
|
|
* @param array $arguments
|
|
* @return string
|
|
*/
|
|
public function buildArguments(array $arguments): string
|
|
{
|
|
if (filled($arguments)) {
|
|
return Str::of('(')
|
|
->append(implode("\n", Arr::flatten($arguments)))
|
|
->append(')')
|
|
->toString();
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Resolve the fields into arguments that are used for filtering.
|
|
*
|
|
* @param array<int, Field> $fields
|
|
* @return array<int, string>
|
|
*/
|
|
public function resolveFilterArguments(array $fields): array
|
|
{
|
|
return collect($fields)
|
|
->map(function (Field $field) {
|
|
if ($field instanceof FilterableField) {
|
|
return collect($field->filterDirectives())
|
|
->map(fn (FilterDirective $directive) => $directive->toString())
|
|
->toArray();
|
|
}
|
|
})
|
|
->flatten()
|
|
->toArray();
|
|
}
|
|
}
|