Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
9a970370
Commit
9a970370
authored
May 28, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished Formatter.
parent
000fdc3d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
158 additions
and
14 deletions
+158
-14
Formatter.php
framework/yii/base/Formatter.php
+3
-9
Formatter.php
framework/yii/i18n/Formatter.php
+4
-4
FormatterTest.php
tests/unit/framework/base/FormatterTest.php
+58
-1
FormatterTest.php
tests/unit/framework/i18n/FormatterTest.php
+93
-0
No files found.
framework/yii/base/Formatter.php
View file @
9a970370
...
@@ -38,13 +38,6 @@ class Formatter extends Component
...
@@ -38,13 +38,6 @@ class Formatter extends Component
*/
*/
public
$datetimeFormat
=
'Y/m/d h:i:s A'
;
public
$datetimeFormat
=
'Y/m/d h:i:s A'
;
/**
/**
* @var array the format used to format a number with PHP number_format() function.
* Three elements may be specified: "decimals", "decimalSeparator" and "thousandSeparator".
* They correspond to the number of digits after the decimal point, the character displayed as the decimal point
* and the thousands separator character.
*/
public
$numberFormat
=
array
(
'decimals'
=>
null
,
'decimalSeparator'
=>
null
,
'thousandSeparator'
=>
null
);
/**
* @var array the text to be displayed when formatting a boolean value. The first element corresponds
* @var array the text to be displayed when formatting a boolean value. The first element corresponds
* to the text display for false, the second element for true. Defaults to <code>array('No', 'Yes')</code>.
* to the text display for false, the second element for true. Defaults to <code>array('No', 'Yes')</code>.
*/
*/
...
@@ -274,14 +267,15 @@ class Formatter extends Component
...
@@ -274,14 +267,15 @@ class Formatter extends Component
}
}
/**
/**
* Formats the value as a decimal number using the PHP number_format() function.
* Formats the value as a number with decimal and thousand separators.
* This method calls the PHP number_format() function to do the formatting.
* @param mixed $value the value to be formatted
* @param mixed $value the value to be formatted
* @param integer $decimals the number of digits after the decimal point
* @param integer $decimals the number of digits after the decimal point
* @param string $decimalSeparator the character displayed as the decimal point
* @param string $decimalSeparator the character displayed as the decimal point
* @param string $thousandSeparator the character displayed as the thousands separator character.
* @param string $thousandSeparator the character displayed as the thousands separator character.
* @return string the formatted result
* @return string the formatted result
*/
*/
public
function
as
Decimal
(
$value
,
$decimals
=
0
,
$decimalSeparator
=
'.'
,
$thousandSeparator
=
','
)
public
function
as
Number
(
$value
,
$decimals
=
0
,
$decimalSeparator
=
'.'
,
$thousandSeparator
=
','
)
{
{
return
number_format
(
$value
,
$decimals
,
$decimalSeparator
,
$thousandSeparator
);
return
number_format
(
$value
,
$decimals
,
$decimalSeparator
,
$thousandSeparator
);
}
}
...
...
framework/yii/i18n/Formatter.php
View file @
9a970370
...
@@ -170,7 +170,7 @@ class Formatter extends \yii\base\Formatter
...
@@ -170,7 +170,7 @@ class Formatter extends \yii\base\Formatter
*/
*/
public
function
asDecimal
(
$value
,
$format
=
null
)
public
function
asDecimal
(
$value
,
$format
=
null
)
{
{
$this
->
createNumberFormatter
(
NumberFormatter
::
DECIMAL
,
$format
)
->
format
(
$value
);
return
$this
->
createNumberFormatter
(
NumberFormatter
::
DECIMAL
,
$format
)
->
format
(
$value
);
}
}
/**
/**
...
@@ -183,7 +183,7 @@ class Formatter extends \yii\base\Formatter
...
@@ -183,7 +183,7 @@ class Formatter extends \yii\base\Formatter
*/
*/
public
function
asCurrency
(
$value
,
$currency
=
'USD'
,
$format
=
null
)
public
function
asCurrency
(
$value
,
$currency
=
'USD'
,
$format
=
null
)
{
{
$this
->
createNumberFormatter
(
NumberFormatter
::
CURRENCY
,
$format
)
->
formatCurrency
(
$value
,
$currency
);
return
$this
->
createNumberFormatter
(
NumberFormatter
::
CURRENCY
,
$format
)
->
formatCurrency
(
$value
,
$currency
);
}
}
/**
/**
...
@@ -195,7 +195,7 @@ class Formatter extends \yii\base\Formatter
...
@@ -195,7 +195,7 @@ class Formatter extends \yii\base\Formatter
*/
*/
public
function
asPercent
(
$value
,
$format
=
null
)
public
function
asPercent
(
$value
,
$format
=
null
)
{
{
$this
->
createNumberFormatter
(
NumberFormatter
::
PERCENT
,
$format
)
->
format
(
$value
);
return
$this
->
createNumberFormatter
(
NumberFormatter
::
PERCENT
,
$format
)
->
format
(
$value
);
}
}
/**
/**
...
@@ -207,7 +207,7 @@ class Formatter extends \yii\base\Formatter
...
@@ -207,7 +207,7 @@ class Formatter extends \yii\base\Formatter
*/
*/
public
function
asScientific
(
$value
,
$format
=
null
)
public
function
asScientific
(
$value
,
$format
=
null
)
{
{
$this
->
createNumberFormatter
(
NumberFormatter
::
SCIENTIFIC
,
$format
)
->
format
(
$value
);
return
$this
->
createNumberFormatter
(
NumberFormatter
::
SCIENTIFIC
,
$format
)
->
format
(
$value
);
}
}
/**
/**
...
...
tests/unit/framework/base/FormatterTest.php
View file @
9a970370
...
@@ -84,7 +84,7 @@ class FormatterTest extends TestCase
...
@@ -84,7 +84,7 @@ class FormatterTest extends TestCase
public
function
testAsHtml
()
public
function
testAsHtml
()
{
{
// todo
// todo
: dependency on HtmlPurifier
}
}
public
function
testAsEmail
()
public
function
testAsEmail
()
...
@@ -98,4 +98,61 @@ class FormatterTest extends TestCase
...
@@ -98,4 +98,61 @@ class FormatterTest extends TestCase
$value
=
'http://sample.com/img.jpg'
;
$value
=
'http://sample.com/img.jpg'
;
$this
->
assertSame
(
"<img src=
\"
$value
\"
alt=
\"\"
/>"
,
$this
->
formatter
->
asImage
(
$value
));
$this
->
assertSame
(
"<img src=
\"
$value
\"
alt=
\"\"
/>"
,
$this
->
formatter
->
asImage
(
$value
));
}
}
public
function
testAsBoolean
()
{
$value
=
true
;
$this
->
assertSame
(
'Yes'
,
$this
->
formatter
->
asBoolean
(
$value
));
$value
=
false
;
$this
->
assertSame
(
'No'
,
$this
->
formatter
->
asBoolean
(
$value
));
$value
=
"111"
;
$this
->
assertSame
(
'Yes'
,
$this
->
formatter
->
asBoolean
(
$value
));
$value
=
""
;
$this
->
assertSame
(
'No'
,
$this
->
formatter
->
asBoolean
(
$value
));
}
public
function
testAsDate
()
{
$value
=
time
();
$this
->
assertSame
(
date
(
'Y/m/d'
,
$value
),
$this
->
formatter
->
asDate
(
$value
));
$this
->
assertSame
(
date
(
'Y-m-d'
,
$value
),
$this
->
formatter
->
asDate
(
$value
,
'Y-m-d'
));
}
public
function
testAsTime
()
{
$value
=
time
();
$this
->
assertSame
(
date
(
'h:i:s A'
,
$value
),
$this
->
formatter
->
asTime
(
$value
));
$this
->
assertSame
(
date
(
'h:i:s'
,
$value
),
$this
->
formatter
->
asTime
(
$value
,
'h:i:s'
));
}
public
function
testAsDatetime
()
{
$value
=
time
();
$this
->
assertSame
(
date
(
'Y/m/d h:i:s A'
,
$value
),
$this
->
formatter
->
asDatetime
(
$value
));
$this
->
assertSame
(
date
(
'Y-m-d h:i:s'
,
$value
),
$this
->
formatter
->
asDatetime
(
$value
,
'Y-m-d h:i:s'
));
}
public
function
testAsInteger
()
{
$value
=
123
;
$this
->
assertSame
(
"
$value
"
,
$this
->
formatter
->
asInteger
(
$value
));
$value
=
123.23
;
$this
->
assertSame
(
"123"
,
$this
->
formatter
->
asInteger
(
$value
));
$value
=
'a'
;
$this
->
assertSame
(
"0"
,
$this
->
formatter
->
asInteger
(
$value
));
}
public
function
testAsDouble
()
{
$value
=
123.12
;
$this
->
assertSame
(
"123.12"
,
$this
->
formatter
->
asDouble
(
$value
));
$this
->
assertSame
(
"123.1"
,
$this
->
formatter
->
asDouble
(
$value
,
1
));
}
public
function
testAsNumber
()
{
$value
=
123123.123
;
$this
->
assertSame
(
"123,123"
,
$this
->
formatter
->
asNumber
(
$value
));
$this
->
assertSame
(
"123.123,12"
,
$this
->
formatter
->
asNumber
(
$value
,
2
,
','
,
'.'
));
}
}
}
tests/unit/framework/i18n/FormatterTest.php
0 → 100644
View file @
9a970370
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yiiunit\framework\i18n
;
use
yii\i18n\Formatter
;
use
yiiunit\TestCase
;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
FormatterTest
extends
TestCase
{
/**
* @var Formatter
*/
protected
$formatter
;
protected
function
setUp
()
{
parent
::
setUp
();
if
(
!
extension_loaded
(
'intl'
))
{
$this
->
markTestSkipped
(
'intl extension is required.'
);
}
$this
->
mockApplication
();
$this
->
formatter
=
new
Formatter
(
array
(
'locale'
=>
'en_US'
,
));
}
protected
function
tearDown
()
{
parent
::
tearDown
();
$this
->
formatter
=
null
;
}
public
function
testAsDecimal
()
{
$value
=
'123'
;
$this
->
assertSame
(
$value
,
$this
->
formatter
->
asDecimal
(
$value
));
$value
=
'123456'
;
$this
->
assertSame
(
"123,456"
,
$this
->
formatter
->
asDecimal
(
$value
));
$value
=
'-123456.123'
;
$this
->
assertSame
(
"-123,456.123"
,
$this
->
formatter
->
asDecimal
(
$value
));
}
public
function
testAsPercent
()
{
$value
=
'123'
;
$this
->
assertSame
(
'12,300%'
,
$this
->
formatter
->
asPercent
(
$value
));
$value
=
'0.1234'
;
$this
->
assertSame
(
"12%"
,
$this
->
formatter
->
asPercent
(
$value
));
$value
=
'-0.009343'
;
$this
->
assertSame
(
"-1%"
,
$this
->
formatter
->
asPercent
(
$value
));
}
public
function
testAsScientific
()
{
$value
=
'123'
;
$this
->
assertSame
(
'1.23E2'
,
$this
->
formatter
->
asScientific
(
$value
));
$value
=
'123456'
;
$this
->
assertSame
(
"1.23456E5"
,
$this
->
formatter
->
asScientific
(
$value
));
$value
=
'-123456.123'
;
$this
->
assertSame
(
"-1.23456123E5"
,
$this
->
formatter
->
asScientific
(
$value
));
}
public
function
testAsCurrency
()
{
$value
=
'123'
;
$this
->
assertSame
(
'$123.00'
,
$this
->
formatter
->
asCurrency
(
$value
));
$value
=
'123.456'
;
$this
->
assertSame
(
"$123.46"
,
$this
->
formatter
->
asCurrency
(
$value
));
$value
=
'-123456.123'
;
$this
->
assertSame
(
"($123,456.12)"
,
$this
->
formatter
->
asCurrency
(
$value
));
}
public
function
testDate
()
{
$time
=
time
();
$this
->
assertSame
(
date
(
'n/j/y'
,
$time
),
$this
->
formatter
->
asDate
(
$time
));
$this
->
assertSame
(
date
(
'g:i A'
,
$time
),
$this
->
formatter
->
asTime
(
$time
));
$this
->
assertSame
(
date
(
'n/j/y g:i A'
,
$time
),
$this
->
formatter
->
asDatetime
(
$time
));
$this
->
assertSame
(
date
(
'M j, Y'
,
$time
),
$this
->
formatter
->
asDate
(
$time
,
'long'
));
$this
->
assertSame
(
date
(
'g:i:s A T'
,
$time
),
$this
->
formatter
->
asTime
(
$time
,
'long'
));
$this
->
assertSame
(
date
(
'M j, Y g:i:s A T'
,
$time
),
$this
->
formatter
->
asDatetime
(
$time
,
'long'
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment