FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Local.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Local extends Component{
6 
7  public $dateFormat = 'd-m-Y';
8  public $timeFormat = 'G:i';
9  public $dateTimeFormat = '[date], [time]';
10 
11  public $decimalPoint = ',';
12  public $thousandsSeparator = '.';
13  public $delimiter = ';';
14 
15  protected function init(){
16  parent::init();
17  $this->dateTimeFormat = strtr($this->dateTimeFormat,['[date]' => $this->dateFormat,'[time]' => $this->timeFormat]);
18  }
19 
20  public function clientConfig(){
21  return array_merge(parent::clientConfig(),[
22  'dateFormat' => $this->dateFormat,
23  'timeFormat' => $this->timeFormat,
24  'dateTimeFormat' => $this->dateTimeFormat,
25  'decimalPoint' => $this->decimalPoint,
26  'thousandsSeparator' => $this->thousandsSeparator,
27  'delimiter' => $this->delimiter
28  ]);
29  }
30 
31  public function formatDate($value){
32  if(!$value) return null;
33  if(!is_object($value)) $value = DateTime\Date::create($value,'Y-m-d');
34  return $value ? $value->format($this->dateFormat) : null;
35  }
36 
37  public function convertDate($value){
38  return DateTime\Date::create($value,$this->dateFormat);
39  }
40 
41  public function formatTime($value){
42  if(\Rsi::nothing($value)) return $value;
43  if(!is_object($value)) $value = DateTime\Time::create($value);
44  return $value ? $value->format($this->timeFormat) : null;
45  }
46 
47  public function convertTime($value){
48  return DateTime\Time::create($value,$this->timeFormat);
49  }
50 
51  public function formatDateTime($value){
52  if(\Rsi::nothing($value)) return $value;
53  if(!is_object($value)) $value = DateTime\DateTime::create($value);
54  return $value ? $value->format($this->dateTimeFormat) : null;
55  }
56 
57  public function convertDateTime($value){
58  return DateTime\DateTime::create($value,$this->dateTimeFormat);
59  }
60 
61  public function formatInt($value){
62  if(\Rsi::nothing($value)) return $value;
63  return number_format($value,0,$this->decimalPoint,$this->thousandsSeparator);
64  }
65 
66  public function convertInt($value){
67  return (int)str_replace($this->thousandsSeparator,'',$value);
68  }
69 
70  public function formatNumber($value,$decimals = 0,$trim = null,$zero = null){
71  if(\Rsi::nothing($value)) return $value;
72  $value = number_format($value,$decimals,$this->decimalPoint,$this->thousandsSeparator);
73  if($decimals && ($trim !== null))
74  while(($trim++ < $decimals) && !substr($value,-1)) $value = substr($value,0,-1);
75  if($zero !== null) $value = preg_replace('/' . preg_quote($this->decimalPoint,'/') . '0*$/',$zero,$value);
76  return rtrim($value,$this->decimalPoint);
77  }
78 
79  public function convertNumber($value,$decimals = 0){
80  return round(str_replace($this->decimalPoint,'.',str_replace($this->thousandsSeparator,'',$value)),$decimals);
81  }
82 
83  public function formatSpellOut($value,$locale = null,$format = null){
84  return \Rsi\Str::transform((new \NumberFormatter($locale,\NumberFormatter::SPELLOUT))->format($value),$format);
85  }
86 
87  public function formatRoman($value){
88  return \Rsi\Number::toRoman($value) ?: $value;
89  }
90 
91  public function formatBytes($value,$decimals = 1){
92  return \Rsi\Number::formatBytes($value,$decimals,$this->decimalPoint,$this->thousandsSeparator);
93  }
94 
95  public function convertBytes($value){
96  return \Rsi\Number::shorthandBytes($value);
97  }
98 
99 }
static create($value, $format=null)
Definition: Date.php:7
convertDateTime($value)
Definition: Local.php:57
convertBytes($value)
Definition: Local.php:95
formatRoman($value)
Definition: Local.php:87
formatBytes($value, $decimals=1)
Definition: Local.php:91
formatSpellOut($value, $locale=null, $format=null)
Definition: Local.php:83
convertDate($value)
Definition: Local.php:37
formatTime($value)
Definition: Local.php:41
$thousandsSeparator
Definition: Local.php:12
formatInt($value)
Definition: Local.php:61
static create($value, $format=null)
Definition: Time.php:7
formatNumber($value, $decimals=0, $trim=null, $zero=null)
Definition: Local.php:70
convertTime($value)
Definition: Local.php:47
convertInt($value)
Definition: Local.php:66
clientConfig()
Definition: Local.php:20
static create($value, $format=null)
Definition: DateTime.php:7
Basic component class.
Definition: Component.php:8
convertNumber($value, $decimals=0)
Definition: Local.php:79
formatDate($value)
Definition: Local.php:31
formatDateTime($value)
Definition: Local.php:51