Skip to content
This repository was archived by the owner on Jul 1, 2019. It is now read-only.

Commit 245e8c3

Browse files
author
Andrey Helldar
committed
Refactoring and fix tests
1 parent 68c6774 commit 245e8c3

File tree

3 files changed

+118
-114
lines changed

3 files changed

+118
-114
lines changed

src/DigitText.php

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,9 @@ class DigitText
6161
private $is_currency = false;
6262

6363
/**
64-
* @var string
64+
* @var null
6565
*/
66-
private $result = '';
67-
68-
/**
69-
* Return result.
70-
*
71-
* @author Andrey Helldar <helldar@ai-rus.com>
72-
*
73-
* @since 2017-03-27
74-
* @return string
75-
*/
76-
public function get()
77-
{
78-
return $this->result;
79-
}
66+
private $digit = null;
8067

8168
/**
8269
* Set lang.
@@ -85,15 +72,12 @@ public function get()
8572
*
8673
* @since 2017-03-27
8774
*
88-
* @param null $lang
89-
*
90-
* @return $this
75+
* @param string $lang
9176
*/
92-
public function lang($lang = null)
77+
private function lang($lang = 'en')
9378
{
94-
$this->lang = !empty(trim($lang)) ? trim($lang) : $this->lang_fallback;
95-
96-
return $this;
79+
$filename = sprintf("%s/lang/%s.php", __DIR__, trim($lang));
80+
$this->lang = file_exists($filename) ? trim($lang) : $this->lang_fallback;
9781
}
9882

9983
/**
@@ -104,40 +88,42 @@ public function lang($lang = null)
10488
* @since 2017-03-27
10589
*
10690
* @param bool $is_currency
107-
*
108-
* @return $this
10991
*/
110-
public function currency($is_currency = false)
92+
private function currency($is_currency = false)
11193
{
11294
$this->is_currency = (bool) $is_currency;
113-
114-
return $this;
11595
}
11696

11797
/**
11898
* Showing a fractional number in a text equivalent.
11999
*
120-
* @param float $digit
100+
* @author Andrey Helldar <helldar@ai-rus.com>
101+
*
102+
* @since 2017-03-27
121103
*
122-
* @return $this
104+
* @param float $digit
105+
* @param string $lang
106+
* @param bool $is_currency
107+
*
108+
* @return mixed|string|void
123109
*/
124-
public function number($digit = 0.0)
110+
public function get($digit = 0.0, $lang = 'en', $is_currency = false)
125111
{
112+
$this->lang($lang);
113+
$this->currency($is_currency);
114+
$this->fixDigit($digit);
115+
126116
// Return text from php_intl library
127-
$intl = $this->intl($digit);
117+
$intl = $this->intl();
128118
if(!empty($intl)) {
129-
$this->result = $intl;
130-
131-
return $this;
119+
return $intl;
132120
}
133121

134122
// Loading texts from locale page
135123
$this->loadTexts();
136124

137-
if(empty($digit) || $digit == 0) {
138-
$this->result = $this->texts['zero'];
139-
140-
return $this;
125+
if($this->digit == 0) {
126+
return $this->texts['zero'];
141127
}
142128

143129
// Get the fractional part
@@ -154,9 +140,32 @@ public function number($digit = 0.0)
154140
}
155141
}
156142

157-
$this->result = $this->is_currency ? $this->getCurrency($result) : trim($result);
143+
return $this->is_currency ? $this->getCurrency($result) : trim($result);
144+
}
158145

159-
return $this;
146+
/**
147+
* @author Andrey Helldar <helldar@ai-rus.com>
148+
*
149+
* @since 2017-03-27l
150+
*
151+
* @param null $digit
152+
*/
153+
private function fixDigit($digit = null)
154+
{
155+
if(empty($digit)) {
156+
$this->digit = 0;
157+
158+
return;
159+
}
160+
161+
if(strripos((string) $digit, '.') === false) {
162+
$this->digit = intval($digit);
163+
164+
return;
165+
}
166+
167+
$digit = explode('.', str_replace([',', '-', ' ', "'", '`'], '', (string) $digit));
168+
$this->digit = sprintf("%s.%s", intval($digit[0]), intval($digit[1]));
160169
}
161170

162171
/**
@@ -167,15 +176,13 @@ public function number($digit = 0.0)
167176
* @since 2016-11-28
168177
* @since 2017-03-27 Refactoring code.
169178
*
170-
* @param float $digit
171-
*
172179
* @return string|void
173180
*/
174-
private function intl($digit = 0.0)
181+
private function intl()
175182
{
176183
if($this->is_currency) {
177184
if(extension_loaded('php_intl')) {
178-
return (new \MessageFormatter($this->lang, '{n, spellout}'))->format(array('n' => $digit));
185+
return (new \MessageFormatter($this->lang, '{n, spellout}'))->format(array('n' => $this->digit));
179186
}
180187
}
181188

@@ -191,8 +198,8 @@ private function intl($digit = 0.0)
191198
*/
192199
private function loadTexts()
193200
{
194-
$locale = sprintf("%s/lang/%s.php", __DIR__, $this->lang);
195-
$lang = file_exists($locale) ? $this->lang : $this->lang_fallback;
201+
$filename = sprintf("%s/lang/%s.php", __DIR__, $this->lang);
202+
$lang = file_exists($filename) ? $this->lang : $this->lang_fallback;
196203
$this->texts = require sprintf("%s/lang/%s.php", __DIR__, $lang);
197204
}
198205

src/Facade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ class Facade
1818
*/
1919
public static function digit($digit = null)
2020
{
21-
return (new DigitText())->number($digit);
21+
return (new DigitText())->get($digit);
2222
}
2323
}

tests/DigitTextTest.php

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,106 +14,103 @@ class DigitTextTest extends PHPUnit_Framework_TestCase
1414
*/
1515
protected $object;
1616

17-
/**
18-
* @covers Helldar\DigitText\DigitText::text
19-
*/
20-
public function testText()
21-
{
22-
$this->testRu();
23-
$this->testEn();
24-
}
25-
2617
/**
2718
* Russian localization testing.
2819
*
2920
* @author Andrey Helldar <helldar@ai-rus.com>
3021
*
31-
* @version 2016-11-28
22+
* @since 2016-11-28
23+
* @since 2017-03-27
3224
*
3325
* @since 1.0
3426
*/
3527
public function testRu()
3628
{
3729
$result = array(
38-
$this->object->number(null, 'ru') => 'ноль',
39-
$this->object->number(64.23, 'ru', true) => 'шестьдесят четыре руб 23 коп',
40-
$this->object->number(764, 'ru') => 'семьсот шестьдесят четыре',
41-
$this->object->number(2866, 'ru') => 'две тысячи восемьсот шестьдесят шесть',
42-
$this->object->number(7700, 'ru') => 'семь тысяч семьсот',
43-
$this->object->number('10,000', 'ru') => 'десять тысяч',
44-
$this->object->number(14383, 'ru') => 'четырнадцать тысячи триста восемьдесят три',
45-
$this->object->number(20383, 'ru') => 'двадцать тысяч триста восемьдесят три',
46-
$this->object->number(700383, 'ru') => 'семьсот тысяч триста восемьдесят три',
47-
$this->object->number(7644383, 'ru') => 'семь миллионов шестьсот сорок четыре тысячи триста восемьдесят три',
48-
$this->object->number(70043783.65, 'ru', true) => 'семьдесят миллионов сорок три тысячи семьсот восемьдесят три руб 65 коп',
49-
$this->object->number(786443783, 'ru') => 'семьсот восемьдесят шесть миллионов четыреста сорок три тысячи семьсот восемьдесят три',
50-
$this->object->number(109, 'ru') => 'сто девять',
51-
$this->object->number(110, 'ru') => 'сто десять',
52-
$this->object->number(111, 'ru') => 'сто одиннадцать',
53-
$this->object->number(112, 'ru') => 'сто двенадцать',
54-
$this->object->number(116, 'ru') => 'сто шестнадцать',
55-
$this->object->number(118, 'ru') => 'сто восемнадцать',
56-
$this->object->number(120, 'ru') => 'сто двадцать',
57-
$this->object->number(121, 'ru') => 'сто двадцать один',
30+
$this->object->get(0, 'ru') => 'ноль',
31+
$this->object->get(64.23, 'ru', true) => 'шестьдесят четыре руб 23 коп',
32+
$this->object->get(764, 'ru') => 'семьсот шестьдесят четыре',
33+
$this->object->get(2866, 'ru') => 'две тысячи восемьсот шестьдесят шесть',
34+
$this->object->get(7700, 'ru') => 'семь тысяч семьсот',
35+
$this->object->get('10,000', 'ru') => 'десять тысяч',
36+
$this->object->get(14383, 'ru') => 'четырнадцать тысячи триста восемьдесят три',
37+
$this->object->get(20383, 'ru') => 'двадцать тысяч триста восемьдесят три',
38+
$this->object->get(700383, 'ru') => 'семьсот тысяч триста восемьдесят три',
39+
$this->object->get(7644383, 'ru') => 'семь миллионов шестьсот сорок четыре тысячи триста восемьдесят три',
40+
$this->object->get(70043783.65, 'ru', true) => 'семьдесят миллионов сорок три тысячи семьсот восемьдесят три руб 65 коп',
41+
$this->object->get(786443783, 'ru') => 'семьсот восемьдесят шесть миллионов четыреста сорок три тысячи семьсот восемьдесят три',
42+
$this->object->get(109, 'ru') => 'сто девять',
43+
$this->object->get(110, 'ru') => 'сто десять',
44+
$this->object->get(111, 'ru') => 'сто одиннадцать',
45+
$this->object->get(112, 'ru') => 'сто двенадцать',
46+
$this->object->get(116, 'ru') => 'сто шестнадцать',
47+
$this->object->get(118, 'ru') => 'сто восемнадцать',
48+
$this->object->get(120, 'ru') => 'сто двадцать',
49+
$this->object->get(121, 'ru') => 'сто двадцать один',
50+
$this->object->get(10010, 'ru') => 'десять тысяч десять',
51+
$this->object->get(10110, 'ru') => 'десять тысяч сто десять',
52+
$this->object->get(510110, 'ru') => 'пятьсот десять тысяч сто десять',
5853
);
5954

60-
$this->testDigits($result);
55+
$this->runTestDigits($result);
6156
}
6257

6358
/**
64-
* Testing the translation of numbers to text equivalent.
59+
* English localization testing.
6560
*
6661
* @author Andrey Helldar <helldar@ai-rus.com>
6762
*
6863
* @version 2016-11-28
6964
*
7065
* @since 1.0
71-
*
72-
* @param array $items
7366
*/
74-
public function testDigits($items = array())
67+
public function testEn()
7568
{
76-
foreach ($items as $key => $result) {
77-
$this->assertEquals($result, $key);
78-
}
69+
$result = array(
70+
$this->object->get() => 'zero',
71+
$this->object->get(64.23, 'en', true) => 'sixty four dollars 23 cents',
72+
$this->object->get(764) => 'seven hundred sixty four',
73+
$this->object->get(2866) => 'two thousands eight hundred sixty six',
74+
$this->object->get(7700) => 'seven thousands seven hundred',
75+
$this->object->get('10,000') => 'ten thousands',
76+
$this->object->get(14383) => 'fourteen thousands three hundred eighty three',
77+
$this->object->get(20383) => 'twenty thousands three hundred eighty three',
78+
$this->object->get(700383) => 'seven hundred thousands three hundred eighty three',
79+
$this->object->get(7644383) => 'seven million six hundred forty four thousands three hundred eighty three',
80+
$this->object->get(70043783.65, 'en', true) => 'seventy million forty three thousands seven hundred eighty three dollars 65 cents',
81+
$this->object->get(786443783) => 'seven hundred eighty six million four hundred forty three thousands seven hundred eighty three',
82+
$this->object->get(109) => 'one hundred nine',
83+
$this->object->get(110) => 'one hundred ten',
84+
$this->object->get(111) => 'one hundred eleven',
85+
$this->object->get(112) => 'one hundred twelve',
86+
$this->object->get(115) => 'one hundred fifteen',
87+
$this->object->get(116) => 'one hundred sixteen',
88+
$this->object->get(118) => 'one hundred eighteen',
89+
$this->object->get(120) => 'one hundred twenty',
90+
$this->object->get(121) => 'one hundred twenty one',
91+
$this->object->get(10010) => 'ten thousands ten',
92+
$this->object->get(10110) => 'ten thousands one hundred ten',
93+
$this->object->get(510110) => 'five hundred ten thousands one hundred ten',
94+
);
95+
96+
$this->runTestDigits($result);
7997
}
8098

8199
/**
82-
* English localization testing.
100+
* Testing the translation of numbers to text equivalent.
83101
*
84102
* @author Andrey Helldar <helldar@ai-rus.com>
85103
*
86-
* @version 2016-11-28
104+
* @since 2016-11-28
105+
* @since 2017-03-27
87106
*
88-
* @since 1.0
107+
* @param array $items
89108
*/
90-
public function testEn()
109+
public function runTestDigits($items = array())
91110
{
92-
$result = array(
93-
$this->object->number() => 'zero',
94-
$this->object->number(64.23, 'en', true) => 'sixty four dollars 23 cents',
95-
$this->object->number(764) => 'seven hundred sixty four',
96-
$this->object->number(2866) => 'two thousands eight hundred sixty six',
97-
$this->object->number(7700) => 'seven thousands seven hundred',
98-
$this->object->number('10,000') => 'ten thousands',
99-
$this->object->number(14383) => 'fourteen thousands three hundred eighty three',
100-
$this->object->number(20383) => 'twenty thousands three hundred eighty three',
101-
$this->object->number(700383) => 'seven hundred thousands three hundred eighty three',
102-
$this->object->number(7644383) => 'seven million six hundred forty four thousands three hundred eighty three',
103-
$this->object->number(70043783.65, 'en', true) => 'seventy million forty three thousands seven hundred eighty three dollars 65 cents',
104-
$this->object->number(786443783) => 'seven hundred eighty six million four hundred forty three thousands seven hundred eighty three',
105-
$this->object->number(109) => 'one hundred nine',
106-
$this->object->number(110) => 'one hundred ten',
107-
$this->object->number(111) => 'one hundred eleven',
108-
$this->object->number(112) => 'one hundred twelve',
109-
$this->object->number(115) => 'one hundred fifteen',
110-
$this->object->number(116) => 'one hundred sixteen',
111-
$this->object->number(118) => 'one hundred eighteen',
112-
$this->object->number(120) => 'one hundred twenty',
113-
$this->object->number(121) => 'one hundred twenty one',
114-
);
115-
116-
$this->testDigits($result);
111+
foreach($items as $key => $result) {
112+
$this->assertEquals($result, $key);
113+
}
117114
}
118115

119116
/**

0 commit comments

Comments
 (0)