Skip to content

Commit

Permalink
Merge pull request #6566 from kenjis/refactor-view-filter-round
Browse files Browse the repository at this point in the history
refactor: make $precision int in View Filter round
  • Loading branch information
kenjis authored Sep 23, 2022
2 parents ca4fde6 + eafb028 commit 7959a37
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion system/View/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,18 @@ public static function prose(string $value): string
* - ceil always rounds up
* - floor always rounds down
*
* @param int|string $precision
* @param int|string $precision precision or type
*
* @return float|string
*/
public static function round(string $value, $precision = 2, string $type = 'common')
{
// In case that $precision is a type like `{ value1|round(ceil) }`
if (! is_numeric($precision)) {
$type = $precision;
$precision = 2;
} else {
$precision = (int) $precision;
}

switch ($type) {
Expand Down
8 changes: 5 additions & 3 deletions tests/system/View/ParserFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,15 @@ public function testRound()
$parser = new Parser($this->config, $this->viewsDir, $this->loader);

$data = [
'value1' => 5.55,
'value1' => 5.555,
];

$template = '{ value1|round(1) } { value1|round(1, common) } { value1|round(ceil) } { value1|round(floor) } { value1|round(unknown) }';
$template = '{ value1|round(1) } / { value1|round(1, common) }'
. ' / { value1|round(ceil) } / { value1|round(floor) }'
. ' / { value1|round(unknown) }';

$parser->setData($data);
$this->assertSame('5.6 5.6 6 5 5.55', $parser->renderString($template));
$this->assertSame('5.6 / 5.6 / 6 / 5 / 5.555', $parser->renderString($template));
}

public function testStripTags()
Expand Down

0 comments on commit 7959a37

Please sign in to comment.