1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\kissform; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use mindplay\kissform\Facets\FieldInterface; |
7
|
|
|
use mindplay\kissform\Validators\CheckRequired; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Optional base class for mutable input field metadata types. |
11
|
|
|
* |
12
|
|
|
* The getValue() and setValue() methods may be overriden in field types |
13
|
|
|
* and should perform conversion to/from native values, and should throw |
14
|
|
|
* exceptions if invalid input or values is given, because validation is |
15
|
|
|
* assumed to have taken place in advance. The default implementations |
16
|
|
|
* handle strings and can be inherited in string-type fields. |
17
|
|
|
* |
18
|
|
|
* getValue() overrides should throw an {@see UnexpectedValueException} |
19
|
|
|
* if the state of the input model is invalid. |
20
|
|
|
* |
21
|
|
|
* setValue() overrides should throw an {@see InvalidArgumentException} |
22
|
|
|
* if the given value is unacceptable. |
23
|
|
|
*/ |
24
|
|
|
abstract class Field implements FieldInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string field name |
28
|
|
|
*/ |
29
|
|
|
protected $name; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string field label (for labels associated with this input field on a form) |
33
|
|
|
*/ |
34
|
|
|
protected $label; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string field input placeholder value (for input placeholders on forms) |
38
|
|
|
*/ |
39
|
|
|
protected $placeholder; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool true, if this field is required |
43
|
|
|
*/ |
44
|
|
|
protected $required = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name field name |
48
|
|
|
*/ |
49
|
57 |
|
public function __construct($name) |
50
|
|
|
{ |
51
|
57 |
|
$this->name = $name; |
52
|
57 |
|
} |
53
|
|
|
|
54
|
44 |
|
public function getName() |
55
|
|
|
{ |
56
|
44 |
|
return $this->name; |
57
|
|
|
} |
58
|
|
|
|
59
|
17 |
|
public function getLabel() |
60
|
|
|
{ |
61
|
17 |
|
return $this->label; |
62
|
|
|
} |
63
|
|
|
|
64
|
12 |
|
public function getPlaceholder() |
65
|
|
|
{ |
66
|
12 |
|
return $this->placeholder; |
67
|
|
|
} |
68
|
|
|
|
69
|
24 |
|
public function isRequired() |
70
|
|
|
{ |
71
|
24 |
|
return $this->required; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getValue(InputModel $model) |
75
|
|
|
{ |
76
|
|
|
return $model->getInput($this); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
10 |
|
public function createValidators() |
80
|
|
|
{ |
81
|
10 |
|
return $this->isRequired() |
82
|
9 |
|
? [new CheckRequired()] |
83
|
10 |
|
: []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $label display label |
88
|
|
|
*/ |
89
|
18 |
|
public function setLabel($label) |
90
|
|
|
{ |
91
|
18 |
|
$this->label = $label; |
92
|
18 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $placeholder placeholder label |
96
|
|
|
*/ |
97
|
1 |
|
public function setPlaceholder($placeholder) |
98
|
|
|
{ |
99
|
1 |
|
$this->placeholder = $placeholder; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param bool $required TRUE, if input is required for this Field; FALSE, if it's optional |
104
|
|
|
*/ |
105
|
15 |
|
public function setRequired($required = true) |
106
|
|
|
{ |
107
|
15 |
|
$this->required = (bool) $required; |
108
|
15 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param InputModel $model |
112
|
|
|
* @param string|null $value |
113
|
|
|
* |
114
|
|
|
* @return void |
115
|
|
|
* |
116
|
|
|
* @throws InvalidArgumentException if the given value is unacceptable. |
117
|
|
|
*/ |
118
|
8 |
|
public function setValue(InputModel $model, $value) |
119
|
|
|
{ |
120
|
8 |
|
if (is_scalar($value)) { |
121
|
8 |
|
$model->setInput($this, (string) $value); |
122
|
1 |
|
} elseif ($value === null) { |
123
|
1 |
|
$model->setInput($this, null); |
124
|
|
|
} else { |
125
|
|
|
throw new InvalidArgumentException("string expected"); |
126
|
|
|
} |
127
|
8 |
|
} |
128
|
|
|
} |
129
|
|
|
|