|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Formularium; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Exception\Exception; |
|
6
|
|
|
|
|
7
|
|
|
class Field |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $name; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Datatype |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $datatype; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $extensions; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $validators; |
|
28
|
|
|
|
|
29
|
7 |
|
public static function getFromData(string $name, array $data) : Field |
|
30
|
|
|
{ |
|
31
|
7 |
|
if (!$name) { |
|
32
|
1 |
|
throw new Exception("Missing name in fields"); |
|
33
|
|
|
} |
|
34
|
6 |
|
if (!array_key_exists('datatype', $data)) { |
|
35
|
1 |
|
throw new Exception("Missing type in field data for $name"); |
|
36
|
|
|
} |
|
37
|
5 |
|
return new Field($name, $data['datatype'], $data['extensions'] ?? [], $data['validators'] ?? []); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Undocumented function |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $name |
|
44
|
|
|
* @param string|Datatype $datatype |
|
45
|
|
|
* @param array $extensions |
|
46
|
|
|
* @param array $validators |
|
47
|
|
|
*/ |
|
48
|
73 |
|
public function __construct(string $name, $datatype, array $extensions = [], array $validators = []) |
|
49
|
|
|
{ |
|
50
|
73 |
|
$this->name = $name; |
|
51
|
73 |
|
if ($datatype instanceof Datatype) { |
|
52
|
68 |
|
$this->datatype = $datatype; |
|
53
|
|
|
} else { |
|
54
|
5 |
|
$this->datatype = Datatype::factory($datatype); |
|
55
|
|
|
} |
|
56
|
71 |
|
$this->extensions = $extensions; |
|
57
|
71 |
|
$this->validators = $validators; |
|
58
|
71 |
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function getName(): string |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->name; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
public function getDatatype(): Datatype |
|
66
|
|
|
{ |
|
67
|
2 |
|
return $this->datatype; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
30 |
|
public function getValidators(): array |
|
71
|
|
|
{ |
|
72
|
30 |
|
return $this->validators; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
/** |
|
76
|
|
|
* @param string $name |
|
77
|
1 |
|
* @param mixed $default |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getValidator(string $name, $default) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->validators[$name] ?? $default; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function getExtensions(): array |
|
86
|
|
|
{ |
|
87
|
1 |
|
return $this->extensions; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $name |
|
92
|
|
|
* @param mixed $default |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getExtension(string $name, $default) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->extensions[$name] ?? $default; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|