|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IndraGunawan\RestService; |
|
4
|
|
|
|
|
5
|
|
|
class ValueFormatter |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Evaluates if the given value is to be treated as empty. |
|
9
|
|
|
* |
|
10
|
|
|
* @param mixed $value |
|
11
|
|
|
* |
|
12
|
|
|
* @return bool |
|
13
|
|
|
*/ |
|
14
|
3 |
|
public function isValueEmpty($value) |
|
15
|
|
|
{ |
|
16
|
3 |
|
return null === $value || '' === $value; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get $value if not empty, $defaultValue otherwise. |
|
21
|
|
|
* |
|
22
|
|
|
* @param mixed $value |
|
23
|
|
|
* @param mixed|null $defaultValue |
|
24
|
|
|
* |
|
25
|
|
|
* @return mixed|null |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function getValue($value, $defaultValue = null) |
|
28
|
|
|
{ |
|
29
|
2 |
|
if (!$this->isValueEmpty($value)) { |
|
30
|
2 |
|
return $value; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
return $defaultValue; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get formatted value base on type and format. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $type |
|
40
|
|
|
* @param string $format |
|
41
|
|
|
* @param mixed $value |
|
42
|
|
|
* @param mixed|null $defaultValue |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function format($type, $format, $value, $defaultValue = null) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$value = $this->getValue($value, $defaultValue); |
|
49
|
|
|
|
|
50
|
1 |
|
if (method_exists($this, 'format'.ucfirst(strtolower($type)))) { |
|
51
|
1 |
|
return $this->{'format'.ucfirst(strtolower($type))}($value, $format); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return $value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Format Integer. |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed $value |
|
61
|
|
|
* @param mixed|null $format |
|
62
|
|
|
* |
|
63
|
|
|
* @return int |
|
64
|
|
|
*/ |
|
65
|
1 |
|
private function formatInteger($value, $format = null) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
1 |
|
return (int) (string) $value; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Format Float. |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $value |
|
74
|
|
|
* @param mixed|null $format |
|
75
|
|
|
* |
|
76
|
|
|
* @return float |
|
77
|
|
|
*/ |
|
78
|
1 |
|
private function formatFloat($value, $format = null) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
1 |
|
return (float) (string) $value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Format String. |
|
85
|
|
|
* |
|
86
|
|
|
* @param mixed $value |
|
87
|
|
|
* @param mixed|null $format |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
1 |
|
private function formatString($value, $format = null) |
|
92
|
|
|
{ |
|
93
|
1 |
|
return sprintf($format ?: '%s', (string) $value); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Format Boolean. |
|
98
|
|
|
* |
|
99
|
|
|
* @param mixed $value |
|
100
|
|
|
* @param mixed|null $format |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
1 |
|
private function formatBoolean($value, $format = null) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
1 |
|
return ('false' === $value || false === $value || 0 === $value) ? false : true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Format Number. |
|
111
|
|
|
* |
|
112
|
|
|
* @param mixed $value |
|
113
|
|
|
* @param mixed|null $format |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
1 |
|
private function formatNumber($value, $format = null) |
|
118
|
|
|
{ |
|
119
|
1 |
|
if ($format) { |
|
120
|
1 |
|
$format = explode('|', $format); |
|
121
|
1 |
|
$decimal = isset($format[0]) ? $format[0] : 0; |
|
122
|
1 |
|
$decimalPoint = isset($format[1]) ? $format[1] : '.'; |
|
123
|
1 |
|
$thousandsSeparator = isset($format[2]) ? $format[2] : ','; |
|
124
|
|
|
|
|
125
|
1 |
|
return number_format((float) (string) $value, $decimal, $decimalPoint, $thousandsSeparator); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
return (string) $value; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Format Datetime. |
|
133
|
|
|
* |
|
134
|
|
|
* @param mixed $value |
|
135
|
|
|
* @param mixed|null $format |
|
136
|
|
|
* |
|
137
|
|
|
* @return \DateTime|string |
|
138
|
|
|
*/ |
|
139
|
1 |
|
private function formatDatetime($value, $format = null) |
|
140
|
|
|
{ |
|
141
|
1 |
|
if ($this->isValueEmpty($value)) { |
|
142
|
1 |
|
return; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
if ($value instanceof \DateTime) { |
|
146
|
1 |
|
return $value->format($format ?: 'Y-m-d\TH:i:s\Z'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
if ($format) { |
|
150
|
1 |
|
return \DateTime::createFromFormat($format, $value); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
1 |
|
return new \DateTime($value); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.