1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Framework\Form |
5
|
|
|
* @author Anton Romanov |
6
|
|
|
* @copyright Copyright (c) 2015-2016, Anton Romanov |
7
|
|
|
* @link http://cadmium-cms.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Form { |
11
|
|
|
|
12
|
|
|
use Dataset, Form, Request, Tag; |
13
|
|
|
|
14
|
|
|
abstract class Field { |
15
|
|
|
|
16
|
|
|
private $form = null, $posted = false; |
17
|
|
|
|
18
|
|
|
protected $key = '', $name = '', $id = '', $value = null; |
19
|
|
|
|
20
|
|
|
protected $config = [], $dataset = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initiazlize field with an owner form, a key, and additional configuration |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
protected function init(Form $form, string $key, array $config = []) { |
27
|
|
|
|
28
|
|
|
$this->form = $form; |
29
|
|
|
|
30
|
|
|
# Set key, name, and id |
31
|
|
|
|
32
|
|
|
if (preg_match(REGEX_FORM_FIELD_KEY, $key)) { |
33
|
|
|
|
34
|
|
|
$prefix = (('' !== $this->form->getName()) ? ($this->form->getName() . '_') : ''); |
35
|
|
|
|
36
|
|
|
$this->key = $key; $this->name = ($prefix . $key); $this->id = str_replace('_', '-', $this->name); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
# Set configuration |
40
|
|
|
|
41
|
|
|
$general = ['disabled' => false, 'required' => false, 'error' => false]; |
42
|
|
|
|
43
|
|
|
$this->dataset = new Dataset(array_merge($general, $this->config)); |
44
|
|
|
|
45
|
|
|
$this->dataset->setArray($config); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get a DOM element with a given name, a value, and a set of attributes |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
protected function getTag(string $name, array $attributes = [], $contents = null) : Tag { |
53
|
|
|
|
54
|
|
|
# Set name and id |
55
|
|
|
|
56
|
|
|
$data = ['name' => $this->name, 'id' => $this->id]; |
57
|
|
|
|
58
|
|
|
# Set appearance |
59
|
|
|
|
60
|
|
|
if ($this->disabled) $data['disabled'] = 'disabled'; |
|
|
|
|
61
|
|
|
|
62
|
|
|
if ($this->required) $data['data-required'] = 'required'; |
|
|
|
|
63
|
|
|
|
64
|
|
|
if ($this->error) $data['data-error'] = 'error'; |
|
|
|
|
65
|
|
|
|
66
|
|
|
# ------------------------ |
67
|
|
|
|
68
|
|
|
return new Tag($name, array_merge($data, $attributes), $contents); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Catch a POST value by the field key |
73
|
|
|
* |
74
|
|
|
* @return true on success or false on failure |
75
|
|
|
*/ |
76
|
|
|
|
77
|
|
|
public function post() : bool { |
78
|
|
|
|
79
|
|
|
if ($this->posted || $this->disabled || ('' === $this->key)) return false; |
|
|
|
|
80
|
|
|
|
81
|
|
|
$this->error = (!$this->setValue(Request::post($this->name)) && $this->required); |
|
|
|
|
82
|
|
|
|
83
|
|
|
# ------------------------ |
84
|
|
|
|
85
|
|
|
return ($this->posted = true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check if a POST value has been catched |
90
|
|
|
*/ |
91
|
|
|
|
92
|
|
|
public function isPosted() : bool { |
93
|
|
|
|
94
|
|
|
return $this->posted; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the field key |
99
|
|
|
*/ |
100
|
|
|
|
101
|
|
|
public function getKey() : string { |
102
|
|
|
|
103
|
|
|
return $this->key; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the field name |
108
|
|
|
*/ |
109
|
|
|
|
110
|
|
|
public function getName() : string { |
111
|
|
|
|
112
|
|
|
return $this->name; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the field id |
117
|
|
|
*/ |
118
|
|
|
|
119
|
|
|
public function getId() : string { |
120
|
|
|
|
121
|
|
|
return $this->id; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the field value |
126
|
|
|
*/ |
127
|
|
|
|
128
|
|
|
public function getValue() { |
129
|
|
|
|
130
|
|
|
return $this->value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set a configuration value |
135
|
|
|
*/ |
136
|
|
|
|
137
|
|
|
public function __set(string $name, $value) { |
138
|
|
|
|
139
|
|
|
$this->dataset->$name = $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get a configuration value |
144
|
|
|
*/ |
145
|
|
|
|
146
|
|
|
public function __get(string $name) { |
147
|
|
|
|
148
|
|
|
return $this->dataset->$name; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check if a configuration value is set |
153
|
|
|
*/ |
154
|
|
|
|
155
|
|
|
public function __isset(string $name) : bool { |
156
|
|
|
|
157
|
|
|
return isset($this->dataset->$name); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.