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 Illuminate\Support\Facades\Facade; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use PHPViet\NumberToWords\DictionaryInterface; |
14
|
|
|
use LogicException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @method static string toWords($number) |
18
|
|
|
* @method static string toCurrency($number, $unit = 'đồng') |
19
|
|
|
* |
20
|
|
|
* @author Vuong Minh <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class N2WFacade 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
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
protected static function getFacadeAccessor(): Transformer |
36
|
|
|
{ |
37
|
|
|
static::checkConfigIsPublished(); |
38
|
|
|
|
39
|
|
|
$dictionary = static::$dictionary ?? static::getDefaultDictionary(); |
40
|
|
|
$dictionary = static::makeDictionary($dictionary); |
|
|
|
|
41
|
|
|
|
42
|
|
|
return app('n2w', compact('dictionary')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Trả về từ điển mặc định trong config. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected static function getDefaultDictionary(): string |
51
|
|
|
{ |
52
|
|
|
return config('n2w.defaults.dictionary'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Tạo từ điển. |
57
|
|
|
* |
58
|
|
|
* @param string $dictionary |
59
|
|
|
* @return DictionaryInterface |
60
|
|
|
*/ |
61
|
|
|
protected static function makeDictionary(string $dictionary): DictionaryInterface |
62
|
|
|
{ |
63
|
|
|
if (! $dictionaryClass = config("n2w.dictionaries.{$dictionary}")) { |
64
|
|
|
throw new InvalidArgumentException(sprintf('Dictionary (%s) is not defined!', $dictionary)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return app()->make($dictionaryClass); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Throw an Exception when the config is not exist |
72
|
|
|
* Please run: php artisan vendor:publish --provider="PHPViet\Laravel\NumberToWords\ServiceProvider" --tag="config" |
73
|
|
|
* |
74
|
|
|
* @throws LogicException |
75
|
|
|
*/ |
76
|
|
|
protected static function checkConfigIsPublished() |
77
|
|
|
{ |
78
|
|
|
if (!config()->has('n2w')) { |
79
|
|
|
throw new LogicException("The config file is not found. You must publish the config before using it!"); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.