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\Laravel\Targets\ModelGenerator; |
9
|
|
|
use Modelarium\Laravel\Targets\SeedGenerator; |
10
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
11
|
|
|
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface; |
12
|
|
|
use Modelarium\Parser; |
13
|
|
|
|
14
|
|
|
class HasOneDirective implements ModelDirectiveInterface, SeedDirectiveInterface |
15
|
|
|
{ |
16
|
|
|
public static function processModelTypeDirective( |
17
|
|
|
ModelGenerator $generator, |
18
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
19
|
|
|
): void { |
20
|
|
|
// nothing |
21
|
|
|
} |
22
|
|
|
|
23
|
3 |
|
public static function processModelFieldDirective( |
24
|
|
|
ModelGenerator $generator, |
25
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
26
|
|
|
\Formularium\Field $fieldFormularium, |
27
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
28
|
|
|
): void { |
29
|
|
|
// nothing |
30
|
3 |
|
} |
31
|
|
|
|
32
|
3 |
|
public static function processModelRelationshipDirective( |
33
|
|
|
ModelGenerator $generator, |
34
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
35
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
36
|
|
|
\Formularium\Datatype $datatype = null |
37
|
|
|
): ?\Formularium\Datatype { |
38
|
3 |
|
list($type, $isRequired) = Parser::getUnwrappedType($field->type); |
39
|
|
|
|
40
|
3 |
|
$sourceTypeName = $generator->getLowerName(); |
41
|
3 |
|
$targetTypeName = $type->name; |
42
|
3 |
|
$relationship = null; |
|
|
|
|
43
|
3 |
|
$isInverse = false; |
|
|
|
|
44
|
3 |
|
$generateRandom = true; // TODO |
|
|
|
|
45
|
|
|
|
46
|
3 |
|
$relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_ONE; |
47
|
3 |
|
$isInverse = false; |
48
|
3 |
|
$generator->class->addMethod(Str::snake(Str::studly($targetTypeName))) |
49
|
3 |
|
->setPublic() |
50
|
3 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\HasOne') |
51
|
3 |
|
->setBody("return \$this->hasOne($targetTypeName::class);"); |
52
|
|
|
|
53
|
3 |
|
$datatypeName = $generator->getRelationshipDatatypeName( |
54
|
3 |
|
$relationship, |
55
|
|
|
$isInverse, |
56
|
|
|
$sourceTypeName, |
57
|
|
|
$targetTypeName |
58
|
|
|
); |
59
|
3 |
|
return DatatypeFactory::factory($datatypeName); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function processSeedTypeDirective( |
63
|
|
|
SeedGenerator $generator, |
64
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
65
|
|
|
): void { |
66
|
|
|
// empty |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public static function processSeedFieldDirective( |
70
|
|
|
SeedGenerator $generator, |
71
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
72
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
73
|
|
|
): void { |
74
|
3 |
|
$type1 = $generator->getLowerName(); |
75
|
3 |
|
$type2 = mb_strtolower($generator->getInflector()->singularize($field->name)); |
76
|
|
|
|
77
|
3 |
|
if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work |
78
|
|
|
$relationship = mb_strtolower($generator->getInflector()->pluralize($field->name)); |
79
|
|
|
$generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship); |
80
|
|
|
} |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return <<<EOF |
86
|
|
|
|
87
|
|
|
try { |
88
|
|
|
\${$targetModel}Items = App\\Models\\$targetModel::all(); |
89
|
|
|
\$model->{$relationship}()->attach( |
90
|
|
|
\${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
catch (\InvalidArgumentException \$e) { |
94
|
|
|
\$model->{$relationship}()->attach( |
95
|
|
|
\${$targetModel}Items->random(1)->pluck('id')->toArray() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
EOF; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|