|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\domain\services\graphql\fields; |
|
3
|
|
|
|
|
4
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
5
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class GraphQLField |
|
10
|
|
|
* |
|
11
|
|
|
* @package Event Espresso |
|
12
|
|
|
* @author Manzoor Wani |
|
13
|
|
|
*/ |
|
14
|
|
|
class GraphQLField |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var mixed $name |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $name; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $name |
|
25
|
|
|
* @param array $config |
|
26
|
|
|
* @throws InvalidArgumentException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($name, array $config = []) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->name = $name; |
|
31
|
|
|
$this->setProps($config); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $config |
|
37
|
|
|
* @throws InvalidArgumentException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setProps(array $config) |
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($config as $key => $value) { |
|
42
|
|
|
$this->{$key} = $value; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the field name. |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function name() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->name; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the model key of the field. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string|null |
|
62
|
|
|
*/ |
|
63
|
|
|
public function key() |
|
64
|
|
|
{ |
|
65
|
|
|
if (isset($this->key)) { |
|
66
|
|
|
return $this->key; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the caps required for the field. |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function caps() |
|
78
|
|
|
{ |
|
79
|
|
|
if (isset($this->caps)) { |
|
80
|
|
|
return (array) $this->caps; |
|
81
|
|
|
} |
|
82
|
|
|
return []; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Whether the field should resolve |
|
88
|
|
|
* based on the user caps etc. |
|
89
|
|
|
* @return boolean |
|
90
|
|
|
*/ |
|
91
|
|
|
public function shouldResolve() |
|
92
|
|
|
{ |
|
93
|
|
|
foreach ($this->caps() as $cap) { |
|
94
|
|
|
if (!current_user_can($cap)) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Whether the field has an explicit resolver set. |
|
104
|
|
|
* @return boolean |
|
105
|
|
|
*/ |
|
106
|
|
|
public function hasInternalResolver() |
|
107
|
|
|
{ |
|
108
|
|
|
return isset($this->resolve) && is_callable($this->resolve); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Checks if the format callback is set. |
|
114
|
|
|
* If yes, then uses it to format the value. |
|
115
|
|
|
* @param mixed $value |
|
116
|
|
|
* @return mixed The formatted value. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function mayBeFormatValue($value) |
|
119
|
|
|
{ |
|
120
|
|
|
if (isset($this->formatCallback) && is_callable($this->formatCallback)) { |
|
121
|
|
|
return call_user_func($this->formatCallback, $value); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
return $value; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Convert the field to array to be |
|
129
|
|
|
* able to pass as config to WP GraphQL |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public function toArray() |
|
133
|
|
|
{ |
|
134
|
|
|
return get_object_vars($this); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: