Completed
Branch EDTR/refactor-fast-api-fetch (385e39)
by
unknown
19:04 queued 10:17
created

GraphQLField::setUseForOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\fields;
4
5
use GraphQL\Type\Definition\ResolveInfo;
6
use LogicException;
7
use WPGraphQL\AppContext;
8
9
/**
10
 * Class GraphQLField
11
 *
12
 * @package       Event Espresso
13
 * @author        Manzoor Wani
14
 */
15
class GraphQLField
16
{
17
18
    /**
19
     * @var string $name
20
     */
21
    protected $name;
22
23
    /**
24
     * @var string|string[] $type
25
     */
26
    protected $type;
27
28
    /**
29
     * @var string|null $key
30
     */
31
    protected $key;
32
33
    /**
34
     * @var string $description
35
     */
36
    protected $description;
37
38
    /**
39
     * @var callable $formatter
40
     */
41
    protected $formatter;
42
43
    /**
44
     * @var callable $resolve
45
     */
46
    protected $resolver;
47
48
    /**
49
     * @var array $caps
50
     */
51
    protected $caps;
52
53
    /**
54
     * @var bool $use_for_input
55
     */
56
    protected $use_for_input;
57
58
    /**
59
     * @var bool $use_for_output
60
     */
61
    protected $use_for_output;
62
63
64
    /**
65
	 * @param string          $name
66
	 * @param string|string[] $type
67
     * @param string|null     $key
68
     * @param string          $description
69
     * @param callable|null   $formatter
70
     * @param callable|null   $resolver
71
     * @param array           $caps
72
     */
73
    public function __construct(
74
		$name,
75
        $type,
76
        $key = null,
77
        $description = '',
78
        callable $formatter = null,
79
        callable $resolver = null,
80
        array $caps = []
81
    ) {
82
        $this->name = $name;
83
        $this->type = $type;
84
        $this->key = $key;
85
        $this->description = $description;
86
        $this->formatter = $formatter;
87
        $this->resolver = $resolver;
88
        $this->caps = $caps;
89
90
        $this->use_for_input = true;
91
        $this->use_for_output = true;
92
    }
93
94
95
    /**
96
     * @return array
97
     */
98
    public function caps()
99
    {
100
        return $this->caps;
101
    }
102
103
104
    /**
105
     * @return string
106
     */
107
    public function description()
108
    {
109
        return $this->description;
110
    }
111
112
113
    /**
114
     * @return string
115
     */
116
    public function key()
117
    {
118
        return $this->key;
119
    }
120
121
122
    /**
123
     * @return string
124
     */
125
    public function name()
126
    {
127
        return $this->name;
128
    }
129
130
131
    /**
132
     * @return string|string[]
133
     */
134
    public function type()
135
    {
136
        return $this->type;
137
    }
138
139
140
    /**
141
     * Convert the field to array to be
142
     * able to pass as config to WP GraphQL
143
     *
144
     * @return array
145
     */
146
    public function toArray()
147
    {
148
        return get_object_vars($this);
149
    }
150
151
152
    /**
153
     * Sets the value for use_for_input.
154
     *
155
	 * @param bool $use_for_input
156
     *
157
     */
158
    protected function setUseForInput($use_for_input)
159
    {
160
        $this->use_for_input = (bool) $use_for_input;
161
    }
162
163
164
    /**
165
     * Whether the field should be used for
166
     * mutation inputs.
167
     *
168
     * @return bool
169
     */
170
    public function useForInput()
171
    {
172
        return (bool) $this->use_for_input;
173
    }
174
175
176
    /**
177
     * Whether the field should be used for
178
     * query outputs.
179
     *
180
     * @return bool
181
     */
182
    public function useForOutput()
183
    {
184
        return (bool) $this->use_for_output;
185
    }
186
187
188
    /**
189
     * Sets the value for use_for_output
190
     *
191
     * @return array
192
     */
193
    protected function setUseForOutput($use_for_output)
194
    {
195
        $this->use_for_output = (bool) $use_for_output;
196
    }
197
198
199
    /**
200
     * Whether the field should resolve
201
     * based on the user caps etc.
202
     *
203
     * @return boolean
204
     */
205
    public function shouldResolve()
206
    {
207
        foreach ($this->caps as $cap) {
208
            if (! current_user_can($cap)) {
209
                return false;
210
            }
211
        }
212
        return true;
213
    }
214
215
216
    /**
217
     * Whether the field has an explicit resolver set.
218
     *
219
     * @return boolean
220
     */
221
    public function hasInternalResolver()
222
    {
223
        return is_callable($this->resolver);
224
    }
225
226
227
    /**
228
     * Whether the field has an explicit resolver set.
229
     *
230
     * @param mixed       $source  The source that's passed down the GraphQL queries
231
     * @param array       $args    The inputArgs on the field
232
     * @param AppContext  $context The AppContext passed down the GraphQL tree
233
     * @param ResolveInfo $info    The ResolveInfo passed down the GraphQL tree
234
     * @return mixed
235
     * @throws LogicException
236
     */
237
    public function resolve($source, array $args, AppContext $context, ResolveInfo $info)
238
    {
239
        if (! $this->hasInternalResolver()) {
240
            throw new LogicException('GraphQLField has no internal resolver.');
241
        }
242
        // dynamic methods using $this don't play nice
243
        // so capture resolver to a single var first
244
        $resolver = $this->resolver;
245
        return $resolver($source, $args, $context, $info);
246
    }
247
248
249
    /**
250
     * Checks if the format callback is set.
251
     * If yes, then uses it to format the value.
252
     *
253
     * @param mixed $value
254
     * @return mixed The formatted value.
255
     */
256
    public function mayBeFormatValue($value)
257
    {
258
        if (is_callable($this->formatter)) {
259
            // dynamic methods using $this don't play nice
260
            // so capture formatter to a single var first
261
            $formatter = $this->formatter;
262
            return $formatter($value);
263
        }
264
        return $value;
265
    }
266
}
267