Completed
Push — feature/update_flight ( 289873 )
by Laurent
18:14
created

SimpleFormRenderer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 12 3
A openingForm() 0 4 1
A closingForm() 0 4 1
A renderElement() 0 13 3
A formatOptions() 0 17 4
A renderSelectElement() 0 15 4
1
<?php
2
/**
3
 *
4
 */
5
6
namespace flightlog\form;
7
8
/**
9
 * @author Laurent De Coninck <[email protected]>
10
 */
11
class SimpleFormRenderer
12
{
13
    /**
14
     * @inheritDoc
15
     */
16
    public function render($element)
17
    {
18
        if ($element instanceof FormInterface) {
19
            return $this->openingForm($element);
20
        }
21
22
        if ($element instanceof FormElementInterface) {
23
            return $this->renderElement($element);
24
        }
25
26
        throw new \InvalidArgumentException('unsupported type');
27
    }
28
29
    /**
30
     * @param FormInterface $form
31
     *
32
     * @return string
33
     */
34
    public function openingForm(FormInterface $form)
35
    {
36
        return sprintf('<form name="%s" method="%s">', $form->getName(), $form->getMethod());
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function closingForm()
43
    {
44
        return '</form>';
45
    }
46
47
    /**
48
     * @param FormElementInterface $element
49
     *
50
     * @return string
51
     */
52
    private function renderElement(FormElementInterface $element)
53
    {
54
        switch($element->getType()){
55
            case FormElementInterface::TYPE_TEXTAREA:
56
                return sprintf('<textarea name="%s" %s>%s</textarea>', $element->getName(), $this->formatOptions($element->getOptions()), $element->getValue());
57
58
            case FormElementInterface::TYPE_SELECT:
59
                return $this->renderSelectElement($element);
0 ignored issues
show
Compatibility introduced by
$element of type object<flightlog\form\FormElementInterface> is not a sub-type of object<flightlog\form\Select>. It seems like you assume a concrete implementation of the interface flightlog\form\FormElementInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
61
            default:
62
                return sprintf('<input type="%s" name="%s" value="%s" %s />', $element->getType(), $element->getName(), $element->getValue(), $this->formatOptions($element->getOptions()));
63
        }
64
    }
65
66
    /**
67
     * Format the attributes options of the element.
68
     *
69
     * @param array $options
70
     *
71
     * @return string
72
     */
73
    private function formatOptions($options)
74
    {
75
        if (!isset($options['attr'])) {
76
            return '';
77
        }
78
79
        if (!is_array($options['attr'])) {
80
            return $options['attr'];
81
        }
82
83
        $attributes = '';
84
        foreach ($options['attr'] as $attributeKey => $attributeValue) {
85
            $attributes .= sprintf(' %s = %s', $attributeKey, $attributeValue);
86
        }
87
88
        return $attributes;
89
    }
90
91
    /**
92
     * @param Select $element
93
     *
94
     * @return string
95
     */
96
    private function renderSelectElement(Select $element)
97
    {
98
        $selectElement = sprintf('<select name="%s" >', $element->getName());
99
100
        if($element->getValueOptions()){
101
            foreach($element->getValueOptions() as $optionValue => $optionLabel){
102
                $selectedAttribute = $optionValue === $element->getValue() ? 'selected' : '';
103
                $selectElement .= sprintf('<option value="%s" %s >%s</option>', $optionValue, $selectedAttribute, $optionLabel);
104
            }
105
        }
106
107
        $selectElement.='</select>';
108
109
        return $selectElement;
110
    }
111
112
113
}