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 InvalidArgumentException; |
12
|
|
|
use Illuminate\Support\Facades\Facade; |
13
|
|
|
use PHPViet\NumberToWords\DictionaryInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @method static string toWords($number) |
17
|
|
|
* @method static string toCurrency($number, $unit = 'đồng') |
18
|
|
|
* |
19
|
|
|
* @author Vuong Minh <[email protected]> |
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
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
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.