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