Completed
Push — master ( 3cee74...e04c03 )
by Nikolas
03:25
created

AutoCompleteField::headElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4285
nc 1
cc 1
eloc 6
nop 1
1
<?php
2
namespace rtens\domin\delivery\web\fields;
3
4
use rtens\domin\delivery\web\Element;
5
use rtens\domin\delivery\web\HeadElements;
6
use rtens\domin\delivery\web\WebField;
7
use rtens\domin\Parameter;
8
9
abstract class AutoCompleteField implements WebField {
10
11
    /**
12
     * @param Parameter $parameter
13
     * @return array With captions indexed by values
14
     */
15
    protected abstract function getOptions(Parameter $parameter);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
16
17
    /**
18
     * @param Parameter $parameter
19
     * @param array $serialized
20
     * @return mixed
21
     */
22
    public function inflate(Parameter $parameter, $serialized) {
23
        return $serialized;
24
    }
25
26
    /**
27
     * @param Parameter $parameter
28
     * @param mixed $value
29
     * @return string
30
     */
31
    public function render(Parameter $parameter, $value) {
32
        return $this->renderComboBox($parameter, $value);
33
    }
34
35
    protected function renderComboBox(Parameter $parameter, $value, $fieldNameSuffix = '', $options = []) {
36
        $id = str_replace(['[', ']'], '-', $parameter->getName());
37
38
        $optionsString = json_encode($options);
39
        $children = $this->renderOptions($parameter, (string)$value);
40
        $children[] = new Element('script', [], ["
41
              $(function(){
42
                $('#$id').combobox($optionsString);
43
              });
44
            "]);
45
46
        return (string)new Element('select', [
47
            'name' => $parameter->getName() . $fieldNameSuffix,
48
            'id' => $id,
49
            'class' => 'form-control combobox'
50
        ], $children);
51
    }
52
53
    private function renderOptions(Parameter $parameter, $value) {
54
        $options = [new Element('option', [], [])];
55
        foreach ($this->getOptions($parameter) as $key => $caption) {
56
            $options[] = new Element('option', array_merge([
57
                'value' => $key
58
            ], (string)$key === (string)$value ? [
59
                'selected' => 'selected'
60
            ] : []), [
61
                $caption
62
            ]);
63
        }
64
        return $options;
65
    }
66
67
    /**
68
     * @param Parameter $parameter
69
     * @return array|\rtens\domin\delivery\web\Element[]
70
     */
71 View Code Duplication
    public function headElements(Parameter $parameter) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
72
        return [
73
            HeadElements::jquery(),
74
            HeadElements::style('//cdn.rawgit.com/rtens/bootstrap-combobox/cffe84e7/css/bootstrap-combobox.css'),
75
            HeadElements::script('//cdn.rawgit.com/rtens/bootstrap-combobox/cffe84e7/js/bootstrap-combobox.js'),
76
            new Element('style', [], ['.typeahead-long { width:100% }'])
77
        ];
78
    }
79
}