1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Helper\HTML\Form; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Ffcms\Core\App; |
7
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
8
|
|
|
use Ffcms\Core\Helper\HTML\System\NativeGenerator; |
9
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
10
|
|
|
|
11
|
|
|
class RadioField extends NativeGenerator implements iField |
12
|
|
|
{ |
13
|
|
|
private $properties; |
14
|
|
|
private $name; |
15
|
|
|
private $value; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* RadioField constructor. Pass inside values. |
19
|
|
|
* @param array $properties |
20
|
|
|
* @param string $name |
21
|
|
|
* @param string|null $value |
22
|
|
|
*/ |
23
|
|
|
public function __construct($properties, $name, $value = null) |
24
|
|
|
{ |
25
|
|
|
$this->properties = $properties; |
26
|
|
|
$this->name = $name; |
27
|
|
|
$this->value = $value; |
28
|
|
|
|
29
|
|
|
// set default input type |
30
|
|
|
$this->properties['type'] = 'radio'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Make function of current field type. Return compiled html response |
35
|
|
|
* @return string |
36
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
37
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
38
|
|
|
*/ |
39
|
|
|
public function make() |
40
|
|
|
{ |
41
|
|
|
// get options from properties |
42
|
|
|
$options = $this->properties['options']; |
43
|
|
|
unset($this->properties['options']); |
44
|
|
View Code Duplication |
if (!Obj::isArray($options) || count($options) < 1) { |
|
|
|
|
45
|
|
|
throw new SyntaxException('Radio field ' . self::nohtml($this->name) . ' have no options passed'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// value is not used there |
49
|
|
|
unset($this->properties['value']); |
50
|
|
|
// options is defined as key->value array? |
51
|
|
|
$optionsKey = $this->properties['optionsKey'] === true; |
52
|
|
|
unset($this->properties['optionsKey']); |
53
|
|
|
$build = null; |
54
|
|
|
// build output dom html |
55
|
|
|
foreach ($options as $idx => $value) { |
56
|
|
|
$property = $this->properties; |
57
|
|
View Code Duplication |
if ($optionsKey === true) { // radio button as [value => text_description] - values is a key |
|
|
|
|
58
|
|
|
$property['value'] = $idx; |
59
|
|
|
if ($idx == $this->value) { |
60
|
|
|
$property['checked'] = null; // def boolean attribute html5 |
61
|
|
|
} |
62
|
|
|
} else { // radio button only with [value] data |
63
|
|
|
$property['value'] = $value; |
64
|
|
|
if ($value == $this->value) { |
65
|
|
|
$property['checked'] = null; // def boolean attribute html5 |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// get template and concat avg response |
70
|
|
|
$build .= App::$View->render('native/form/radio_list', [ |
71
|
|
|
'tag' => self::buildSingleTag('input', $property), |
72
|
|
|
'text' => $value, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $build; |
77
|
|
|
} |
78
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.