1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the trendsoft/capital. |
||
5 | * (c) jabber <[email protected]> |
||
6 | * This source file is subject to the MIT license that is bundled |
||
7 | * with this source code in the file LICENSE. |
||
8 | */ |
||
9 | |||
10 | namespace Capital; |
||
11 | |||
12 | class Money |
||
13 | { |
||
14 | private $money; |
||
15 | |||
16 | private $uppers = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; |
||
17 | |||
18 | private $units = ['分', '角']; |
||
19 | |||
20 | private $grees = ['元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万', '拾', '佰']; |
||
21 | |||
22 | private $thanOne = false; |
||
23 | |||
24 | /** |
||
25 | * Money constructor. |
||
26 | * |
||
27 | * @param int|number|string $money default 0 |
||
28 | */ |
||
29 | 12 | public function __construct($money = 0) |
|
30 | { |
||
31 | 12 | $this->setMoney($money); |
|
32 | 11 | } |
|
33 | |||
34 | /** |
||
35 | * Get Init Money. |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | 2 | public function getMoney(): string |
|
40 | { |
||
41 | 2 | return $this->money; |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param int|number|string $money default 0 |
||
46 | */ |
||
47 | 12 | public function setMoney($money = 0) |
|
48 | { |
||
49 | 12 | if (!(is_float($money) || is_numeric($money) || is_int($money))) { |
|
50 | 1 | throw new \InvalidArgumentException($money); |
|
51 | } |
||
52 | 11 | if ($money > 1) { |
|
53 | 6 | $this->thanOne = true; |
|
54 | } |
||
55 | 11 | $this->money = number_format($money, 2, '.', ''); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | 11 | } |
|
57 | |||
58 | /** |
||
59 | * Convert to Capital. |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | 4 | public function toCapital(): string |
|
64 | { |
||
65 | 4 | @list($intPart, $decimalPart) = explode('.', $this->money, 2); |
|
66 | 4 | if (0.0 === floatval($this->money)) { |
|
67 | 3 | return '零元'; |
|
68 | } |
||
69 | 3 | $result = $this->getIntPart($intPart); |
|
70 | 3 | $result .= $this->getDecimalPart($decimalPart); |
|
71 | |||
72 | 3 | return $result; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Parse to Capital. |
||
77 | * |
||
78 | * @param int|number|string $money default 0 |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | 1 | public function parse($money): string |
|
83 | { |
||
84 | 1 | $this->setMoney($money); |
|
85 | |||
86 | 1 | return $this->toCapital(); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get Int Part. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | 3 | private function getIntPart($intPart) |
|
95 | { |
||
96 | 3 | $result = ''; |
|
97 | 3 | $gree = strlen($intPart) - 1; |
|
98 | 3 | if ($intPart > 0) { |
|
99 | 3 | for ($i = 0; $i < strlen($intPart); ++$i) { |
|
100 | 3 | $num = $intPart[$i]; |
|
101 | 3 | $result .= $this->uppers[$num].$this->grees[$gree--]; |
|
102 | } |
||
103 | } |
||
104 | |||
105 | 3 | $result = str_replace('零亿', '亿零', $result); |
|
106 | 3 | $result = str_replace('零万', '万零', $result); |
|
107 | |||
108 | 3 | $result = str_replace('零拾', '零', $result); |
|
109 | 3 | $result = str_replace('零佰', '零', $result); |
|
110 | 3 | $result = str_replace('零仟', '零', $result); |
|
111 | |||
112 | 3 | $result = str_replace('零零', '零', $result); |
|
113 | 3 | $result = str_replace('零零', '零', $result); |
|
114 | |||
115 | 3 | $result = str_replace('零亿', '亿', $result); |
|
116 | 3 | $result = str_replace('零万', '万', $result); |
|
117 | 3 | $result = str_replace('零元', '元', $result); |
|
118 | |||
119 | 3 | return $result; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Get Decimal Part. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 3 | private function getDecimalPart($decimalPart) |
|
128 | { |
||
129 | 3 | $result = ''; |
|
130 | 3 | if ($decimalPart > 0) { |
|
131 | 1 | $unit = strlen($decimalPart) - 1; |
|
132 | 1 | for ($i = 0; $i < strlen($decimalPart); ++$i) { |
|
133 | 1 | $num = $decimalPart[$i]; |
|
134 | 1 | $result .= $this->uppers[$num].$this->units[$unit--]; |
|
135 | } |
||
136 | } |
||
137 | 3 | $result = str_replace('零分', '', $result); |
|
138 | 3 | if ($this->thanOne) { |
|
139 | 2 | $result = str_replace('零角', '零', $result); |
|
140 | } else { |
||
141 | 3 | $result = str_replace('零角', '', $result); |
|
142 | } |
||
143 | |||
144 | 3 | return $result; |
|
145 | } |
||
146 | } |
||
147 |