Passed
Push — main ( 31d92c...b21012 )
by Stefan
17:18
created

FormRange::setMinMax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Range slider element
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormRange extends FormInput
14
{
15
    /** @var int value */
16
    protected int $iValue = 0;
17
    /** @var int step */
18
    protected int $iStep = 1;
19
    /** @var int min value */
20
    protected int $iMin = 0;
21
    /** @var int max value */
22
    protected int $iMax = 100;
23
    
24
    /**
25
     * @param string $strName   name of input
26
     * @param string $strWidth  width
27
     * @param int $iMin
28
     * @param int $iMax
29
     * @param int $wFlags       flags (default = 0)
30
     */
31
    public function __construct(string $strName, string $strWidth, int $iMin=0, int $iMax=100, int $wFlags = 0) 
32
    {
33
        parent::__construct($strName, $strWidth, $wFlags);
34
        $this->strType = 'range';
35
        $this->iMin = $iMin;
36
        $this->iMax = $iMax;
37
    }
38
    
39
    /**
40
     * Set min/max value accepted by input field. 
41
     * @param int $iMin - set to null, if no limitation wanted
42
     * @param int $iMax - set to null, if no limitation wanted
43
     */
44
    public function setMinMax(?int $iMin, ?int $iMax) : void 
45
    {
46
        $this->iMin = $iMin ?? $this->iMin;
47
        $this->iMax = $iMax ?? $this->iMax;
48
    }
49
    
50
    /**
51
     * Set step width to be performed by slider.
52
     * @param int $iStep
53
     */
54
    public function setStep(int $iStep) : void
55
    {
56
        $this->iStep = $iStep;
57
    }
58
    
59
    /**
60
     * get value from dataprovider.
61
     * Value have to be retrieved earlier because it may be needed for the value-label
62
     * before the value of the range element is set. 
63
     */
64
    protected function onParentSet() : void
65
    {
66
        $this->iValue = intval($this->oFG->getData()->getValue($this->strName)) ?? $this->iMin;
67
    }
68
    
69
    /**
70
     * {@inheritDoc}
71
     * @see \SKien\Formgenerator\FormElement::buildValue()
72
     */
73
    protected function buildValue() : string
74
    {
75
        $strHTML = ' value="' . $this->iValue . '"';
76
        return $strHTML;
77
    }
78
    
79
    
80
    /**
81
     * Build the HTML-notation for the input element.
82
     * {@inheritDoc}
83
     * @see \SKien\Formgenerator\FormElement::getHTML()
84
     */
85
    public function getHTML() : string
86
    {
87
        $this->addAttribute('step', (string)$this->iStep);
88
        $this->addAttribute('min', (string)$this->iMin);
89
        $this->addAttribute('max', (string)$this->iMax);
90
        $this->addAttribute('autocomplete', 'off');
91
        
92
        $this->processFlags();
93
        $strHTML = $this->buildContainerDiv('display: flex;');
94
        
95
        $this->strID = $this->strID ?: $this->strName;
96
        
97
        if ($this->oFlags->isSet(FormFlags::SHOW_VALUE) && !$this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
98
            $strHTML .= $this->buildValueLabel();
99
        }
100
        $strHTML .= '<div class="slider" style="width: ' . $this->size . ';">';
101
        $strHTML .= '<input';
102
        $strHTML .= ' type="' . $this->strType . '"';
103
        $strHTML .= ' name="' . $this->strName . '"';
104
        $strHTML .= $this->buildClass();
105
        $strHTML .= $this->buildID();
106
        $strHTML .= $this->buildStyle();
107
        $strHTML .= $this->buildTabindex();
108
        $strHTML .= $this->buildAttributes();
109
        $strHTML .= $this->buildValue();
110
        $strHTML .= '>';
111
        $strHTML .= '</div>' . PHP_EOL;
112
        if ($this->oFlags->isSet(FormFlags::SHOW_VALUE) && $this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
113
            $strHTML .= $this->buildValueLabel();
114
        }
115
        $strHTML .= '</div>' . PHP_EOL;
116
        
117
        return $strHTML;
118
    }
119
    
120
    /**
121
     * Build the label to display the actual value selected by the range element.
122
     * @return string
123
     */
124
    protected function buildValueLabel() : string
125
    {
126
        // if min is negative it can be longer than the max value...
127
        $iLen = (strlen((string)$this->iMin) > strlen((string)$this->iMax)) ? strlen((string)$this->iMin) : strlen((string)$this->iMax);
128
        
129
        $strHTML = '<label class="slider_label"';
130
        $strHTML .= ' for="' . $this->strID . '"';
131
        $strHTML .= ' style="width: ' . $iLen . 'em;';
132
        if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
133
            $strHTML .= ' text-align: right;';
134
        }
135
        $strHTML .= '">';
136
        $strHTML .= $this->iValue;
137
        $strHTML .= '</label>' . PHP_EOL;
138
        
139
        return $strHTML;
140
    }
141
}
142