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 |
|
$lowerName = mb_strtolower($generator->getInflector()->singularize($field->name)); |
42
|
|
|
|
43
|
1 |
|
$type1 = $generator->getLowerName(); |
44
|
1 |
|
$type2 = $lowerName; |
45
|
|
|
|
46
|
|
|
// we only generate once, so use a comparison for that |
47
|
1 |
|
if (strcasecmp($type1, $type2) < 0) { |
48
|
1 |
|
$generator->generateManyToManyTable($type1, $type2); |
49
|
|
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
public static function processModelTypeDirective( |
53
|
|
|
ModelGenerator $generator, |
54
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
55
|
|
|
): void { |
56
|
|
|
// nothing |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public static function processModelFieldDirective( |
60
|
|
|
ModelGenerator $generator, |
61
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
62
|
|
|
\Formularium\Field $fieldFormularium, |
63
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
64
|
|
|
): void { |
65
|
|
|
// nothing |
66
|
1 |
|
} |
67
|
|
|
|
68
|
1 |
|
public static function processModelRelationshipDirective( |
69
|
|
|
ModelGenerator $generator, |
70
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
71
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
72
|
|
|
\Formularium\Datatype $datatype = null |
73
|
|
|
): ?\Formularium\Datatype { |
74
|
1 |
|
$lowerName = mb_strtolower($generator->getInflector()->singularize($field->name)); |
75
|
1 |
|
$lowerNamePlural = $generator->getInflector()->pluralize($lowerName); |
76
|
|
|
|
77
|
1 |
|
$sourceTypeName = $generator->getLowerName(); |
78
|
1 |
|
$targetTypeName = $lowerName; |
79
|
1 |
|
$relationship = null; |
|
|
|
|
80
|
1 |
|
$isInverse = false; |
|
|
|
|
81
|
|
|
|
82
|
1 |
|
$targetClass = Str::studly($generator->getInflector()->singularize($field->name)); |
83
|
1 |
|
$generateRandom = true; // TODO |
|
|
|
|
84
|
1 |
|
$relationship = RelationshipFactory::RELATIONSHIP_MANY_TO_MANY; |
85
|
1 |
|
$isInverse = true; |
86
|
1 |
|
$generator->class->addMethod($lowerNamePlural) |
87
|
1 |
|
->setPublic() |
88
|
1 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany') |
89
|
1 |
|
->setBody("return \$this->belongsToMany($targetClass::class);"); |
90
|
|
|
|
91
|
1 |
|
$datatypeName = $generator->getRelationshipDatatypeName( |
92
|
1 |
|
$relationship, |
93
|
|
|
$isInverse, |
94
|
|
|
$sourceTypeName, |
95
|
|
|
$targetTypeName |
96
|
|
|
); |
97
|
1 |
|
return DatatypeFactory::factory($datatypeName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function processSeedTypeDirective( |
101
|
|
|
SeedGenerator $generator, |
102
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
103
|
|
|
): void { |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public static function processSeedFieldDirective( |
107
|
|
|
SeedGenerator $generator, |
108
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
109
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
110
|
|
|
): void { |
111
|
1 |
|
$type1 = $generator->getLowerName(); |
112
|
1 |
|
$type2 = mb_strtolower($generator->getInflector()->singularize($field->name)); |
113
|
|
|
|
114
|
1 |
|
if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work |
115
|
1 |
|
$relationship = mb_strtolower($generator->getInflector()->pluralize($field->name)); |
116
|
1 |
|
$generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship); |
117
|
|
|
} |
118
|
1 |
|
} |
119
|
|
|
|
120
|
1 |
|
protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
return <<<EOF |
123
|
|
|
|
124
|
|
|
try { |
125
|
1 |
|
\${$targetModel}Items = App\\Models\\$targetModel::all(); |
126
|
1 |
|
\$model->{$relationship}()->attach( |
127
|
1 |
|
\${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray() |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
catch (\InvalidArgumentException \$e) { |
131
|
1 |
|
\$model->{$relationship}()->attach( |
132
|
1 |
|
\${$targetModel}Items->random(1)->pluck('id')->toArray() |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
EOF; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|