Completed
Pull Request — master (#4)
by
unknown
09:27
created

Transformer::getDictionary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/phpviet/number-to-words
4
 * @copyright (c) PHP Viet
5
 * @license [MIT](http://www.opensource.org/licenses/MIT)
6
 */
7
8
namespace PHPViet\NumberToWords;
9
10
/**
11
 * @author Vuong Minh <[email protected]>
12
 * @since 1.0.0
13
 */
14
class Transformer
15
{
16
    use Concerns\Collapse;
17
    use Concerns\NumberResolver;
18
    use Concerns\TripletsConverter;
19
    use Concerns\TripletTransformer;
20
21
    /**
22
     * @var DictionaryInterface
23
     */
24
    protected $dictionary;
25
26
    /**
27
     * Tạo đối tượng mới với từ điển chỉ định.
28
     *
29
     * @param  DictionaryInterface  $dictionary
30
     */
31
    public function __construct(?DictionaryInterface $dictionary = null)
32
    {
33
        if (null === $dictionary) {
34
            $dictionary = new Dictionary();
35
        }
36
37
        $this->dictionary = $dictionary;
38
    }
39
40
    /**
41
     * Chuyển đổi số sang chữ số.
42
     *
43
     * @param  int|float|string  $number
44
     * @return string
45
     * @throws \InvalidArgumentException
46
     */
47
    public function toWords($number): string
48
    {
49
        [$minus, $number, $decimal] = $this->resolveNumber($number);
0 ignored issues
show
Bug introduced by
The variable $minus does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $decimal does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
50
        $words[] = $minus ? $this->dictionary->minus() : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$words was never initialized. Although not strictly required by PHP, it is generally a good practice to add $words = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
51
52
        if (0 === $number) {
53
            $words[] = $this->dictionary->zero();
54
        }
55
56
        $triplets = $this->numberToTriplets($number);
57
58
        foreach ($triplets as $pos => $triplet) {
59
            if (0 < $triplet) {
60
                $words[] = $this->tripletToWords($triplet, 0 === $pos, count($triplets) - $pos - 1);
61
            }
62
        }
63
64
        if (0 < $decimal) {
65
            $words[] = $this->dictionary->fraction();
66
            $words[] = $this->toWords($decimal);
67
        }
68
69
        return $this->collapseWords($words);
70
    }
71
72
    /**
73
     * Chuyển đổi số sang chữ số kết hợp với đơn vị tiền tệ.
74
     *
75
     * @param $number
76
     * @param  array|string[]|string  $unit
77
     * @return string
78
     * @throws \InvalidArgumentException
79
     */
80
    public function toCurrency($number, $unit = 'đồng'): string
81
    {
82
        $unit = (array) $unit;
83
        $originNumber = $number;
84
        [$minus, $number, $decimal] = $this->resolveNumber($number);
0 ignored issues
show
Bug introduced by
The variable $minus does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $decimal does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
85
86
        if (0 === $decimal || ! isset($unit[1])) {
87
            $words[] = $this->toWords($originNumber);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$words was never initialized. Although not strictly required by PHP, it is generally a good practice to add $words = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
88
            $words[] = $unit[0];
89
        } else {
90
            [$unit, $decimalUnit] = $unit;
0 ignored issues
show
Bug introduced by
The variable $decimalUnit does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
91
            $words[] = $minus ? $this->dictionary->minus() : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$words was never initialized. Although not strictly required by PHP, it is generally a good practice to add $words = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
92
            $words[] = $this->toWords($number);
93
            $words[] = $unit;
94
            $words[] = $this->toWords($decimal);
95
            $words[] = $decimalUnit;
96
        }
97
98
        return $this->collapseWords($words);
99
    }
100
}
101