From 60ba20491efd83d3bcd51aa68761d26a64f12317 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 3 Aug 2013 16:51:57 -0400 Subject: [PATCH] Added support for passing parameters to the format method. --- framework/yii/base/Formatter.php | 30 +++++++++++++++++++++-------- tests/unit/framework/base/FormatterTest.php | 10 ++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/framework/yii/base/Formatter.php b/framework/yii/base/Formatter.php index 5326ed8..d9b5778 100644 --- a/framework/yii/base/Formatter.php +++ b/framework/yii/base/Formatter.php @@ -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. - * For type "xyz", the method "asXyz" will be used. For example, if the type is "html", - * then [[asHtml()]] will be used. Type names are case insensitive. + * For type "xyz", the method "asXyz" will be used. For example, if the format is "html", + * then [[asHtml()]] will be used. Format names are case insensitive. * @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 * @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)) { - return $this->$method($value); + return call_user_func_array(array($this, $method), $params); } else { - throw new InvalidParamException("Unknown type: $type"); + throw new InvalidParamException("Unknown type: $format"); } } diff --git a/tests/unit/framework/base/FormatterTest.php b/tests/unit/framework/base/FormatterTest.php index b851ae1..01dd682 100644 --- a/tests/unit/framework/base/FormatterTest.php +++ b/tests/unit/framework/base/FormatterTest.php @@ -189,4 +189,14 @@ class FormatterTest extends TestCase $this->assertSame("123123,12", $this->formatter->asNumber($value, 2)); $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')); + } }