N2W::__call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * @link https://github.com/phpviet/yii-number-to-words
4
 *
5
 * @copyright (c) PHP Viet
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace phpviet\yii\numberToWords;
10
11
use Yii;
12
use yii\base\Component;
13
use yii\base\InvalidConfigException;
14
use PHPViet\NumberToWords\Dictionary;
15
use PHPViet\NumberToWords\Transformer;
16
use PHPViet\NumberToWords\SouthDictionary;
17
18
/**
19
 * @method string toWords($number)
20
 * @method string toCurrency($number, $unit = 'đồng')
21
 *
22
 * @author Vuong Minh <[email protected]>
23
 * @since 1.0.0
24
 */
25
class N2W extends Component
26
{
27
    /**
28
     * Hằng khai báo từ điển trong Nam.
29
     */
30
    const SOUTH_DICTIONARY = 'south';
31
32
    /**
33
     * Hằng khai báo từ điển tiêu chuẩn.
34
     */
35
    const STANDARD_DICTIONARY = 'standard';
36
37
    /**
38
     * Từ điển sử dụng để đọc số.
39
     *
40
     * @var string
41
     */
42
    public $dictionary = self::STANDARD_DICTIONARY;
43
44
    /**
45
     * Danh sách các từ điển đã thiết lập.
46
     *
47
     * @var array
48
     */
49
    protected $dictionaries = [];
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function init(): void
55
    {
56
        if (empty($this->dictionaryClasses)) {
0 ignored issues
show
Bug introduced by
The property dictionaryClasses does not seem to exist. Did you mean dictionary?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
57
            $this->setDictionaries([]);
58
        }
59
60
        parent::init();
61
    }
62
63
    /**
64
     * Hổ trợ việc gọi các phương thức bên trong transformer.
65
     *
66
     * @param string $name
67
     * @param array $params
68
     * @return mixed
69
     */
70
    public function __call($name, $params)
71
    {
72
        $transformer = $this->getTransformer();
73
74
        if (method_exists($transformer, $name)) {
75
            return call_user_func_array([$transformer, $name], $params);
76
        }
77
78
        return parent::__call($name, $params);
79
    }
80
81
    /**
82
     * Trả về đối tượng giúp cho việc chuyển đổi số sang chữ số.
83
     *
84
     * @return Transformer
85
     */
86
    protected function getTransformer(): Transformer
87
    {
88
        if (! isset($this->dictionaries[$this->dictionary])) {
89
            throw new InvalidConfigException(sprintf('Dictionary (%s) is not defined!', $this->dictionary));
90
        }
91
92
        $dictionary = Yii::createObject($this->dictionaries[$this->dictionary]);
93
94
        return Yii::createObject(Transformer::class, [$dictionary]);
95
    }
96
97
    /**
98
     * Thiết lập các từ điển.
99
     *
100
     * @param array $dictionaries
101
     */
102
    public function setDictionaries(array $dictionaries): void
103
    {
104
        $this->dictionaries = array_merge($this->defaultDictionaries(), $dictionaries);
105
    }
106
107
    /**
108
     * Danh sách từ điển mặc định.
109
     *
110
     * @return array
111
     */
112
    protected function defaultDictionaries(): array
113
    {
114
        return [
115
            self::SOUTH_DICTIONARY => SouthDictionary::class,
116
            self::STANDARD_DICTIONARY => Dictionary::class,
117
        ];
118
    }
119
}
120