Traits
2 weeks ago
BasicField.php
2 weeks ago
ButtonField.php
2 weeks ago
CheckboxField.php
2 weeks ago
DateField.php
2 weeks ago
DatePickerField.php
2 weeks ago
Header.php
2 weeks ago
HiddenField.php
2 weeks ago
ImageInputField.php
2 weeks ago
InputNumberField.php
2 weeks ago
InputTextField.php
2 weeks ago
MultipleInputTextField.php
2 weeks ago
NoOnceField.php
2 weeks ago
NoValueField.php
2 weeks ago
Paragraph.php
2 weeks ago
ProductSelect.php
2 weeks ago
RadioField.php
2 weeks ago
SelectField.php
2 weeks ago
SubmitField.php
2 weeks ago
TextAreaField.php
2 weeks ago
TimepickerField.php
2 weeks ago
WooSelect.php
2 weeks ago
WyswigField.php
2 weeks ago
BasicField.php
284 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Forms\Field; |
| 4 | |
| 5 | use FSVendor\WPDesk\Forms\Field; |
| 6 | use FSVendor\WPDesk\Forms\Sanitizer\NoSanitize; |
| 7 | use FSVendor\WPDesk\Forms\Serializer; |
| 8 | use FSVendor\WPDesk\Forms\Serializer\NoSerialize; |
| 9 | use FSVendor\WPDesk\Forms\Validator\ChainValidator; |
| 10 | use FSVendor\WPDesk\Forms\Validator\RequiredValidator; |
| 11 | /** |
| 12 | * Base class for fields. Is responsible for settings all required field values and provides standard implementation for |
| 13 | * the field interface. |
| 14 | * |
| 15 | * @package WPDesk\Forms |
| 16 | */ |
| 17 | abstract class BasicField implements Field |
| 18 | { |
| 19 | use Field\Traits\HtmlAttributes; |
| 20 | /** @var array[] */ |
| 21 | protected $meta; |
| 22 | protected $default_value; |
| 23 | public function __construct() |
| 24 | { |
| 25 | $this->meta['class'] = []; |
| 26 | } |
| 27 | public function get_label() |
| 28 | { |
| 29 | return $this->meta['label']; |
| 30 | } |
| 31 | /** |
| 32 | * @param string $value |
| 33 | * |
| 34 | * @return $this |
| 35 | */ |
| 36 | public function set_label($value) |
| 37 | { |
| 38 | $this->meta['label'] = $value; |
| 39 | return $this; |
| 40 | } |
| 41 | public function get_description_tip() |
| 42 | { |
| 43 | return $this->meta['description_tip']; |
| 44 | } |
| 45 | public function has_description_tip() |
| 46 | { |
| 47 | return isset($this->meta['description_tip']); |
| 48 | } |
| 49 | public function should_override_form_template() |
| 50 | { |
| 51 | return isset($this->attributes['overrite_template']) ? $this->attributes['overrite_template'] : \false; |
| 52 | } |
| 53 | public function get_description() |
| 54 | { |
| 55 | return $this->meta['description']; |
| 56 | } |
| 57 | public function has_label() |
| 58 | { |
| 59 | return isset($this->meta['label']); |
| 60 | } |
| 61 | public function has_description() |
| 62 | { |
| 63 | return isset($this->meta['description']); |
| 64 | } |
| 65 | public function set_description($value) |
| 66 | { |
| 67 | $this->meta['description'] = $value; |
| 68 | return $this; |
| 69 | } |
| 70 | public function set_description_tip($value) |
| 71 | { |
| 72 | $this->meta['description_tip'] = $value; |
| 73 | return $this; |
| 74 | } |
| 75 | /** |
| 76 | * @return array |
| 77 | * |
| 78 | * @deprecated not sure if needed. TODO: Check later. |
| 79 | */ |
| 80 | public function get_type() |
| 81 | { |
| 82 | return $this->attributes['type']; |
| 83 | } |
| 84 | /** |
| 85 | * @param string $value |
| 86 | * |
| 87 | * @return $this |
| 88 | */ |
| 89 | public function set_placeholder($value) |
| 90 | { |
| 91 | $this->meta['placeholder'] = $value; |
| 92 | return $this; |
| 93 | } |
| 94 | public function has_placeholder() |
| 95 | { |
| 96 | return isset($this->meta['placeholder']); |
| 97 | } |
| 98 | public function get_placeholder() |
| 99 | { |
| 100 | return $this->meta['placeholder']; |
| 101 | } |
| 102 | /** |
| 103 | * @param string $name |
| 104 | * |
| 105 | * @return $this |
| 106 | */ |
| 107 | public function set_name($name) |
| 108 | { |
| 109 | $this->attributes['name'] = $name; |
| 110 | return $this; |
| 111 | } |
| 112 | public function get_meta_value($name) |
| 113 | { |
| 114 | return $this->meta[$name]; |
| 115 | } |
| 116 | public function get_classes() |
| 117 | { |
| 118 | return implode(' ', $this->meta['class']); |
| 119 | } |
| 120 | public function has_classes() |
| 121 | { |
| 122 | return !empty($this->meta['class']); |
| 123 | } |
| 124 | public function has_data() |
| 125 | { |
| 126 | return !empty($this->meta['data']); |
| 127 | } |
| 128 | /** |
| 129 | * @return array |
| 130 | */ |
| 131 | public function get_data() |
| 132 | { |
| 133 | return empty($this->meta['data']) ? [] : $this->meta['data']; |
| 134 | } |
| 135 | public function get_possible_values() |
| 136 | { |
| 137 | return isset($this->meta['possible_values']) ? $this->meta['possible_values'] : []; |
| 138 | } |
| 139 | public function get_id() |
| 140 | { |
| 141 | return isset($this->attributes['id']) ? $this->attributes['id'] : sanitize_title($this->get_name()); |
| 142 | } |
| 143 | public function get_name() |
| 144 | { |
| 145 | return $this->attributes['name']; |
| 146 | } |
| 147 | public function is_multiple() |
| 148 | { |
| 149 | return isset($this->attributes['multiple']) ? $this->attributes['multiple'] : \false; |
| 150 | } |
| 151 | /** |
| 152 | * @return $this |
| 153 | */ |
| 154 | public function set_disabled() |
| 155 | { |
| 156 | $this->attributes['disabled'] = \true; |
| 157 | return $this; |
| 158 | } |
| 159 | public function is_disabled() |
| 160 | { |
| 161 | return isset($this->attributes['disabled']) ? $this->attributes['disabled'] : \false; |
| 162 | } |
| 163 | /** |
| 164 | * @return $this |
| 165 | */ |
| 166 | public function set_readonly() |
| 167 | { |
| 168 | $this->attributes['readonly'] = \true; |
| 169 | return $this; |
| 170 | } |
| 171 | public function is_readonly() |
| 172 | { |
| 173 | return isset($this->attributes['readonly']) ? $this->attributes['readonly'] : \false; |
| 174 | } |
| 175 | /** |
| 176 | * @return $this |
| 177 | */ |
| 178 | public function set_required() |
| 179 | { |
| 180 | $this->meta['required'] = \true; |
| 181 | return $this; |
| 182 | } |
| 183 | /** |
| 184 | * @param string $class_name |
| 185 | * |
| 186 | * @return $this |
| 187 | */ |
| 188 | public function add_class($class_name) |
| 189 | { |
| 190 | $this->meta['class'][$class_name] = $class_name; |
| 191 | return $this; |
| 192 | } |
| 193 | /** |
| 194 | * @param string $class_name |
| 195 | * |
| 196 | * @return $this |
| 197 | */ |
| 198 | public function unset_class($class_name) |
| 199 | { |
| 200 | unset($this->meta['class'][$class_name]); |
| 201 | return $this; |
| 202 | } |
| 203 | /** |
| 204 | * @param string $data_name |
| 205 | * @param string $data_value |
| 206 | * |
| 207 | * @return $this |
| 208 | */ |
| 209 | public function add_data($data_name, $data_value) |
| 210 | { |
| 211 | if (!isset($this->meta['data'])) { |
| 212 | $this->meta['data'] = []; |
| 213 | } |
| 214 | $this->meta['data'][$data_name] = $data_value; |
| 215 | return $this; |
| 216 | } |
| 217 | /** |
| 218 | * @param string $data_name |
| 219 | * |
| 220 | * @return $this |
| 221 | */ |
| 222 | public function unset_data($data_name) |
| 223 | { |
| 224 | unset($this->meta['data'][$data_name]); |
| 225 | return $this; |
| 226 | } |
| 227 | public function is_meta_value_set($name) |
| 228 | { |
| 229 | return isset($this->meta[$name]); |
| 230 | } |
| 231 | public function is_class_set($name) |
| 232 | { |
| 233 | return isset($this->meta['class'][$name]); |
| 234 | } |
| 235 | public function get_default_value() |
| 236 | { |
| 237 | return $this->default_value; |
| 238 | } |
| 239 | /** |
| 240 | * @param string $value |
| 241 | * |
| 242 | * @return $this |
| 243 | */ |
| 244 | public function set_default_value($value) |
| 245 | { |
| 246 | $this->default_value = $value; |
| 247 | return $this; |
| 248 | } |
| 249 | /** |
| 250 | * @return ChainValidator |
| 251 | */ |
| 252 | public function get_validator() |
| 253 | { |
| 254 | $chain = new ChainValidator(); |
| 255 | if ($this->is_required()) { |
| 256 | $chain->attach(new RequiredValidator()); |
| 257 | } |
| 258 | return $chain; |
| 259 | } |
| 260 | public function is_required() |
| 261 | { |
| 262 | return isset($this->meta['required']) ? $this->meta['required'] : \false; |
| 263 | } |
| 264 | public function get_sanitizer() |
| 265 | { |
| 266 | return new NoSanitize(); |
| 267 | } |
| 268 | /** |
| 269 | * @return Serializer |
| 270 | */ |
| 271 | public function get_serializer() |
| 272 | { |
| 273 | if (isset($this->meta['serializer']) && $this->meta['serializer'] instanceof Serializer) { |
| 274 | return $this->meta['serializer']; |
| 275 | } |
| 276 | return new NoSerialize(); |
| 277 | } |
| 278 | public function set_serializer(Serializer $serializer) |
| 279 | { |
| 280 | $this->meta['serializer'] = $serializer; |
| 281 | return $this; |
| 282 | } |
| 283 | } |
| 284 |