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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
60ba2049
Commit
60ba2049
authored
Aug 03, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for passing parameters to the format method.
parent
d836654a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
8 deletions
+32
-8
Formatter.php
framework/yii/base/Formatter.php
+22
-8
FormatterTest.php
tests/unit/framework/base/FormatterTest.php
+10
-0
No files found.
framework/yii/base/Formatter.php
View file @
60ba2049
...
@@ -71,22 +71,36 @@ class Formatter extends Component
...
@@ -71,22 +71,36 @@ class Formatter extends Component
}
}
/**
/**
* Formats the value based on the given type.
* Formats the value based on the given
format
type.
* This method will call one of the "as" methods available in this class to do the formatting.
* This method will call one of the "as" methods available in this class to do the formatting.
* For type "xyz", the method "asXyz" will be used. For example, if the
type
is "html",
* For type "xyz", the method "asXyz" will be used. For example, if the
format
is "html",
* then [[asHtml()]] will be used.
Type
names are case insensitive.
* then [[asHtml()]] will be used.
Format
names are case insensitive.
* @param mixed $value the value to be formatted
* @param mixed $value the value to be formatted
* @param string $type the type of the value, e.g., "html", "text".
* @param string|array $format the format of the value, e.g., "html", "text". To specify additional
* parameters of the formatting method, you may use an array. The first element of the array
* specifies the format name, while the rest of the elements will be used as the parameters to the formatting
* method. For example, a format of `array('date', 'Y-m-d')` will cause the invocation of `asDate($value, 'Y-m-d')`.
* @return string the formatting result
* @return string the formatting result
* @throws InvalidParamException if the type is not supported by this class.
* @throws InvalidParamException if the type is not supported by this class.
*/
*/
public
function
format
(
$value
,
$
type
)
public
function
format
(
$value
,
$
format
)
{
{
$method
=
'as'
.
$type
;
if
(
is_array
(
$format
))
{
if
(
!
isset
(
$format
[
0
]))
{
throw
new
InvalidParamException
(
'The $format array must contain at least one element.'
);
}
$f
=
$format
[
0
];
$format
[
0
]
=
$value
;
$params
=
$format
;
$format
=
$f
;
}
else
{
$params
=
array
(
$value
);
}
$method
=
'as'
.
$format
;
if
(
method_exists
(
$this
,
$method
))
{
if
(
method_exists
(
$this
,
$method
))
{
return
$this
->
$method
(
$value
);
return
call_user_func_array
(
array
(
$this
,
$method
),
$params
);
}
else
{
}
else
{
throw
new
InvalidParamException
(
"Unknown type:
$
type
"
);
throw
new
InvalidParamException
(
"Unknown type:
$
format
"
);
}
}
}
}
...
...
tests/unit/framework/base/FormatterTest.php
View file @
60ba2049
...
@@ -189,4 +189,14 @@ class FormatterTest extends TestCase
...
@@ -189,4 +189,14 @@ class FormatterTest extends TestCase
$this
->
assertSame
(
"123123,12"
,
$this
->
formatter
->
asNumber
(
$value
,
2
));
$this
->
assertSame
(
"123123,12"
,
$this
->
formatter
->
asNumber
(
$value
,
2
));
$this
->
assertSame
(
$this
->
formatter
->
nullDisplay
,
$this
->
formatter
->
asNumber
(
null
));
$this
->
assertSame
(
$this
->
formatter
->
nullDisplay
,
$this
->
formatter
->
asNumber
(
null
));
}
}
public
function
testFormat
()
{
$value
=
time
();
$this
->
assertSame
(
date
(
'Y/m/d'
,
$value
),
$this
->
formatter
->
format
(
$value
,
'date'
));
$this
->
assertSame
(
date
(
'Y/m/d'
,
$value
),
$this
->
formatter
->
format
(
$value
,
'DATE'
));
$this
->
assertSame
(
date
(
'Y-m-d'
,
$value
),
$this
->
formatter
->
format
(
$value
,
array
(
'date'
,
'Y-m-d'
)));
$this
->
setExpectedException
(
'\yii\base\InvalidParamException'
);
$this
->
assertSame
(
date
(
'Y-m-d'
,
$value
),
$this
->
formatter
->
format
(
$value
,
'data'
));
}
}
}
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