Completed
Push — master ( 0eb5d9...ce204c )
by David
14s
created

SourceField::getFailWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Annotations;
5
6
/**
7
 * SourceFields are fields that are directly source from the base object into GraphQL.
8
 *
9
 * @Annotation
10
 * @Target({"CLASS"})
11
 * @Attributes({
12
 *   @Attribute("name", type = "string"),
13
 *   @Attribute("logged", type = "bool"),
14
 *   @Attribute("right", type = "TheCodingMachine\GraphQL\Controllers\Annotations\Right"),
15
 *   @Attribute("outputType", type = "string"),
16
 *   @Attribute("isId", type = "bool"),
17
 *   @Attribute("failWith", type = "mixed"),
18
 * })
19
 *
20
 * FIXME: remove idId since outputType="ID" is equivalent
21
 */
22
class SourceField implements SourceFieldInterface
23
{
24
    /**
25
     * @var Right|null
26
     */
27
    private $right;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $name;
33
34
    /**
35
     * @var bool
36
     */
37
    private $logged;
38
39
    /**
40
     * @var string|null
41
     */
42
    private $outputType;
43
44
    /**
45
     * @var bool
46
     */
47
    private $id;
48
49
    /**
50
     * The default value to use if the right is not enforced.
51
     *
52
     * @var mixed
53
     */
54
    private $failWith;
55
56
    /**
57
     * @var bool
58
     */
59
    private $hasFailWith = false;
60
61
    /**
62
     * @param mixed[] $attributes
63
     */
64
    public function __construct(array $attributes = [])
65
    {
66
        $this->name = $attributes['name'] ?? null;
67
        $this->logged = $attributes['logged'] ?? false;
68
        $this->right = $attributes['right'] ?? null;
69
        $this->outputType = $attributes['outputType'] ?? null;
70
        $this->id = $attributes['isId'] ?? false;
71
        if (array_key_exists('failWith', $attributes)) {
72
            $this->failWith = $attributes['failWith'];
73
            $this->hasFailWith = true;
74
        }
75
    }
76
77
    /**
78
     * Returns the GraphQL right to be applied to this source field.
79
     *
80
     * @return Right|null
81
     */
82
    public function getRight(): ?Right
83
    {
84
        return $this->right;
85
    }
86
87
    /**
88
     * Returns the name of the GraphQL query/mutation/field.
89
     * If not specified, the name of the method should be used instead.
90
     *
91
     * @return null|string
92
     */
93
    public function getName(): ?string
94
    {
95
        return $this->name;
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    public function isLogged(): bool
102
    {
103
        return $this->logged;
104
    }
105
106
    /**
107
     * Returns the GraphQL return type of the request (as a string).
108
     * The string can represent the FQCN of the type or an entry in the container resolving to the GraphQL type.
109
     *
110
     * @return string|null
111
     */
112
    public function getOutputType(): ?string
113
    {
114
        return $this->outputType;
115
    }
116
117
    /**
118
     * If the GraphQL type is "ID", isID will return true.
119
     *
120
     * @return bool
121
     */
122
    public function isId(): bool
123
    {
124
        return $this->id;
125
    }
126
127
    /**
128
     * Returns the default value to use if the right is not enforced.
129
     *
130
     * @return mixed
131
     */
132
    public function getFailWith()
133
    {
134
        return $this->failWith;
135
    }
136
137
    /**
138
     * True if a default value is available if a right is not enforced.
139
     *
140
     * @return bool
141
     */
142
    public function canFailWith(): bool
143
    {
144
        return $this->hasFailWith;
145
    }
146
}
147