Skip to content

Commit

Permalink
add support for filter (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrenge authored May 13, 2023
1 parent d8fd54a commit 8000aed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Liquid/StandardFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ public static function raw($input)
}


/**
* Converts into JSON string
*
* @param mixed $input
*
* @return string
*/
public static function json($input)
{
return json_encode($input);
}


/**
* Escape a string
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Liquid/StandardFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,36 @@ public function testRaw()
}
}

public function testJson()
{
$data = array(
array(
"before" => "Anything",
"after" => "\"Anything\"",
),
array(
"before" => 3,
"after" => 3,
),
array(
"before" => array(1, 2, 3),
"after" => "[1,2,3]",
),
array(
"before" => array("one" => 1, "two" => 2, "three" => 3),
"after" => "{\"one\":1,\"two\":2,\"three\":3}",
),
array(
"before" => array("one" => 1, "two" => array(1, 2, 3), "three" => 3),
"after" => "{\"one\":1,\"two\":[1,2,3],\"three\":3}",
),
);

foreach ($data as $testCase) {
$this->assertEquals($testCase['after'], StandardFilters::json($testCase['before']));
}
}

public function testEscape()
{
$data = array(
Expand Down

0 comments on commit 8000aed

Please sign in to comment.