Skip to content

Commit

Permalink
Merge pull request #1 from phpviet/analysis-XpavR2
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
vuongxuongminh authored Jun 5, 2019
2 parents e8e0c2f + 4b39a96 commit 2284261
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 57 deletions.
10 changes: 3 additions & 7 deletions src/Concerns/TripletTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
trait TripletTransformer
{

/**
* Trả về từ điển hổ trợ cho việc chuyển đổi.
*
Expand All @@ -37,13 +36,12 @@ protected function tripletToWords(int $triplet, bool $isFirst, int $exponent): s
[$hundred, $ten, $unit] = $this->splitTriplet($triplet);
$dictionary = $this->getDictionary();

if (0 < $hundred || !$isFirst) {
if (0 < $hundred || ! $isFirst) {
$words[] = $dictionary->getTripletHundred($hundred);

if (0 === $ten && 0 < $unit) {
$words[] = $dictionary->tripletTenSeparator();
}

}

if (0 < $ten) {
Expand All @@ -67,8 +65,8 @@ protected function tripletToWords(int $triplet, bool $isFirst, int $exponent): s
*/
private function splitTriplet(int $triplet): array
{
$hundred = (int)($triplet / 100) % 10;
$ten = (int)($triplet / 10) % 10;
$hundred = (int) ($triplet / 100) % 10;
$ten = (int) ($triplet / 10) % 10;
$unit = $triplet % 10;

return [$hundred, $ten, $unit];
Expand Down Expand Up @@ -101,6 +99,4 @@ private function getTripletUnit(int $unit, int $ten): string

return $dictionary->getTripletUnit($unit);
}


}
5 changes: 1 addition & 4 deletions src/Concerns/TripletsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
trait TripletsConverter
{

/**
* Chia số truyền vào thành các cụm gồm 3 số để hổ trợ cho việc chuyển sang chữ số.
*
Expand All @@ -26,11 +25,9 @@ protected function numberToTriplets(int $number): array

while (0 < $number) {
array_unshift($result, $number % 1000);
$number = (int)($number / 1000);
$number = (int) ($number / 1000);
}

return $result;
}


}
40 changes: 20 additions & 20 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Dictionary implements DictionaryInterface
'sáu',
'bảy',
'tám',
'chín'
'chín',
];

/**
Expand All @@ -48,7 +48,7 @@ class Dictionary implements DictionaryInterface
'sáu mươi',
'bảy mươi',
'tám mươi',
'chín mươi'
'chín mươi',
];

/**
Expand All @@ -69,115 +69,115 @@ class Dictionary implements DictionaryInterface
'triệu',
'tỷ',
'nghìn tỷ',
'triệu tỷ'
'triệu tỷ',
];

/**
* @inheritDoc
* {@inheritdoc}
*/
public function zero(): string
{
return static::$tripletUnits[0];
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function minus(): string
{
return 'âm';
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function separator(): string
{
return ' ';
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function tripletTenSeparator(): string
{
return 'linh';
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function specialTripletUnitOne(): string
{
return 'mốt';
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function specialTripletUnitFour(): string
{
return '';
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function specialTripletUnitFive(): string
{
return 'lăm';
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function fraction(): string
{
return 'phẩy';
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function getTripletUnit(int $unit): string
{
if (!isset(static::$tripletUnits[$unit])) {
if (! isset(static::$tripletUnits[$unit])) {
throw new InvalidArgumentException(sprintf('Unit arg (`%s`) must be in 0-9 range!', $unit));
}

return static::$tripletUnits[$unit];
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function getTripletTen(int $ten): string
{
if (!isset(static::$tripletTens[$ten])) {
if (! isset(static::$tripletTens[$ten])) {
throw new InvalidArgumentException(sprintf('Ten arg (`%s`) must be in 0-9 range!', $ten));
}

return static::$tripletTens[$ten];
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function getTripletHundred(int $hundred): string
{
if (!isset(static::$tripletUnits[$hundred])) {
if (! isset(static::$tripletUnits[$hundred])) {
throw new InvalidArgumentException(sprintf('Hundred arg (`%s`) must be in 0-9 range!', $hundred));
}

return static::$tripletUnits[$hundred] . $this->separator() . static::$hundred;
return static::$tripletUnits[$hundred].$this->separator().static::$hundred;
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function getExponent(int $power): string
{
if (!isset(static::$exponents[$power])) {
if (! isset(static::$exponents[$power])) {
throw new InvalidArgumentException(sprintf('Power arg (`%s`) not exist in vietnamese dictionary!', $power));
}

Expand Down
1 change: 0 additions & 1 deletion src/DictionaryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,4 @@ public function getTripletHundred(int $hundred): string;
* @return string
*/
public function getExponent(int $power): string;

}
8 changes: 3 additions & 5 deletions src/SouthDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
class SouthDictionary extends Dictionary
{

/**
* Thay nghìn => ngàn.
*
Expand All @@ -25,23 +24,22 @@ class SouthDictionary extends Dictionary
'triệu',
'tỷ',
'nghìn tỷ',
'triệu tỷ'
'triệu tỷ',
];

/**
* @inheritDoc
* {@inheritdoc}
*/
public function tripletTenSeparator(): string
{
return 'lẻ';
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function specialTripletUnitFour(): string
{
return 'bốn';
}

}
11 changes: 3 additions & 8 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
class Transformer
{

use TripletsConverter;
use TripletTransformer;

Expand Down Expand Up @@ -48,7 +47,7 @@ public function __construct(?DictionaryInterface $dictionary = null)
*/
public function toWords($number): string
{
if (!is_numeric($number)) {
if (! is_numeric($number)) {
throw new InvalidArgumentException(sprintf('Number arg (`%s`) must be numeric!', $number));
}

Expand All @@ -67,11 +66,9 @@ public function toWords($number): string
$triplets = $this->numberToTriplets($number);

foreach ($triplets as $pos => $triplet) {

if (0 < $triplet) {
$words[] = $this->tripletToWords($triplet, 0 === $pos, count($triplets) - $pos - 1);
}

}

if (0 < $decimal) {
Expand Down Expand Up @@ -118,7 +115,7 @@ public function toCurrency($number, $unit = 'đồng'): string
protected function resolve($number): array
{
$number += 0; // trick xóa các số 0 lẻ sau cùng của phân số
$number = (string)$number;
$number = (string) $number;

if (false !== strpos($number, '.')) {
$result = explode('.', $number, 2);
Expand All @@ -130,12 +127,10 @@ protected function resolve($number): array
}

/**
* @inheritDoc
* {@inheritdoc}
*/
protected function getDictionary(): DictionaryInterface
{
return $this->dictionary;
}

}

6 changes: 2 additions & 4 deletions tests/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
class CurrencyTest extends TestCase
{

/**
* @dataProvider dataProvider
*/
Expand Down Expand Up @@ -132,11 +131,10 @@ public function usdDataProvider(): array
['bảy nghìn bốn trăm bốn mươi tư đô hai mươi sáu xen', 7444.26],
['một nghìn bốn trăm bảy mươi tư đô sáu xen', 1474.60],
['bảy nghìn bảy trăm bảy mươi đô', 7770],
['ba nghìn bảy trăm chín mươi hai đô', 3792]
['ba nghìn bảy trăm chín mươi hai đô', 3792],
];
}


public function dataProvider(): array
{
return [
Expand Down Expand Up @@ -238,7 +236,7 @@ public function dataProvider(): array
['năm triệu sáu trăm chín mươi nghìn bảy trăm đồng', 5690700],
['chín trăm mười bảy triệu bảy trăm đồng', 917000700],
['năm trăm nghìn bảy trăm đồng', 500700],
['mười bảy triệu bảy trăm nghìn một trăm đồng', 17700100]
['mười bảy triệu bảy trăm nghìn một trăm đồng', 17700100],
];
}
}
7 changes: 3 additions & 4 deletions tests/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
class NumberTest extends TestCase
{

/**
* @dataProvider dataProvider
*/
Expand Down Expand Up @@ -140,7 +139,7 @@ public function fractionDataProvider(): array
['năm trăm ba mươi phẩy tám trăm ba mươi sáu', 530.836],
['năm mươi lăm phẩy mười chín', 55.190],
['hai trăm tám mươi chín phẩy sáu trăm ba mươi mốt', 289.631],
['năm trăm linh một phẩy bảy trăm mười bốn', 501.714]
['năm trăm linh một phẩy bảy trăm mười bốn', 501.714],
];
}

Expand Down Expand Up @@ -246,7 +245,7 @@ public function minusDataProvider(): array
['âm ba', -3],
['âm hai', -2],
['âm một', -1],
['không', 0]
['không', 0],
];
}

Expand Down Expand Up @@ -375,7 +374,7 @@ public function dataProvider(): array
['một tỷ không trăm bảy mươi sáu', 1000000076],
['một tỷ không trăm bảy mươi bảy', 1000000077],
['một tỷ không trăm bảy mươi tám', 1000000078],
['một tỷ không trăm bảy mươi chín', 1000000079]
['một tỷ không trăm bảy mươi chín', 1000000079],
];
}
}
3 changes: 1 addition & 2 deletions tests/SouthDictionaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
class SouthDictionaryTest extends BaseTestCase
{

/**
* @dataProvider dataProvider
*/
Expand Down Expand Up @@ -129,7 +128,7 @@ public function dataProvider()
['sáu mươi ngàn năm trăm lẻ ba', 60503],
['ba mươi lăm ngàn hai trăm chín mươi bốn', 35294],
['tám mươi tám ngàn bốn trăm hai mươi bảy', 88427],
['sáu trăm năm mươi ngàn sáu trăm bảy mươi ba', 650673]
['sáu trăm năm mươi ngàn sáu trăm bảy mươi ba', 650673],
];
}
}
Loading

0 comments on commit 2284261

Please sign in to comment.