1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
4
|
|
|
|
5
|
|
|
use Formularium\Factory\DatatypeFactory; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Modelarium\Datatypes\RelationshipFactory; |
8
|
|
|
use Modelarium\Exception\DirectiveException; |
9
|
|
|
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface; |
10
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
11
|
|
|
use Modelarium\Laravel\Targets\SeedGenerator; |
12
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
13
|
|
|
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface; |
14
|
|
|
use Modelarium\Laravel\Targets\MigrationCodeFragment; |
15
|
|
|
use Modelarium\Laravel\Targets\MigrationGenerator; |
16
|
|
|
|
17
|
|
|
class BelongsToManyDirective implements MigrationDirectiveInterface, ModelDirectiveInterface, SeedDirectiveInterface |
18
|
|
|
{ |
19
|
|
|
public static function processMigrationTypeDirective( |
20
|
|
|
MigrationGenerator $generator, |
21
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
22
|
|
|
): void { |
23
|
|
|
throw new DirectiveException("Directive not supported here"); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function processMigrationFieldDirective( |
27
|
|
|
MigrationGenerator $generator, |
28
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
29
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
30
|
|
|
MigrationCodeFragment $code |
31
|
|
|
): void { |
32
|
|
|
throw new DirectiveException("Directive not supported here"); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public static function processMigrationRelationshipDirective( |
36
|
|
|
MigrationGenerator $generator, |
37
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
38
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
39
|
|
|
MigrationCodeFragment $code |
40
|
|
|
): void { |
41
|
1 |
|
$type1 = $generator->getLowerFirstLetterName(); |
42
|
1 |
|
$type2 = lcfirst($generator->getInflector()->singularize($field->name)); |
43
|
|
|
|
44
|
|
|
// we only generate once, so use a comparison for that |
45
|
1 |
|
if (strcasecmp($type1, $type2) < 0) { |
46
|
1 |
|
$generator->generateManyToManyTable($type1, $type2); |
47
|
|
|
} |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
public static function processModelTypeDirective( |
51
|
|
|
ModelGenerator $generator, |
52
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
53
|
|
|
): void { |
54
|
|
|
// nothing |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public static function processModelFieldDirective( |
58
|
|
|
ModelGenerator $generator, |
59
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
60
|
|
|
\Formularium\Field $fieldFormularium, |
61
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
62
|
|
|
): void { |
63
|
|
|
// nothing |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
public static function processModelRelationshipDirective( |
67
|
|
|
ModelGenerator $generator, |
68
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
69
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
70
|
|
|
\Formularium\Datatype $datatype = null |
71
|
|
|
): ?\Formularium\Datatype { |
72
|
1 |
|
$lowerName = lcfirst($generator->getInflector()->singularize($field->name)); |
73
|
1 |
|
$lowerNamePlural = lcfirst($generator->getInflector()->pluralize($lowerName)); |
74
|
|
|
|
75
|
1 |
|
$sourceTypeName = $generator->getBaseName(); |
76
|
1 |
|
$targetTypeName = $lowerName; |
77
|
1 |
|
$relationship = null; |
|
|
|
|
78
|
1 |
|
$isInverse = false; |
|
|
|
|
79
|
|
|
|
80
|
1 |
|
$targetClass = Str::studly($generator->getInflector()->singularize($field->name)); |
81
|
1 |
|
$generateRandom = true; // TODO |
|
|
|
|
82
|
1 |
|
$relationship = RelationshipFactory::RELATIONSHIP_MANY_TO_MANY; |
83
|
1 |
|
$isInverse = true; |
84
|
1 |
|
$generator->class->addMethod($lowerNamePlural) |
85
|
1 |
|
->setPublic() |
86
|
1 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany') |
87
|
1 |
|
->setBody("return \$this->belongsToMany($targetClass::class);"); |
88
|
|
|
|
89
|
1 |
|
$datatypeName = $generator->getRelationshipDatatypeName( |
90
|
1 |
|
$relationship, |
91
|
|
|
$isInverse, |
92
|
|
|
$sourceTypeName, |
93
|
|
|
$targetTypeName |
94
|
|
|
); |
95
|
1 |
|
return DatatypeFactory::factory($datatypeName); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function processSeedTypeDirective( |
99
|
|
|
SeedGenerator $generator, |
100
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
101
|
|
|
): void { |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public static function processSeedFieldDirective( |
105
|
|
|
SeedGenerator $generator, |
106
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
107
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
108
|
|
|
): void { |
109
|
1 |
|
$type1 = $generator->getLowerName(); |
110
|
1 |
|
$type2 = lcfirst($generator->getInflector()->singularize($field->name)); |
111
|
|
|
|
112
|
1 |
|
if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work |
113
|
1 |
|
$relationship = lcfirst($generator->getInflector()->pluralize($field->name)); |
114
|
1 |
|
$generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship); |
115
|
|
|
} |
116
|
1 |
|
} |
117
|
|
|
|
118
|
1 |
|
protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string |
|
|
|
|
119
|
|
|
{ |
120
|
1 |
|
$className = Str::studly($targetModel); |
121
|
|
|
return <<<EOF |
122
|
|
|
|
123
|
|
|
try { |
124
|
1 |
|
\${$targetModel}Items = App\\Models\\$className::all(); |
125
|
1 |
|
\$model->{$relationship}()->attach( |
126
|
1 |
|
\${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray() |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
catch (\InvalidArgumentException \$e) { |
130
|
1 |
|
\$model->{$relationship}()->attach( |
131
|
1 |
|
\${$targetModel}Items->random(1)->pluck('id')->toArray() |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
EOF; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|