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