1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Formularium; |
4
|
|
|
|
5
|
|
|
use Formularium\Exception\Exception; |
6
|
|
|
use Formularium\Factory\DatatypeFactory; |
7
|
|
|
|
8
|
|
|
class Field |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $name; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Datatype |
17
|
|
|
*/ |
18
|
|
|
protected $datatype; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $renderable; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $validators; |
29
|
|
|
|
30
|
52 |
|
/** |
31
|
|
|
* @var array |
32
|
52 |
|
*/ |
33
|
1 |
|
protected $metadata; |
34
|
|
|
|
35
|
51 |
|
public static function getFromData(string $name, array $data) : Field |
36
|
1 |
|
{ |
37
|
|
|
if (!$name) { |
38
|
50 |
|
throw new Exception("Missing name in fields"); |
39
|
|
|
} |
40
|
|
|
if (!array_key_exists('datatype', $data)) { |
41
|
|
|
throw new Exception("Missing type in field data for $name"); |
42
|
|
|
} |
43
|
|
|
return new Field($name, $data['datatype'], $data['renderable'] ?? [], $data['validators'] ?? [], $data['metadata'] ?? []); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
178 |
|
* @param string $name |
48
|
|
|
* @param string|Datatype $datatype |
49
|
178 |
|
* @param array $renderable |
50
|
178 |
|
* @param array $validators |
51
|
128 |
|
* @param array $metadata |
52
|
|
|
*/ |
53
|
50 |
|
public function __construct(string $name, $datatype, array $renderable = [], array $validators = [], array $metadata = []) |
54
|
|
|
{ |
55
|
176 |
|
$this->name = $name; |
56
|
176 |
|
if ($datatype instanceof Datatype) { |
57
|
176 |
|
$this->datatype = $datatype; |
58
|
47 |
|
} else { |
59
|
|
|
$this->datatype = DatatypeFactory::factory($datatype); |
60
|
|
|
} |
61
|
|
|
$this->renderable = $renderable; |
62
|
176 |
|
$this->validators = $validators; |
63
|
|
|
$this->metadata = $metadata; |
64
|
|
|
foreach ($this->validators as $name => $data) { |
65
|
|
|
if (!is_array($data)) { |
66
|
|
|
throw new Exception("Validator data for $name must be an array"); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
* @param string|Datatype $datatype |
74
|
|
|
* @param array $renderable |
75
|
|
|
* @param array $validators |
76
|
8 |
|
* @return self |
77
|
|
|
*/ |
78
|
8 |
|
public static function create(string $name, $datatype, array $renderable = [], array $validators = [], array $metadata = []): self |
79
|
|
|
{ |
80
|
|
|
return new self($name, $datatype, $renderable, $validators, $metadata); |
81
|
13 |
|
} |
82
|
|
|
|
83
|
13 |
|
public function getName(): string |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
10 |
|
} |
87
|
|
|
|
88
|
10 |
|
public function getDatatype(): Datatype |
89
|
|
|
{ |
90
|
|
|
return $this->datatype; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getValidators(): array |
94
|
|
|
{ |
95
|
|
|
return $this->validators; |
96
|
34 |
|
} |
97
|
|
|
|
98
|
34 |
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
* @param mixed $default |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
public function getValidator(string $name, $default = []) |
104
|
|
|
{ |
105
|
|
|
return $this->validators[$name] ?? $default; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get option value from a validator. |
110
|
|
|
* |
111
|
|
|
* @param string $validator The validator name. |
112
|
|
|
* @param string $option The validation option. |
113
|
|
|
* @param mixed $default The default value. |
114
|
|
|
* @return mixed The option value or the default value if there is none. |
115
|
|
|
*/ |
116
|
|
|
public function getValidatorOption(string $validator, string $option = 'value', $default = null) |
117
|
|
|
{ |
118
|
|
|
return $this->validators[$validator][$option] ?? $default; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sets an option value |
123
|
|
|
* |
124
|
|
|
* @param string $validator |
125
|
|
|
* @param string $option |
126
|
|
|
* @param mixed $value |
127
|
|
|
* @return self |
128
|
3 |
|
*/ |
129
|
|
|
public function setValidatorOption(string $validator, string $option, $value): self |
130
|
3 |
|
{ |
131
|
|
|
$this->validators[$validator][$option] = $value; |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getRenderables(): array |
136
|
|
|
{ |
137
|
|
|
return $this->renderable; |
138
|
2 |
|
} |
139
|
|
|
|
140
|
2 |
|
/** |
141
|
|
|
* @param string $name |
142
|
|
|
* @param mixed $default |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
public function getRenderable(string $name, $default) |
146
|
|
|
{ |
147
|
|
|
return $this->renderable[$name] ?? $default; |
148
|
1 |
|
} |
149
|
|
|
|
150
|
1 |
|
public function getMetadata(): array |
151
|
|
|
{ |
152
|
1 |
|
return $this->metadata; |
153
|
1 |
|
} |
154
|
1 |
|
|
155
|
|
|
/** |
156
|
1 |
|
* @param string $name |
157
|
1 |
|
* @param mixed $default |
158
|
1 |
|
* @return mixed |
159
|
1 |
|
*/ |
160
|
|
|
public function getMetadataValue(string $name, $default) |
161
|
|
|
{ |
162
|
1 |
|
return $this->metadata[$name] ?? $default; |
163
|
1 |
|
} |
164
|
|
|
|
165
|
1 |
|
/** |
166
|
1 |
|
* Sets an option value |
167
|
|
|
* |
168
|
|
|
* @param string $name |
169
|
|
|
* @param mixed $value |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
|
|
public function setMetadataValue(string $name, $value): self |
173
|
|
|
{ |
174
|
|
|
$this->metaata[$name] = $value; |
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function toGraphqlQuery(): string |
179
|
|
|
{ |
180
|
|
|
return $this->datatype->getGraphqlField($this->getName()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function toGraphqlTypeDefinition(): string |
184
|
|
|
{ |
185
|
|
|
$renderable = array_map( |
186
|
|
|
function ($name, $value) { |
187
|
|
|
$v = $value; |
188
|
|
|
if (is_string($value)) { |
189
|
|
|
$v = '"' . str_replace('"', '\\"', $value) . '"'; |
190
|
|
|
} |
191
|
|
|
return ' ' . $name . ': ' . $v; |
192
|
|
|
}, |
193
|
|
|
array_keys($this->renderable), |
194
|
|
|
$this->renderable |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
return $this->getName() . ': ' . $this->datatype->getGraphqlType() . |
198
|
|
|
($this->getValidator(Datatype::REQUIRED, false) ? '' : '!') . |
199
|
|
|
// TODO: validators |
200
|
|
|
($this->renderable ? " @renderable(\n" . join("\n", $renderable) . "\n)" : '') . |
201
|
|
|
"\n"; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function toArray(): array |
205
|
|
|
{ |
206
|
|
|
return [ |
207
|
|
|
'name' => $this->name, |
208
|
|
|
'datatype' => $this->datatype->getName(), |
209
|
|
|
'validators' => $this->validators, |
210
|
|
|
'renderable' => $this->renderable, |
211
|
|
|
'metadata' => $this->metadata, |
212
|
|
|
]; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|