|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQL\Doctrine\Attribute; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract attribute with common logic for Argument and Field. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class AbstractAttribute implements ApiAttribute |
|
13
|
|
|
{ |
|
14
|
|
|
protected const NO_VALUE_PASSED = '_hacky_no_value_passed_marker_'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Instance of the GraphQL type. |
|
18
|
|
|
*/ |
|
19
|
|
|
private ?Type $typeInstance = null; |
|
20
|
|
|
|
|
21
|
|
|
private mixed $defaultValue; |
|
22
|
|
|
|
|
23
|
|
|
private bool $hasDefaultValue = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param null|string $type FQCN of PHP class implementing the GraphQL type |
|
27
|
|
|
*/ |
|
28
|
16 |
|
public function __construct( |
|
29
|
|
|
private ?string $name, |
|
30
|
|
|
private readonly ?string $type, |
|
31
|
|
|
private ?string $description, |
|
32
|
|
|
mixed $defaultValue, |
|
33
|
|
|
) { |
|
34
|
16 |
|
if ($defaultValue !== self::NO_VALUE_PASSED) { |
|
35
|
|
|
$this->setDefaultValue($defaultValue); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
11 |
|
public function toArray(): array |
|
40
|
|
|
{ |
|
41
|
11 |
|
$data = [ |
|
42
|
11 |
|
'name' => $this->getName(), |
|
43
|
11 |
|
'type' => $this->getTypeInstance(), |
|
44
|
11 |
|
'description' => $this->getDescription(), |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
11 |
|
if ($this->hasDefaultValue()) { |
|
48
|
9 |
|
$data['defaultValue'] = $this->getDefaultValue(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
11 |
|
return $data; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
15 |
|
public function getName(): ?string |
|
55
|
|
|
{ |
|
56
|
15 |
|
return $this->name; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
15 |
|
public function setName(string $name): void |
|
60
|
|
|
{ |
|
61
|
15 |
|
$this->name = $name; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
15 |
|
public function getType(): ?string |
|
65
|
|
|
{ |
|
66
|
15 |
|
return $this->type; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
15 |
|
public function getDescription(): ?string |
|
70
|
|
|
{ |
|
71
|
15 |
|
return $this->description; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
15 |
|
public function setDescription(?string $description): void |
|
75
|
|
|
{ |
|
76
|
15 |
|
$this->description = $description; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
15 |
|
public function hasDefaultValue(): bool |
|
80
|
|
|
{ |
|
81
|
15 |
|
return $this->hasDefaultValue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
9 |
|
public function getDefaultValue(): mixed |
|
85
|
|
|
{ |
|
86
|
9 |
|
return $this->defaultValue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
9 |
|
public function setDefaultValue(mixed $defaultValue): void |
|
90
|
|
|
{ |
|
91
|
9 |
|
$this->defaultValue = $defaultValue; |
|
92
|
9 |
|
$this->hasDefaultValue = true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
15 |
|
public function getTypeInstance(): ?Type |
|
96
|
|
|
{ |
|
97
|
15 |
|
return $this->typeInstance; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
15 |
|
public function setTypeInstance(?Type $typeInstance): void |
|
101
|
|
|
{ |
|
102
|
15 |
|
$this->typeInstance = $typeInstance; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|