Skip to content

Commit bca8f85

Browse files
update
1 parent 21bee70 commit bca8f85

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

NumberToWords.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class NumberToWords {
2525
*/
2626
static private $currencyData;
2727

28+
/**
29+
* Formatted text data
30+
*
31+
* @var mixed
32+
*/
33+
static private $text;
34+
2835
/**
2936
* Allow Cents
3037
* @param bool|null $cents
@@ -34,11 +41,11 @@ class NumberToWords {
3441
*/
3542
static public function cents($cents = null)
3643
{
37-
$self = clone new self();
44+
$clone = self::clone();
3845

3946
self::$allowCents = $cents;
4047

41-
return $self;
48+
return $clone;
4249
}
4350

4451
/**
@@ -51,11 +58,11 @@ static public function cents($cents = null)
5158
*/
5259
static public function iso($code = null)
5360
{
54-
$self = clone new self();
61+
$clone = self::clone();
5562

5663
self::$currencyData = self::getCurrencyText($code);
5764

58-
return $self;
65+
return $clone;
5966
}
6067

6168
/**
@@ -67,10 +74,12 @@ static public function iso($code = null)
6774
* @param bool|null $cents
6875
* - [optional] Default is false
6976
*
70-
* @return string
77+
* @return $this
7178
*/
72-
static public function text($number, $cents = null)
79+
static public function number($number, $cents = null)
7380
{
81+
$clone = self::clone();
82+
7483
if(is_null(self::$allowCents) && is_bool($cents)){
7584
self::$allowCents = $cents;
7685
}
@@ -79,7 +88,7 @@ static public function text($number, $cents = null)
7988
$number = Str::trim($number);
8089

8190
// if cents is allowed
82-
if(self::$allowCents){
91+
if(self::$allowCents){
8392

8493
// get name of currency
8594
$currencyText = self::$currencyData['name'] ?? null;
@@ -98,7 +107,20 @@ static public function text($number, $cents = null)
98107
Str::replace(["\n", "\r"], '', $numberText)
99108
);
100109

101-
return ucfirst($numberText) . $currencyText . self::toCents($number);
110+
// add to text
111+
self::$text = ucfirst($numberText) . $currencyText . self::toCents($number);
112+
113+
return $clone;
114+
}
115+
116+
/**
117+
* Translate text into readable formats
118+
*
119+
* @return string|null
120+
*/
121+
static public function translate()
122+
{
123+
return self::$text;
102124
}
103125

104126
/**
@@ -233,4 +255,14 @@ static public function getCurrencyText($code = null)
233255
return Str::convertArrayCase($data, 'lower', 'lower');
234256
}
235257

258+
/**
259+
* Clone self
260+
*
261+
* @return $this
262+
*/
263+
static private function clone()
264+
{
265+
return clone new self();
266+
}
267+
236268
}

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,40 @@ composer require tamedevelopers/support
5757

5858

5959
## Number to Words
60+
- Has three chainable methods
6061

61-
| iso(country iso3) | cents | text |
62-
|---------------------------|-------|------|
63-
| COUNTRY ISO 3 `AFG\|NGA\|GBR\|USA ` | Get abs |
62+
| iso (country iso3) | cents | number |
63+
|----------------------------|-------|--------|
64+
| COUNTRY ISO 3 `AFG \| NGA \| GBR \| USA ` | `true \| false` | numeric figures ('4,200') |
65+
| If so | | |
6466

6567

68+
### ISO
69+
- Takes param as `string` and case-insensitive
70+
71+
```
72+
NumberToWords::iso('nga')
73+
```
74+
75+
### Cents
76+
- Takes param as `boolean`. Default is `false`
77+
78+
```
79+
NumberToWords::cents(true)
80+
```
81+
82+
### Number
83+
- Takes two param as `int\|float\|string` and `boolean`
84+
85+
```
86+
NumberToWords::number(1290)->translate()
87+
```
88+
89+
### Usage
90+
```
91+
NumberToWords()->iso('TUR')->number('120.953', true)->translate()
92+
```
93+
6694

6795
## Asset
6896
- Takes a param as `string` path to asset file

Tests/currency.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
dd(
99

10-
NumberToWords::iso('nga')->text(1205435349345443534, true),
10+
NumberToWords::iso('nga')->number(1205435349345443534, true)->translate(),
1111

12-
NumberToWords::cents(true)->iso('nga')->text(455987.09),
12+
NumberToWords::cents(true)->iso('nga')->number(455987.09)->translate(),
1313

14-
NumberToWords::cents(true)->iso('FRA')->text(34590323),
14+
NumberToWords::iso('FRA')->number(34590323, true)->translate(),
1515

16-
NumberToWords::cents(true)->iso('TUR')->text('120.953'),
16+
NumberToWords::iso('TUR')->number('120.953', true)->translate(),
1717

18-
NumberToWords()->cents(false)->text(1999),
18+
NumberToWords()->number(1999),
1919

2020
// NumberToWords()->CurrencyNames()
2121

0 commit comments

Comments
 (0)