1
|
|
|
<?php |
2
|
|
|
namespace Ffcms\Core\Helper\HTML\Form; |
3
|
|
|
|
4
|
|
|
use Ffcms\Core\Helper\HTML\System\NativeGenerator; |
5
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
6
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
7
|
|
|
|
8
|
|
|
class SelectField extends NativeGenerator implements iField |
9
|
|
|
{ |
10
|
|
|
private $properties; |
11
|
|
|
private $name; |
12
|
|
|
private $value; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* SelectField constructor. Pass arguments inside model |
16
|
|
|
* @param array $properties |
17
|
|
|
* @param string $name |
18
|
|
|
* @param string|null $value |
19
|
|
|
*/ |
20
|
|
|
public function __construct($properties, $name, $value = null) |
21
|
|
|
{ |
22
|
|
|
$this->properties = $properties; |
23
|
|
|
$this->name = $name; |
24
|
|
|
$this->value = $value; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Build <select {$properties} >[<options>]</select> response |
29
|
|
|
* {@inheritDoc} |
30
|
|
|
* @see \Ffcms\Core\Helper\HTML\Form\iField::make() |
31
|
|
|
*/ |
32
|
|
|
public function make() |
33
|
|
|
{ |
34
|
|
|
// get options from properties |
35
|
|
|
$options = $this->properties['options']; |
36
|
|
|
unset($this->properties['options']); |
37
|
|
|
if (!Obj::isArray($options) || count($options) < 1) { |
38
|
|
|
throw new SyntaxException('Select field ' . self::nohtml($this->name) . ' have no options passed'); |
39
|
|
|
} |
40
|
|
|
// value is not used there |
41
|
|
|
unset($this->properties['value']); |
42
|
|
|
// options is defined as key->value array? |
43
|
|
|
$optionsKey = $this->properties['optionsKey'] === true; |
44
|
|
|
unset($this->properties['optionsKey']); |
45
|
|
|
$buildOpt = null; |
46
|
|
|
foreach ($options as $optIdx => $opt) { |
47
|
|
|
$optionProperty = []; |
48
|
|
|
if ($optionsKey === true) { // options with value => text |
49
|
|
|
$optionProperty['value'] = $optIdx; |
50
|
|
|
if ($optIdx == $this->value) { |
51
|
|
|
$optionProperty['selected'] = null; // def boolean attribute html5 |
52
|
|
|
} |
53
|
|
|
} else { // only value option |
54
|
|
|
if ($opt == $this->value) { |
55
|
|
|
$optionProperty['selected'] = null; // def boolean attribute html5 |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$buildOpt .= self::buildContainerTag('option', $optionProperty, $opt); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// return compiled DOM |
62
|
|
|
return self::buildContainerTag('select', $this->properties, $buildOpt, true); |
63
|
|
|
} |
64
|
|
|
} |