| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace SKien\Formgenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Input field for float value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Default the separators tried to get from the local environment, but can be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * specified in the configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Default align is right - this can be overwritten ion the configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * #### History | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * - *2021-01-30*   initial version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @package Formgenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @version 1.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Stefanius <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @copyright MIT License - see the LICENSE file for details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class FormFloat extends FormInput | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var string decimal point     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected string $strDP = '.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var string thousands separator     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected string $strTS = ','; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var int decimal digits     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected int $iDec = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var bool empty entries allowed. If false, empty input is set to '0.0' */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected bool $bEmptyAllowed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Creates input field for float values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $strName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param int|string $iSize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param int $wFlags    default value = 0 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public function __construct(string $strName, $iSize, int $iDecimalPoints, int $wFlags = 0)  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         parent::__construct($strName, $iSize, $wFlags); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $this->iDec = $iDecimalPoints; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @see \SKien\Formgenerator\FormElement::fromXML() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $strName = self::getAttribString($oXMLElement, 'name', ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $strSize = self::getAttribString($oXMLElement, 'size', ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $iDecimalPoints = self::getAttribInt($oXMLElement, 'digits', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $wFlags = self::getAttribFlags($oXMLElement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $oFormElement = new self($strName, $strSize, $iDecimalPoints, $wFlags); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $oFormParent->add($oFormElement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $oFormElement->readAdditionalXML($oXMLElement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $oFormElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * get format from configuration: <ul> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * <li> right alignment (default: true) </li> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * <li> decimal point and thousands separator (default: from locale settings) </li></ul> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected function onParentSet() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if ($this->oFG->getConfig()->getBool('Float.RightAlign', true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->addFlags(FormFlags::ALIGN_RIGHT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $li = localeconv(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->strDP = $this->oFG->getConfig()->getString('Float.DecimalPoint', ($li['mon_decimal_point'] ?: '.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->strTS = $this->oFG->getConfig()->getString('Float.ThousandsSep', ($li['mon_thousands_sep'] ?: ',')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->addAttribute('data-validation', 'float:' . ($this->bEmptyAllowed ? 'e' : 'x') . $this->iDec . $this->strDP . $this->strTS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->setPlaceholder($this->oFG->getConfig()->getString('Float.Placeholder')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @see \SKien\Formgenerator\FormElement::buildValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected function buildValue() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $fltValue = floatval($this->oFG->getData()->getValue($this->strName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if ($this->oFlags->isSet(FormFlags::NO_ZERO) && $fltValue == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $strValue = number_format($fltValue, $this->iDec, $this->strDP, $this->strTS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $strHTML = ' value="' . $strValue . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return $strHTML; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |