1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Modelarium\Datatypes\RelationshipFactory; |
7
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
8
|
|
|
use Modelarium\Laravel\Targets\SeedGenerator; |
9
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
10
|
|
|
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface; |
11
|
|
|
|
12
|
|
|
class BelongsToDirective implements ModelDirectiveInterface, SeedDirectiveInterface |
13
|
|
|
{ |
14
|
|
|
public static function processModelTypeDirective( |
15
|
|
|
ModelGenerator $generator, |
16
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
17
|
|
|
): void { |
18
|
|
|
// nothing |
19
|
|
|
} |
20
|
|
|
|
21
|
4 |
|
public static function processModelFieldDirective( |
22
|
|
|
ModelGenerator $generator, |
23
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
24
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
25
|
|
|
): void { |
26
|
|
|
// nothing |
27
|
4 |
|
} |
28
|
|
|
|
29
|
4 |
|
public static function processModelRelationshipDirective( |
30
|
|
|
ModelGenerator $generator, |
31
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
32
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
33
|
|
|
): string { |
34
|
4 |
|
$lowerName = mb_strtolower($generator->getInflector()->singularize($field->name)); |
35
|
4 |
|
$lowerNamePlural = $generator->getInflector()->pluralize($lowerName); |
|
|
|
|
36
|
|
|
|
37
|
4 |
|
$sourceTypeName = $generator->getLowerName(); |
38
|
4 |
|
$targetTypeName = $lowerName; |
39
|
4 |
|
$relationship = null; |
|
|
|
|
40
|
4 |
|
$isInverse = false; |
|
|
|
|
41
|
|
|
|
42
|
4 |
|
$targetClass = '\\App\\Models\\' . Str::studly($generator->getInflector()->singularize($field->name)); |
43
|
4 |
|
$generateRandom = true; // TODO |
|
|
|
|
44
|
4 |
|
$relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_MANY; |
45
|
4 |
|
$isInverse = true; |
46
|
4 |
|
$generator->class->addMethod($lowerName) |
47
|
4 |
|
->setPublic() |
48
|
4 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsTo') |
49
|
4 |
|
->setBody("return \$this->belongsTo($targetClass::class);"); |
50
|
|
|
|
51
|
4 |
|
$relationshipDatatype = "relationship:" . ($isInverse ? "inverse:" : "") . |
|
|
|
|
52
|
4 |
|
"$relationship:$sourceTypeName:$targetTypeName"; |
53
|
|
|
|
54
|
4 |
|
return $relationshipDatatype; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public static function processSeedFieldDirective( |
58
|
|
|
SeedGenerator $generator, |
59
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
60
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
61
|
|
|
): void { |
62
|
4 |
|
$type1 = $generator->getLowerName(); |
63
|
4 |
|
$type2 = mb_strtolower($generator->getInflector()->singularize($field->name)); |
64
|
|
|
|
65
|
4 |
|
if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work |
66
|
4 |
|
$relationship = mb_strtolower($generator->getInflector()->pluralize($field->name)); |
67
|
4 |
|
$generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship); |
68
|
|
|
} |
69
|
4 |
|
} |
70
|
|
|
|
71
|
4 |
|
protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
return <<<EOF |
74
|
|
|
|
75
|
|
|
try { |
76
|
4 |
|
\${$targetModel}Items = App\\Models\\$targetModel::all(); |
77
|
4 |
|
\$model->{$relationship}()->attach( |
78
|
4 |
|
\${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray() |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
catch (\InvalidArgumentException \$e) { |
82
|
4 |
|
\$model->{$relationship}()->attach( |
83
|
4 |
|
\${$targetModel}Items->random(1)->pluck('id')->toArray() |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
EOF; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|