1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\FluidSchema; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use function addslashes; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use function var_export; |
10
|
|
|
|
11
|
|
|
class TdbmFluidColumnGraphqlOptions |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var TdbmFluidColumnOptions |
15
|
|
|
*/ |
16
|
|
|
private $tdbmFluidColumnOptions; |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name; |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $outputType; |
25
|
|
|
/** |
26
|
|
|
* @var FluidColumn |
27
|
|
|
*/ |
28
|
|
|
private $fluidColumn; |
29
|
|
|
|
30
|
|
|
public function __construct(TdbmFluidColumnOptions $tdbmFluidColumnOptions, FluidColumn $fluidColumn) |
31
|
|
|
{ |
32
|
|
|
$this->tdbmFluidColumnOptions = $tdbmFluidColumnOptions; |
33
|
|
|
$this->fluidColumn = $fluidColumn; |
34
|
|
|
if (!$this->getComment()->hasAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field')) { |
35
|
|
|
$this->generateFieldAnnotation(); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function getComment(): Comment |
40
|
|
|
{ |
41
|
|
|
$comment = $this->fluidColumn->getDbalColumn()->getComment(); |
42
|
|
|
|
43
|
|
|
return new Comment($comment ?? ''); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function fieldName(string $name): self |
47
|
|
|
{ |
48
|
|
|
$this->name = $name; |
49
|
|
|
$this->generateFieldAnnotation(); |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function outputType(string $outputType): self |
54
|
|
|
{ |
55
|
|
|
$this->outputType = $outputType; |
56
|
|
|
$this->generateFieldAnnotation(); |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function generateFieldAnnotation(): void |
61
|
|
|
{ |
62
|
|
|
$outputType = null; |
63
|
|
|
if ($this->outputType !== null) { |
64
|
|
|
$outputType = $this->outputType; |
65
|
|
|
} elseif ($this->fluidColumn->getDbalColumn()->getType() === Type::getType(Type::GUID)) { |
66
|
|
|
// are we part of a foreign key or not? |
67
|
|
|
$fks = $this->tdbmFluidColumnOptions->then()->getDbalTable()->getForeignKeys(); |
68
|
|
|
$isPartOfFk = false; |
69
|
|
|
foreach ($fks as $fk) { |
70
|
|
|
if (in_array($this->fluidColumn->getDbalColumn()->getName(), $fk->getUnquotedLocalColumns(), true) === true) { |
71
|
|
|
$isPartOfFk = true; |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
if ($isPartOfFk === false) { |
76
|
|
|
$outputType = 'ID'; |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
// If the column is the primary key, let's add an ID type |
80
|
|
|
$pk = $this->tdbmFluidColumnOptions->then()->getDbalTable()->getPrimaryKey(); |
81
|
|
|
if ($pk !== null && $pk->getUnquotedColumns() === [$this->fluidColumn->getDbalColumn()->getName()]) { |
82
|
|
|
$outputType = 'ID'; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$parameters = array_filter([ |
87
|
|
|
'name' => $this->name, |
88
|
|
|
'outputType' => $outputType |
89
|
|
|
]); |
90
|
|
|
if (empty($parameters)) { |
91
|
|
|
$parameters = null; |
92
|
|
|
} |
93
|
|
|
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field', $parameters); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function logged(bool $mustBeLogged = true): self |
97
|
|
|
{ |
98
|
|
|
if ($mustBeLogged) { |
99
|
|
|
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Logged'); |
100
|
|
|
} else { |
101
|
|
|
$this->removeAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Logged'); |
102
|
|
|
} |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function right(string $rightName): self |
107
|
|
|
{ |
108
|
|
|
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Right', ['name'=>$rightName]); |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function failWith($value): self |
113
|
|
|
{ |
114
|
|
|
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\FailWith', $value, true, true); |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $annotation |
120
|
|
|
* @param mixed $content |
121
|
|
|
* @param bool $replaceExisting |
122
|
|
|
* @return TdbmFluidColumnGraphqlOptions |
123
|
|
|
*/ |
124
|
|
|
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self |
125
|
|
|
{ |
126
|
|
|
$this->tdbmFluidColumnOptions->addAnnotation($annotation, $content, $replaceExisting, $explicitNull); |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function removeAnnotation(string $annotation): self |
131
|
|
|
{ |
132
|
|
|
$this->tdbmFluidColumnOptions->removeAnnotation($annotation); |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function endGraphql(): TdbmFluidColumnOptions |
137
|
|
|
{ |
138
|
|
|
return $this->tdbmFluidColumnOptions; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function then(): TdbmFluidTable |
142
|
|
|
{ |
143
|
|
|
return $this->tdbmFluidColumnOptions->then(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function column(string $name): TdbmFluidColumn |
147
|
|
|
{ |
148
|
|
|
return $this->tdbmFluidColumnOptions->column($name); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|