FormRange   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 63
dl 0
loc 161
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A readAdditionalXML() 0 5 2
A setStep() 0 3 1
B getHTML() 0 33 6
A buildValueLabel() 0 16 3
A __construct() 0 6 1
A setMinMax() 0 4 1
A fromXML() 0 11 1
A onParentSet() 0 4 2
A buildValue() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Range slider element.
8
 *
9
 * @SKienImage FormRange.png
10
 *
11
 * @package Formgenerator
12
 * @author Stefanius <[email protected]>
13
 * @copyright MIT License - see the LICENSE file for details
14
 */
15
class FormRange extends FormInput
16
{
17
    /** @var int value */
18
    protected int $iValue = 0;
19
    /** @var int step */
20
    protected int $iStep = 1;
21
    /** @var int min value */
22
    protected int $iMin = 0;
23
    /** @var int max value */
24
    protected int $iMax = 100;
25
26
    /**
27
     * Create a range slider.
28
     * @param string $strName   name of the Element
29
     * @param string $strWidth  width
30
     * @param int $iMin         min value of the slider
31
     * @param int $iMax         max value of the slider
32
     * @param int $wFlags       any combination of FormFlag constants
33
     */
34
    public function __construct(string $strName, string $strWidth, int $iMin = 0, int $iMax = 100, int $wFlags = 0)
35
    {
36
        parent::__construct($strName, $strWidth, $wFlags);
37
        $this->strType = 'range';
38
        $this->iMin = $iMin;
39
        $this->iMax = $iMax;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     * @see \SKien\Formgenerator\FormElement::fromXML()
45
     * @internal l
46
     */
47
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
48
    {
49
        $strName = self::getAttribString($oXMLElement, 'name');
50
        $strWidth = self::getAttribString($oXMLElement, 'width');
51
        $iMin = self::getAttribInt($oXMLElement, 'min', 0);
52
        $iMax = self::getAttribInt($oXMLElement, 'max', 100);
53
        $wFlags = self::getAttribFlags($oXMLElement);
54
        $oFormElement = new self($strName, $strWidth, $iMin, $iMax, $wFlags);
55
        $oFormParent->add($oFormElement);
56
        $oFormElement->readAdditionalXML($oXMLElement);
57
        return $oFormElement;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
63
     * @internal
64
     */
65
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
66
    {
67
        parent::readAdditionalXML($oXMLElement);
68
        if (self::hasAttrib($oXMLElement, 'step')) {
69
            $this->setStep(self::getAttribInt($oXMLElement, 'step', 1));
70
        }
71
    }
72
73
    /**
74
     * Set min/max value for the slider.
75
     * @param int $iMin
76
     * @param int $iMax
77
     */
78
    public function setMinMax(?int $iMin, ?int $iMax) : void
79
    {
80
        $this->iMin = $iMin ?? $this->iMin;
81
        $this->iMax = $iMax ?? $this->iMax;
82
    }
83
84
    /**
85
     * Set step width to be performed by slider.
86
     * @param int $iStep
87
     */
88
    public function setStep(int $iStep) : void
89
    {
90
        $this->iStep = $iStep;
91
    }
92
93
    /**
94
     * get value from dataprovider.
95
     * Value have to be retrieved earlier because it may be needed for the value-label
96
     * before the value of the range element is set.
97
     */
98
    protected function onParentSet() : void
99
    {
100
        $value = $this->oFG->getData()->getValue($this->strName);
101
        $this->iValue = $value ? intval($value) : $this->iMin;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     * @see \SKien\Formgenerator\FormElement::buildValue()
107
     */
108
    protected function buildValue() : string
109
    {
110
        $strHTML = ' value="' . $this->iValue . '"';
111
        return $strHTML;
112
    }
113
114
115
    /**
116
     * Build the HTML-notation for the input element.
117
     * {@inheritDoc}
118
     * @see \SKien\Formgenerator\FormElement::getHTML()
119
     * @internal
120
     */
121
    public function getHTML() : string
122
    {
123
        $this->addAttribute('step', (string)$this->iStep);
124
        $this->addAttribute('min', (string)$this->iMin);
125
        $this->addAttribute('max', (string)$this->iMax);
126
        $this->addAttribute('autocomplete', 'off');
127
128
        $this->processFlags();
129
        $strHTML = $this->buildContainerDiv('display: flex;');
130
131
        $this->strID = $this->strID ?: $this->strName;
132
133
        if ($this->oFlags->isSet(FormFlags::SHOW_VALUE) && !$this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
134
            $strHTML .= $this->buildValueLabel();
135
        }
136
        $strHTML .= '<div class="slider" style="width: ' . $this->size . ';">';
137
        $strHTML .= '<input';
138
        $strHTML .= ' type="' . $this->strType . '"';
139
        $strHTML .= ' name="' . $this->strName . '"';
140
        $strHTML .= $this->buildClass();
141
        $strHTML .= $this->buildID();
142
        $strHTML .= $this->buildStyle();
143
        $strHTML .= $this->buildTabindex();
144
        $strHTML .= $this->buildAttributes();
145
        $strHTML .= $this->buildValue();
146
        $strHTML .= '>';
147
        $strHTML .= '</div>' . PHP_EOL;
148
        if ($this->oFlags->isSet(FormFlags::SHOW_VALUE) && $this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
149
            $strHTML .= $this->buildValueLabel();
150
        }
151
        $strHTML .= '</div>' . PHP_EOL;
152
153
        return $strHTML;
154
    }
155
156
    /**
157
     * Build the label to display the actual value selected by the range element.
158
     * @return string
159
     */
160
    protected function buildValueLabel() : string
161
    {
162
        // if min is negative it can be longer than the max value...
163
        $iLen = (strlen((string)$this->iMin) > strlen((string)$this->iMax)) ? strlen((string)$this->iMin) : strlen((string)$this->iMax);
164
165
        $strHTML = '<label class="slider_label"';
166
        $strHTML .= ' for="' . $this->strID . '"';
167
        $strHTML .= ' style="width: ' . $iLen . 'em;';
168
        if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
169
            $strHTML .= ' text-align: right;';
170
        }
171
        $strHTML .= '">';
172
        $strHTML .= $this->iValue;
173
        $strHTML .= '</label>' . PHP_EOL;
174
175
        return $strHTML;
176
    }
177
}
178