FormMeter   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 61
c 2
b 1
f 0
dl 0
loc 165
rs 10
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromXML() 0 11 1
A readAdditionalXML() 0 11 4
A setMessureRange() 0 5 1
B getHTML() 0 32 7
A buildValue() 0 4 1
A onParentSet() 0 4 2
A setMinMax() 0 4 1
A buildTitle() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Meter element.
8
 *
9
 * The meter element represents a scalar measurement within a known range
10
 * (defined by min/max), or a fractional value (also known as a gauge). <br/>
11
 * The element is particularly suitable when it is desired that the color
12
 * display should change dependent on certain boundaries.
13
 *
14
 * > The meter element should not be used to indicate progress (as in a progress bar). Use
15
 * > a progress element instead.
16
 * > The meter element also does not represent a scalar value of arbitrary range unless
17
 * > at least a maximum is known.
18
 * >
19
 * > Examples:
20
 * > - disk usage
21
 * > - temperature within defined boundaries
22
 * > - relevance of a query result
23
 * > - fraction of a voting population to have selected a particular candidate.
24
 *
25
 * In addition to the min / max range, a lower and upper bound (low/high) and an optimum
26
 * value for checking and color coding can optionally be specified.
27
 *
28
 * <b>Usage without optimum  value:  </b><br/>
29
 * If the value to be displayed is below the lower or above the upper bound, the output
30
 * is in a different color than within the bounds.
31
 *
32
 * <b>Usage including optimum  value:  </b><br/>
33
 * This means that the color display changes between three categories (bad / middle /
34
 * good), depending on the value.
35
 * 1. *bad-middle-good*, if the optimum value is between the min and low bound.
36
 * 2. *good-middle-bad*, if the optimum value is between the high and max bound.
37
 *
38
 * @SKienImage FormMeter.png
39
 *
40
 * @package Formgenerator
41
 * @author Stefanius <[email protected]>
42
 * @copyright MIT License - see the LICENSE file for details
43
 */
44
class FormMeter extends FormInput
45
{
46
    /** default min value */
47
    protected const MIN_DEFAULT = 0;
48
    /** default max value */
49
    protected const MAX_DEFAULT = 1.0;
50
51
    /** @var float value */
52
    protected float $fltValue = 0;
53
    /** @var float min value */
54
    protected float $fltMin;
55
    /** @var float max value */
56
    protected float $fltMax;
57
    /** @var float low value */
58
    protected ?float $fltLow = null;
59
    /** @var float high value */
60
    protected ?float $fltHigh = null;
61
    /** @var float optimum value */
62
    protected ?float $fltOpt = null;
63
64
    /**
65
     * Create a range slider.
66
     * @param string $strName   name of the Element
67
     * @param string $strWidth  width
68
     * @param float $fltMin     min value
69
     * @param float $fltMax     max value
70
     * @param int $wFlags       any combination of FormFlag constants
71
     */
72
    public function __construct(string $strName, string $strWidth, float $fltMin = self::MIN_DEFAULT, float $fltMax = self::MAX_DEFAULT, int $wFlags = 0)
73
    {
74
        parent::__construct($strName, $strWidth, $wFlags);
75
        $this->fltMin = $fltMin;
76
        $this->fltMax = $fltMax;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     * @see \SKien\Formgenerator\FormElement::fromXML()
82
     * @internal l
83
     */
84
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
85
    {
86
        $strName = self::getAttribString($oXMLElement, 'name');
87
        $strWidth = self::getAttribString($oXMLElement, 'width');
88
        $fltMin = self::getAttribFloat($oXMLElement, 'min', self::MIN_DEFAULT);
89
        $fltMax = self::getAttribFloat($oXMLElement, 'max', self::MAX_DEFAULT);
90
        $wFlags = self::getAttribFlags($oXMLElement);
91
        $oFormElement = new self($strName, $strWidth, $fltMin, $fltMax, $wFlags);
92
        $oFormParent->add($oFormElement);
93
        $oFormElement->readAdditionalXML($oXMLElement);
94
        return $oFormElement;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
100
     * @internal
101
     */
102
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
103
    {
104
        parent::readAdditionalXML($oXMLElement);
105
        if (self::hasAttrib($oXMLElement, 'low')) {
106
            $this->fltLow = self::getAttribFloat($oXMLElement, 'low');
107
        }
108
        if (self::hasAttrib($oXMLElement, 'high')) {
109
            $this->fltHigh = self::getAttribFloat($oXMLElement, 'high');
110
        }
111
        if (self::hasAttrib($oXMLElement, 'optimum')) {
112
            $this->fltOpt = self::getAttribFloat($oXMLElement, 'optimum');
113
        }
114
    }
115
116
    /**
117
     * Set min/max value for the slider.
118
     * @param float $fltMin
119
     * @param float $fltMax
120
     */
121
    public function setMinMax(?float $fltMin, ?float $fltMax) : void
122
    {
123
        $this->fltMin = $fltMin ?? $this->fltMin;
124
        $this->fltMax = $fltMax ?? $this->fltMax;
125
    }
126
127
    /**
128
     * Set the upper/lower bound of the measured range and the optimum value.
129
     * @param float $fltLow
130
     * @param float $fltHigh
131
     * @param float $fltOpt
132
     */
133
    public function setMessureRange(?float $fltLow, ?float $fltHigh, ?float $fltOpt = null) : void
134
    {
135
        $this->fltLow = $fltLow;
136
        $this->fltHigh = $fltHigh;
137
        $this->fltOpt = $fltOpt;
138
    }
139
140
    /**
141
     * get value from dataprovider.
142
     * Value have to be retrieved earlier because it may be needed for the value-label
143
     * before the value of the range element is set.
144
     */
145
    protected function onParentSet() : void
146
    {
147
        $value = $this->oFG->getData()->getValue($this->strName);
148
        $this->fltValue = $value ? floatval($value) : $this->fltMin;
149
    }
150
151
    /**
152
     * {@inheritDoc}
153
     * @see \SKien\Formgenerator\FormElement::buildValue()
154
     */
155
    protected function buildValue() : string
156
    {
157
        $strHTML = ' value="' . $this->fltValue . '"';
158
        return $strHTML;
159
    }
160
161
162
    /**
163
     * Build the HTML-notation for the input element.
164
     * {@inheritDoc}
165
     * @see \SKien\Formgenerator\FormElement::getHTML()
166
     * @internal
167
     */
168
    public function getHTML() : string
169
    {
170
        $this->addAttribute('min', (string)$this->fltMin);
171
        $this->addAttribute('max', (string)$this->fltMax);
172
        $this->addAttribute('low', $this->fltLow ? (string)$this->fltLow : null); // null casted to string results to empty string!
173
        $this->addAttribute('high', $this->fltHigh ? (string)$this->fltHigh : null);
174
        $this->addAttribute('optimum', $this->fltOpt ? (string)$this->fltOpt : null);
175
176
        if (!empty($this->size)) {
177
            $this->addStyle('width', (string)$this->size);
178
        }
179
        $this->processFlags();
180
181
        $strInnerHTML = $this->getAttribute('title');
182
        if ($strInnerHTML === null) {
183
            $strInnerHTML = $this->buildTitle();
184
            $this->addAttribute('title', $strInnerHTML);
185
        }
186
187
        $this->strID = $this->strID ?: $this->strName;
188
189
        $strHTML = $this->buildContainerDiv();
190
        $strHTML .= '<meter ';
191
        $strHTML .= FormElement::buildClass(); // direct call of FormElement - we don't want 'Input_OK' ...
192
        $strHTML .= $this->buildID();
193
        $strHTML .= $this->buildStyle();
194
        $strHTML .= $this->buildAttributes();
195
        $strHTML .= $this->buildValue();
196
        $strHTML .= '>' . $strInnerHTML . '</meter>';
197
        $strHTML .= '</div>' . PHP_EOL;
198
199
        return $strHTML;
200
    }
201
202
    /**
203
     * Build a title from value and min/max settings.
204
     * @return string
205
     */
206
    protected function buildTitle() : string
207
    {
208
        return $this->fltValue . ' / ' . $this->fltMax;
209
    }
210
}
211