|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface; |
|
7
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
|
8
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
|
9
|
|
|
use Modelarium\Laravel\Targets\MigrationCodeFragment; |
|
10
|
|
|
use Modelarium\Laravel\Targets\MigrationGenerator; |
|
11
|
|
|
|
|
12
|
|
|
class MigrationUniqueIndexDirective implements ModelDirectiveInterface, MigrationDirectiveInterface |
|
13
|
|
|
{ |
|
14
|
|
|
public static function processModelTypeDirective( |
|
15
|
|
|
ModelGenerator $generator, |
|
16
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
17
|
|
|
): void { |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public static function processModelFieldDirective( |
|
21
|
|
|
ModelGenerator $generator, |
|
22
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
23
|
|
|
\Formularium\Field $fieldFormularium, |
|
24
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
25
|
|
|
): void { |
|
26
|
|
|
$fieldName = $field->name; |
|
27
|
|
|
$studlyName = $generator->getStudlyName(); |
|
28
|
|
|
$generator->class->addMethod('from' . Str::studly($fieldName)) |
|
29
|
|
|
->setPublic() |
|
30
|
|
|
->setStatic() |
|
31
|
|
|
->setReturnType('\\App\\Models\\' . $studlyName) |
|
32
|
|
|
->setReturnNullable() |
|
33
|
|
|
->addComment("Factory from the $fieldName unique index") |
|
34
|
|
|
->setBody("return {$studlyName}::firstWhere('$fieldName', \$value);") |
|
35
|
|
|
->addParameter('value'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function processModelRelationshipDirective( |
|
39
|
|
|
ModelGenerator $generator, |
|
40
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
41
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
42
|
|
|
): string { |
|
43
|
|
|
return ''; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public static function processMigrationTypeDirective( |
|
47
|
|
|
MigrationGenerator $generator, |
|
48
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
49
|
|
|
): void { |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function processMigrationFieldDirective( |
|
53
|
|
|
MigrationGenerator $generator, |
|
54
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
55
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
|
56
|
|
|
MigrationCodeFragment $code |
|
57
|
|
|
): void { |
|
58
|
|
|
$code->appendExtraLine('$table->unique("' . $field->name . '");'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|