1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/phpviet/symfony-number-to-words |
4
|
|
|
* |
5
|
|
|
* @copyright (c) PHP Viet |
6
|
|
|
* @license [MIT](https://opensource.org/licenses/MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace PHPViet\Symfony\NumberToWords; |
10
|
|
|
|
11
|
|
|
use PHPViet\NumberToWords\Transformer; |
12
|
|
|
use PHPViet\NumberToWords\DictionaryInterface; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Vuong Minh <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class Service |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Container hổ trợ việc lấy các service từ điển. |
24
|
|
|
* |
25
|
|
|
* @var ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Chứa thông tin cấu hình từ dev. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Khởi tạo dịch vụ chuyển đổi số sang chữ số. |
38
|
|
|
* |
39
|
|
|
* @param ContainerInterface $container |
40
|
|
|
* @param array $config |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ContainerInterface $container, array $config = []) |
43
|
|
|
{ |
44
|
|
|
$this->container = $container; |
45
|
|
|
$this->config = $config; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Chuyển đổi số sang chữ số. |
50
|
|
|
* |
51
|
|
|
* @param string|int|float $number |
52
|
|
|
* @param string|null $dictionary |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function toWords($number, ?string $dictionary = null): string |
56
|
|
|
{ |
57
|
|
|
$dictionary = $this->getDictionaryInstance($dictionary); |
58
|
|
|
$transformer = $this->createTransformer($dictionary); |
59
|
|
|
|
60
|
|
|
return $transformer->toWords($number); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Chuyển đổi số sang tiền tệ. |
65
|
|
|
* |
66
|
|
|
* @param string|float|int $number |
67
|
|
|
* @param array|string[]|string $unit |
68
|
|
|
* @param string|null $dictionary |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function toCurrency($number, $unit = 'đồng', ?string $dictionary = null): string |
72
|
|
|
{ |
73
|
|
|
$dictionary = $this->getDictionaryInstance($dictionary); |
74
|
|
|
$transformer = $this->createTransformer($dictionary); |
75
|
|
|
|
76
|
|
|
return $transformer->toCurrency($number, $unit); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Khởi tạo đối tượng hổ trợ việc chuyển đổi. |
81
|
|
|
* |
82
|
|
|
* @param DictionaryInterface|null $dictionary |
83
|
|
|
* @return Transformer |
84
|
|
|
*/ |
85
|
|
|
protected function createTransformer(?DictionaryInterface $dictionary): Transformer |
86
|
|
|
{ |
87
|
|
|
return new Transformer($dictionary); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Trả về từ điển dùng để chuyển đổi số sang chữ số. |
92
|
|
|
* |
93
|
|
|
* @param string|null $dictionary |
94
|
|
|
* @return DictionaryInterface|null |
95
|
|
|
*/ |
96
|
|
|
protected function getDictionaryInstance(?string $dictionary): ?DictionaryInterface |
97
|
|
|
{ |
98
|
|
|
$dictionary = $dictionary ?? $this->getDefaultDictionary(); |
99
|
|
|
|
100
|
|
|
if (! $dictionaryService = $this->config['dictionaries'][$dictionary] ?? null) { |
101
|
|
|
throw new InvalidConfigurationException(sprintf('Dictionary (%s) is not defined!', $dictionary)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->container->get($dictionaryService); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Trả về từ điển mặc định nếu không chỉ định. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function getDefaultDictionary(): string |
113
|
|
|
{ |
114
|
|
|
return $this->config['defaults']['dictionary']; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|