-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added the DecodeJSON filter * Added a test for the filter * Added the documentation for the filter * Fixed the documentation * The filter `$assoc` argument default value is set to `true` * More notes to the documentation * Added a note about why the `$accos` default value is set to true not to forget
- Loading branch information
Showing
4 changed files
with
195 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Particle. | ||
* | ||
* @link http://github.com/particle-php for the canonical source repository | ||
* @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com) | ||
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License | ||
*/ | ||
namespace Particle\Filter\FilterRule; | ||
|
||
use Particle\Filter\FilterRule; | ||
|
||
/** | ||
* Class DecodeJSON | ||
* | ||
* A filter that decodes the given value from JSON. If a value is not a string, it is returned as is. If a value is not | ||
* a correct JSON or if an encoded data is deeper than the recursion limit, `null` is returned. | ||
* | ||
* @package Particle\Filter\FilterRule | ||
*/ | ||
class DecodeJSON extends FilterRule | ||
{ | ||
/** | ||
* @var bool When `true`, decoded objects will be converted into associative arrays | ||
*/ | ||
protected $assoc; | ||
|
||
/** | ||
* @var int Decode recursion depth | ||
*/ | ||
protected $depth; | ||
|
||
/** | ||
* @var int Bitmask of JSON decode options | ||
*/ | ||
protected $options; | ||
|
||
/** | ||
* Set required params for JSON decoding | ||
* | ||
* @param bool $assoc When `true`, decoded objects will be converted into associative arrays | ||
* @param int $depth Decode recursion dept | ||
* @param int $options Bitmask of JSON decode options | ||
* @see http://php.net/manual/en/function.json-decode.php More information about the parameters | ||
*/ | ||
public function __construct($assoc, $depth, $options) | ||
{ | ||
$this->assoc = $assoc; | ||
$this->depth = $depth; | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* Decodes the value JSON | ||
* | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
public function filter($value) | ||
{ | ||
if (!is_string($value)) { | ||
return $value; | ||
} | ||
|
||
return json_decode($value, $this->assoc, $this->depth, $this->options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
namespace Particle\Filter\Tests\FilterRule; | ||
|
||
use Particle\Filter\Filter; | ||
|
||
class DecodeJSONTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Filter | ||
*/ | ||
protected $filter; | ||
|
||
/** | ||
* Prepare the filter | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->filter = new Filter(); | ||
} | ||
|
||
/** | ||
* @dataProvider getTestParams | ||
* @param mixed $value | ||
* @param bool $assoc | ||
* @param int $depth | ||
* @param int $options | ||
* @param mixed $filteredValue | ||
*/ | ||
public function testDecodeJSONFilterRule($value, $assoc, $depth, $options, $filteredValue) | ||
{ | ||
$this->filter->value('test')->decodeJSON($assoc, $depth, $options); | ||
|
||
$result = $this->filter->filter([ | ||
'test' => $value | ||
]); | ||
|
||
$this->assertEquals($filteredValue, $result['test']); | ||
} | ||
|
||
public function testDecodeJSONFilterRuleWithBigInt() | ||
{ | ||
$this->filter->value('test1')->decodeJSON(false, 100, 0); | ||
$this->filter->value('test2')->decodeJSON(false, 100, JSON_BIGINT_AS_STRING); | ||
|
||
$result = $this->filter->filter([ | ||
'test1' => '1000000000000000000000000000000', | ||
'test2' => '1000000000000000000000000000000' | ||
]); | ||
|
||
$this->assertInternalType('float', $result['test1']); | ||
$this->assertInternalType('string', $result['test2']); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getTestParams() | ||
{ | ||
return [ | ||
// Ordinary JSON | ||
[ | ||
'{"string": "foo", "int": 100500, "array": [false, true, null]}', | ||
false, | ||
100, | ||
0, | ||
(object)['string' => 'foo', 'int' => 100500, 'array' => [false, true, null]] | ||
], | ||
['"Hello"', false, 100, 0, 'Hello'], | ||
['1999', false, 100, 0, 1999], | ||
|
||
// Using the assoc parameter | ||
['{"string": "foo", "int": 100500}', true, 100, 0, ['string' => 'foo', 'int' => 100500]], | ||
|
||
// Using the depth parameter | ||
['{"1": {"2": {"3": {"4": "the end"}}}}', true, 100, 0, [1 => [2 => [3 => [4 => "the end"]]]]], | ||
['{"1": {"2": {"3": {"4": "the end"}}}}', true, 3, 0, null], | ||
|
||
// Incorrect JSON | ||
['I am not a JSON', false, 100, 0, null], | ||
['', false, 100, 0, null], | ||
|
||
// Not a string | ||
[null, false, 100, 0, null], | ||
[3456, false, 100, 0, 3456], | ||
]; | ||
} | ||
} |