FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Number.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Number extends \Rsi\Fred\Controller\Widget{
6 
7  const DECIMALS = 'decimals'; //!< Number of decimals to use (for rounding).
8  const ROUND = 'round'; //!< Rounding value (e.g. with rounding set to 5, 8 will become 10).
9  const TRIM = 'trim'; //!< Trim the number to this many decimals (when zeros).
10 
11  public $round = null;
12  public $trim = null;
13 
14  public function clientConfig(){
15  return array_merge(parent::clientConfig(),[
16  self::MIN => $this->min || $this->nothing($this->min) ? $this->min : '0.0', //otherwise it gets wiped
17  self::MAX => $this->max || $this->nothing($this->max) ? $this->max : '0.0', //otherwise it gets wiped
18  self::DECIMALS => $this->decimals,
19  self::ROUND => $this->round,
20  self::TRIM => $this->trim
21  ]);
22  }
23 
24  public function round($value){
25  if($value === null) return null;
26  $value = round($value,$this->decimals);
27  return $this->round > 0 ? round($value / $this->round) * $this->round : $value;
28  }
29 
30  public function convert($value){
31  return \Rsi::nothing($value) ? null : $this->round($this->local->convertNumber($value,$this->decimals));
32  }
33 
34  public function format($value,$trim = true){
35  return $this->local->formatNumber($this->round($value),$this->decimals,$trim ? $this->trim : null);
36  }
37 
38  public function checkFormat($value,$index = null){
39  return preg_match('/^-?\\d+(\\.\\d+)?$/',$value);
40  }
41 
42  public function getDecimals(){
43  return $this->config(self::DECIMALS) ?: ($this->round ? max(0,(int)ceil(-log10($this->round))) : null);
44  }
45 
46 }
format($value, $trim=true)
Definition: Number.php:34
nothing($value)
Check if a value is empty.
Definition: Widget.php:240
const DECIMALS
Number of decimals to use (for rounding).
Definition: Number.php:7
const TRIM
Trim the number to this many decimals (when zeros).
Definition: Number.php:9
config($key, $default=null)
Get single configuration value.
Definition: Widget.php:93
checkFormat($value, $index=null)
Definition: Number.php:38
const ROUND
Rounding value (e.g. with rounding set to 5, 8 will become 10).
Definition: Number.php:8