├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── n2w.php └── src ├── N2WFacade.php ├── ServiceProvider.php ├── Transformer.php └── helpers.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2.0.1 2 | ----------------- 3 | 4 | + Fix can not load default config #5. 5 | 6 | 2.0.0 7 | ----------------- 8 | 9 | + Bump Laravel to version 8.x 10 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) PHP Viet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 |

Laravel Number To Words

6 |
7 |

8 | Latest version 9 | Build status 10 | Quantity score 11 | StyleCI 12 | Total download 13 | License 14 |

15 |

16 | 17 | ## Thông tin 18 | 19 | Laravel number to words hổ trợ chuyển đổi số sang chữ số Tiếng Việt. 20 | 21 | ## Cài đặt 22 | 23 | Cài đặt Laravel Number To Words thông qua [Composer](https://getcomposer.org): 24 | 25 | ```bash 26 | composer require phpviet/laravel-number-to-words 27 | ``` 28 | 29 | ## Cách sử dụng 30 | 31 | ### Các tính năng của extension: 32 | 33 | - [`Chuyển đổi số sang chữ số`](#Chuyển-đổi-số-sang-chữ-số) 34 | - [`Chuyển đổi số sang tiền tệ`](#Chuyển-đổi-số-sang-tiền-tệ) 35 | - [`Thay cách đọc số`](#Thay-cách-đọc-số) 36 | 37 | ### Chuyển đổi số sang chữ số 38 | 39 | + Sử dụng thông qua facade `N2W`: 40 | 41 | ```php 42 | use N2W; 43 | 44 | // âm năm 45 | N2W::toWords(-5); 46 | 47 | // năm 48 | N2W::toWords(5); 49 | 50 | // năm phẩy năm 51 | N2W::toWords(5.5); 52 | ``` 53 | 54 | + Sử dụng thông qua hàm hổ trợ `n2w`: 55 | 56 | ```php 57 | // mười lăm 58 | n2w(15); 59 | 60 | // một trăm linh năm 61 | n2w(105); 62 | 63 | // hai mươi tư 64 | n2w(24); 65 | ``` 66 | 67 | ### Chuyển đổi số sang tiền tệ 68 | 69 | + Sử dụng thông qua facade `N2W`: 70 | 71 | ```php 72 | use N2W; 73 | 74 | // năm triệu sáu trăm chín mươi nghìn bảy trăm đồng 75 | N2W::toCurrency(5690700); 76 | ``` 77 | 78 | + Sử dụng thông qua hàm hổ trợ `n2c`: 79 | 80 | ```php 81 | // chín mươi lăm triệu năm trăm nghìn hai trăm đồng 82 | n2c(95500200); 83 | ``` 84 | 85 | Ngoài ra ta còn có thể sử dụng đơn vị tiền tệ khác thông qua tham trị thứ 2 của phương thức 86 | `toCurrency` và hàm `n2c` với mảng phần từ đầu tiên là đơn vị cho số nguyên và kế tiếp là đơn vị của phân số: 87 | 88 | ```php 89 | use N2W; 90 | 91 | // sáu nghìn bảy trăm bốn mươi hai đô bảy xen 92 | N2W::toCurrency(6742.7, ['đô', 'xen']); 93 | 94 | // chín nghìn bốn trăm chín mươi hai đô mười lăm xen 95 | n2c(9492.15, ['đô', 'xen']); 96 | ``` 97 | 98 | ### Thay cách đọc số 99 | 100 | > Nếu như bạn cảm thấy cách đọc ở trên ổn rồi thì hãy bỏ qua bước này. 101 | 102 | Đầu tiên để thay đổi cách đọc số bạn cần phải publish file cấu hình thông qua câu lệnh: 103 | 104 | ```php 105 | php artisan vendor:publish --provider="PHPViet\Laravel\NumberToWords\ServiceProvider" --tag="config" 106 | ``` 107 | 108 | Sau khi publish xong ta sẽ có được file config `config/n2w.php` như sau: 109 | 110 | ```php 111 | return [ 112 | /** 113 | * Cấu hình từ điển mặc định theo chuẩn chung của cả nước 114 | */ 115 | 'defaults' => [ 116 | 'dictionary' => 'standard', 117 | ], 118 | 'dictionaries' => [ 119 | /** 120 | * Cấu hình các từ điển custom theo ý bạn. 121 | */ 122 | 'standard' => PHPViet\NumberToWords\Dictionary::class, 123 | 'south' => PHPViet\NumberToWords\SouthDictionary::class 124 | ] 125 | ]; 126 | ``` 127 | 128 | Ngay bây giờ bạn hãy thử đổi default `standard` sang `south`, toàn bộ phương thức chuyển 129 | đổi số sang chữ số và tiền tệ sẽ đọc theo phong cách trong Nam: 130 | 131 | ```php 132 | use N2W; 133 | 134 | // một trăm linh một => một trăm lẻ một 135 | N2W::toWords(101); 136 | 137 | // một nghìn => một ngàn 138 | N2W::toWords(1000); 139 | 140 | // hai mươi tư => hai mươi bốn 141 | N2W::toWords(24); 142 | 143 | // một trăm hai mươi tư nghìn không trăm linh một đồng => một trăm hai mươi bốn ngàn không trăm lẻ một đồng 144 | N2W::toCurrency(124001); 145 | ``` 146 | 147 | hoặc bạn muốn sử dụng linh động hơn thì hãy chỉ định từ điển: 148 | 149 | ```php 150 | // một trăm hai mươi tư nghìn không trăm linh một 151 | n2w(124001); 152 | 153 | // một trăm hai mươi bốn ngàn không trăm lẻ một 154 | n2w(124001, 'south'); 155 | ``` 156 | 157 | Nếu như bạn muốn thay đổi cách đọc theo ý bạn thì hãy tạo một lớp `Dictionary` kế thừa 158 | `PHPViet\NumberToWords\Dictionary` hoặc thực thi mẫu trừu tượng `PHPViet\NumberToWords\DictionaryInterface`: 159 | 160 | ```php 161 | 162 | use PHPViet\NumberToWords\Dictionary; 163 | use PHPViet\NumberToWords\Transformer; 164 | 165 | class MyDictionary extends Dictionary { 166 | 167 | /** 168 | * @inheritDoc 169 | */ 170 | public function specialTripletUnitFive(): string 171 | { 172 | return 'nhăm'; 173 | } 174 | 175 | } 176 | ``` 177 | 178 | Sau đó khai báo vào config: 179 | 180 | ```php 181 | return [ 182 | /** 183 | * Cấu hình từ điển mặc định theo chuẩn chung của cả nước 184 | */ 185 | 'defaults' => [ 186 | 'dictionary' => 'my', 187 | ], 188 | 'dictionaries' => [ 189 | /** 190 | * Cấu hình các từ điển custom theo ý bạn. 191 | */ 192 | 'standard' => PHPViet\NumberToWords\Dictionary::class, 193 | 'south' => PHPViet\NumberToWords\SouthDictionary::class, 194 | 'my' => MyDictionary::class 195 | ] 196 | ]; 197 | ``` 198 | 199 | Và hãy thử ngay: 200 | 201 | ```php 202 | use N2W; 203 | 204 | // mười nhăm 205 | N2W::toWords(15); 206 | ``` 207 | 208 | ## Dành cho nhà phát triển 209 | 210 | Nếu như bạn cảm thấy extension còn thiếu sót hoặc sai sót và bạn muốn đóng góp để phát triển chung, 211 | chúng tôi rất hoan nghênh! Hãy tạo các `issue` để đóng góp ý tưởng cho phiên bản kế tiếp 212 | hoặc tạo `PR` để đóng góp. Cảm ơn! 213 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phpviet/laravel-number-to-words", 3 | "description": "Laravel number to words hổ trợ chuyển đổi số sang chữ số Tiếng Việt.", 4 | "keywords": [ 5 | "phpviet", 6 | "laravel-number-to-words" 7 | ], 8 | "homepage": "https://github.com/phpviet/laravel-number-to-words", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Vuong Xuong Minh", 13 | "email": "vuongxuongminh@gmail.com" 14 | } 15 | ], 16 | "require": { 17 | "phpviet/number-to-words": "^1.2", 18 | "illuminate/support": "^6.0 || ^7.0 || ^8.0" 19 | }, 20 | "require-dev": { 21 | "orchestra/testbench": "^4.0 || ^5.0 || ^6.0" 22 | }, 23 | "autoload": { 24 | "files": [ 25 | "src/helpers.php" 26 | ], 27 | "psr-4": { 28 | "PHPViet\\Laravel\\NumberToWords\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "PHPViet\\Laravel\\NumberToWords\\Tests\\": "tests" 34 | } 35 | }, 36 | "scripts": { 37 | "test": "vendor/bin/phpunit" 38 | }, 39 | "config": { 40 | "sort-packages": true 41 | }, 42 | "extra": { 43 | "branch-alias": { 44 | "dev-master": "2.x-dev" 45 | }, 46 | "laravel": { 47 | "aliases": { 48 | "N2W": "PHPViet\\Laravel\\NumberToWords\\N2WFacade" 49 | }, 50 | "providers": [ 51 | "PHPViet\\Laravel\\NumberToWords\\ServiceProvider" 52 | ] 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /config/n2w.php: -------------------------------------------------------------------------------- 1 | [ 14 | 'dictionary' => 'standard', 15 | ], 16 | 'dictionaries' => [ 17 | /* 18 | * Cấu hình các từ điển custom theo ý bạn. 19 | */ 20 | 'standard' => PHPViet\NumberToWords\Dictionary::class, 21 | 'south' => PHPViet\NumberToWords\SouthDictionary::class, 22 | ], 23 | ]; 24 | -------------------------------------------------------------------------------- /src/N2WFacade.php: -------------------------------------------------------------------------------- 1 | 20 | * @since 1.0.0 21 | */ 22 | class N2WFacade extends Facade 23 | { 24 | /** 25 | * Từ điển hiện tại. 26 | * 27 | * @var null|DictionaryInterface 28 | */ 29 | public static $dictionary; 30 | 31 | /** 32 | * {@inheritdoc} 33 | */ 34 | protected static function getFacadeAccessor(): Transformer 35 | { 36 | $dictionary = static::$dictionary ?? static::getDefaultDictionary(); 37 | $dictionary = static::makeDictionary($dictionary); 38 | 39 | return app('n2w', compact('dictionary')); 40 | } 41 | 42 | /** 43 | * Trả về từ điển mặc định trong config. 44 | * 45 | * @return string 46 | */ 47 | protected static function getDefaultDictionary(): string 48 | { 49 | return config('n2w.defaults.dictionary'); 50 | } 51 | 52 | /** 53 | * Tạo từ điển. 54 | * 55 | * @param string $dictionary 56 | * @return DictionaryInterface 57 | */ 58 | protected static function makeDictionary(string $dictionary): DictionaryInterface 59 | { 60 | if (! $dictionaryClass = config("n2w.dictionaries.{$dictionary}")) { 61 | throw new InvalidArgumentException(sprintf('Dictionary (%s) is not defined!', $dictionary)); 62 | } 63 | 64 | return app()->make($dictionaryClass); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 18 | * @since 1.0.0 19 | */ 20 | class ServiceProvider extends BaseServiceProvider 21 | { 22 | public $bindings = [ 23 | 'n2w' => Transformer::class, 24 | ]; 25 | 26 | public $singletons = [ 27 | Dictionary::class => Dictionary::class, 28 | DictionaryInterface::class => Dictionary::class, 29 | SouthDictionary::class => SouthDictionary::class, 30 | ]; 31 | 32 | public function boot(): void 33 | { 34 | $this->publishes([ 35 | __DIR__.'/../config/n2w.php' => config_path('n2w.php'), 36 | ], 'config'); 37 | } 38 | 39 | public function register(): void 40 | { 41 | $this->mergeConfigFrom(__DIR__.'/../config/n2w.php', 'n2w'); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Transformer.php: -------------------------------------------------------------------------------- 1 | 16 | * @since 1.0.0 17 | */ 18 | class Transformer extends BaseTransformer 19 | { 20 | use Macroable; 21 | } 22 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 |