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 $decimals = null;
12  public $round = null;
13  public $trim = null;
14 
15  public function clientConfig(){
16  return array_merge(parent::clientConfig(),[
17  self::DECIMALS => $this->decimals,
18  self::ROUND => $this->round,
19  self::TRIM => $this->trim
20  ]);
21  }
22 
23  public function round($value){
24  if($value === null) return null;
25  if($this->decimals !== null) $value = round($value,$this->decimals);
26  return $this->round > 0 ? round($value / $this->round) * $this->round : $value;
27  }
28 
29  public function convert($value){
30  return \Rsi::nothing($value) ? null : $this->round($this->local->convertNumber($value,$this->decimals));
31  }
32 
33  public function format($value,$trim = true){
34  return $this->local->formatNumber($this->round($value),$this->decimals,$trim ? $this->trim : null);
35  }
36 
37  public function checkFormat($value,$index = null){
38  return is_scalar($value) && preg_match('/^-?\\d+(\\.\\d+)?$/',$value);
39  }
40 
41  public function getDecimals(){
42  return $this->config(self::DECIMALS) ?: ($this->round ? max(0,(int)ceil(-log10($this->round))) : null);
43  }
44 
45 }
Rsi\Fred\Controller\Widget\Number\format
format($value, $trim=true)
Definition: Number.php:33
Rsi\Fred\Controller\Widget\Number\TRIM
const TRIM
Trim the number to this many decimals (when zeros).
Definition: Number.php:9
Rsi\Fred\Controller\Widget\Number\$round
$round
Definition: Number.php:12
Rsi\Fred\Controller\Widget\Number\$trim
$trim
Definition: Number.php:13
Rsi\Fred\Controller\Widget\Number\convert
convert($value)
Convert a value from user format to standard, internal format.
Definition: Number.php:29
Rsi\Fred\Controller\Widget\Number\getDecimals
getDecimals()
Definition: Number.php:41
Rsi\Fred\Controller\Widget\config
config($key, $default=null)
Get single configuration value.
Definition: Widget.php:98
Rsi\Fred\Controller\Widget
Definition: Char.php:3
Rsi\Fred\Controller\Widget\Number\DECIMALS
const DECIMALS
Number of decimals to use (for rounding).
Definition: Number.php:7
Rsi\Fred\Controller\Widget\Number\round
round($value)
Definition: Number.php:23
Rsi\Fred\Controller\Widget\Number\checkFormat
checkFormat($value, $index=null)
Definition: Number.php:37
Rsi\Fred\Controller\Widget\Number\clientConfig
clientConfig()
Public configuration.
Definition: Number.php:15
Rsi\Fred\Controller\Widget\Number
Definition: Number.php:5
Rsi\Fred\Controller\Widget\Number\$decimals
$decimals
Definition: Number.php:11
Rsi\Fred\Controller\Widget\Number\ROUND
const ROUND
Rounding value (e.g. with rounding set to 5, 8 will become 10).
Definition: Number.php:8