mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
69 lines
2.4 KiB
PHP
69 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\GraphQL\Schema\Inputs\Base;
|
|
|
|
use App\Contracts\GraphQL\Fields\CreatableField;
|
|
use App\Contracts\GraphQL\Fields\RequiredOnCreation;
|
|
use App\GraphQL\Schema\Fields\Field;
|
|
use App\GraphQL\Schema\Inputs\Input;
|
|
use App\GraphQL\Schema\Inputs\Relations\CreateBelongsToInput;
|
|
use App\GraphQL\Schema\Inputs\Relations\CreateBelongsToManyInput;
|
|
use App\GraphQL\Schema\Inputs\Relations\CreateHasManyInput;
|
|
use App\GraphQL\Schema\Relations\BelongsToManyRelation;
|
|
use App\GraphQL\Schema\Relations\BelongsToRelation;
|
|
use App\GraphQL\Schema\Relations\HasManyRelation;
|
|
use App\GraphQL\Schema\Relations\Relation;
|
|
use App\GraphQL\Schema\Types\EloquentType;
|
|
use App\GraphQL\Support\InputField;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class CreateInput extends Input
|
|
{
|
|
public function __construct(
|
|
protected EloquentType $type,
|
|
) {}
|
|
|
|
public function getName(): string
|
|
{
|
|
return "Create{$this->type->getName()}Input";
|
|
}
|
|
|
|
/**
|
|
* @return InputField[]
|
|
*/
|
|
public function fieldClasses(): array
|
|
{
|
|
$fields = [];
|
|
|
|
$baseType = $this->type;
|
|
|
|
$fields[] = collect($baseType->fieldClasses())
|
|
->filter(fn (Field $field): bool => $field instanceof CreatableField) // and submitable field?
|
|
->map(
|
|
fn (Field&CreatableField $field): InputField => new InputField($field->getName(), $field->type().($field instanceof RequiredOnCreation ? '!' : ''))
|
|
)
|
|
->toArray();
|
|
|
|
$fields[] = collect($baseType->relations())
|
|
->mapWithKeys(function (Relation $relation): array {
|
|
$baseType = $relation->getBaseType();
|
|
if (! $baseType instanceof EloquentType) {
|
|
return [];
|
|
}
|
|
|
|
return match (true) {
|
|
$relation instanceof BelongsToRelation => [$relation->getName() => new CreateBelongsToInput($baseType)],
|
|
$relation instanceof HasManyRelation => [$relation->getName() => new CreateHasManyInput($baseType)],
|
|
$relation instanceof BelongsToManyRelation => [$relation->getName() => new CreateBelongsToManyInput($relation->getEdgeType()->getPivotType())],
|
|
default => [],
|
|
};
|
|
})
|
|
->map(fn (Input $input, string $name): InputField => new InputField($name, $input->getName()))
|
|
->toArray();
|
|
|
|
return Arr::flatten($fields);
|
|
}
|
|
}
|