Skip to content

Commit 7882482

Browse files
Prepare for test
1 parent de618de commit 7882482

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"scrutinizer/ocular": "^1.5"
2525
},
2626
"autoload": {
27+
"files": [
28+
"src/helpers.php"
29+
],
2730
"psr-4": {
2831
"PHPViet\\Laravel\\NumberToWords\\": "src"
2932
}
@@ -33,6 +36,9 @@
3336
"PHPViet\\Laravel\\NumberToWords\\Tests\\": "tests"
3437
}
3538
},
39+
"scripts": {
40+
"test": "vendor/bin/phpunit"
41+
},
3642
"config": {
3743
"sort-packages": true
3844
},

config/n2w.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-number-to-words
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
return [
10+
/**
11+
* Cấu hình từ điển mặc định theo chuẩn chung của cả nước
12+
*/
13+
'defaults' => [
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+
];

src/Facades/N2W.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-number-to-words
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\NumberToWords\Facades;
10+
11+
use InvalidArgumentException;
12+
use PHPViet\NumberToWords\Transformer;
13+
use Illuminate\Support\Facades\Facade;
14+
use PHPViet\NumberToWords\DictionaryInterface;
15+
16+
/**
17+
* @method static string toWords($number)
18+
* @method static string toCurrency($number, $unit = 'đồng')
19+
*
20+
* @author Vuong Minh <vuongxuongminh@gmail.com>
21+
* @since 1.0.0
22+
*/
23+
class N2W extends Facade
24+
{
25+
/**
26+
* Từ điển hiện tại.
27+
*
28+
* @var null|DictionaryInterface
29+
*/
30+
public static $dictionary;
31+
32+
/**
33+
* Cache data của các từ điển.
34+
*
35+
* @var array|DictionaryInterface[]
36+
*/
37+
private static $dictionaries = [];
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
protected static function getFacadeAccessor(): Transformer
43+
{
44+
$dictionary = static::$dictionary ?? static::getDefaultDictionary();
45+
$dictionary = static::getDictionaryInstance($dictionary);
46+
47+
return app('n2w', [$dictionary]);
48+
}
49+
50+
/**
51+
* Trả về từ điển mặc định trong config.
52+
*
53+
* @return DictionaryInterface
54+
*/
55+
public static function getDefaultDictionary(): DictionaryInterface
56+
{
57+
return config('n2w.defaults.dictionary');
58+
}
59+
60+
/**
61+
* Tạo từ điển.
62+
*
63+
* @param string $dictionary
64+
* @return DictionaryInterface
65+
*/
66+
protected static function getDictionaryInstance(string $dictionary): DictionaryInterface
67+
{
68+
if (!$dictionaryClass = config("n2w.dictionaries.{$dictionary}")) {
69+
throw new InvalidArgumentException(sprintf('Dictionary (%s) is not defined!', $dictionary));
70+
}
71+
72+
if (!isset(static::$dictionaries[$dictionaryClass])) {
73+
return static::$dictionaries[$dictionaryClass] = app()->make($dictionaryClass);
74+
}
75+
76+
return static::$dictionaries[$dictionaryClass];
77+
}
78+
79+
80+
}

src/ServiceProvider.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-number-to-words
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\NumberToWords;
10+
11+
use PHPViet\NumberToWords\Transformer;
12+
use Illuminate\Contracts\Support\DeferrableProvider;
13+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
14+
15+
/**
16+
* @author Vuong Minh <vuongxuongminh@gmail.com>
17+
* @since 1.0.0
18+
*/
19+
class ServiceProvider extends BaseServiceProvider implements DeferrableProvider
20+
{
21+
22+
public $bindings = [
23+
'n2w' => Transformer::class
24+
];
25+
26+
public function boot(): void
27+
{
28+
$this->publishes([
29+
__DIR__ . '/../config/n2w.php' => config_path('n2w.php'),
30+
], 'config');
31+
}
32+
33+
public function register(): void
34+
{
35+
$this->mergeConfigFrom(__DIR__ . '/../config/n2w.php', 'n2w');
36+
}
37+
38+
public function provides(): array
39+
{
40+
return ['n2w'];
41+
}
42+
43+
}

src/helpers.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use PHPViet\Laravel\NumberToWords\Facades\N2W;
4+
5+
if (!function_exists('n2w')) {
6+
7+
/**
8+
* Hàm hổ trợ chuyển đổi số sang chữ số.
9+
*
10+
* @param $number
11+
* @param string|null $dictionary
12+
* @return string
13+
*/
14+
function n2w($number, string $dictionary = null): string
15+
{
16+
$currentDictionary = N2W::$dictionary;
17+
N2W::$dictionary = $dictionary;
18+
$result = N2W::toWords($number);
19+
N2W::$dictionary = $currentDictionary;
20+
21+
return $result;
22+
}
23+
24+
}
25+
26+
if (!function_exists('n2c')) {
27+
28+
/**
29+
* Hàm hổ trợ chuyển đổi số sang tiền tệ.
30+
*
31+
* @param $number
32+
* @param string|null $dictionary
33+
* @param null|string|array $unit
34+
* @return string
35+
*/
36+
function n2c($number, $unit = null, string $dictionary = null): string
37+
{
38+
$currentDictionary = N2W::$dictionary;
39+
N2W::$dictionary = $dictionary;
40+
$result = N2W::toCurrency($number, $unit);
41+
N2W::$dictionary = $currentDictionary;
42+
43+
return $result;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)