1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Particle. |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/particle-php for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com) |
7
|
|
|
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License |
8
|
|
|
*/ |
9
|
|
|
namespace Particle\Filter\FilterRule; |
10
|
|
|
|
11
|
|
|
use Particle\Filter\FilterRule; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DecodeJSON |
15
|
|
|
* |
16
|
|
|
* 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 |
17
|
|
|
* a correct JSON or if an encoded data is deeper than the recursion limit, `null` is returned. |
18
|
|
|
* |
19
|
|
|
* @package Particle\Filter\FilterRule |
20
|
|
|
*/ |
21
|
|
|
class DecodeJSON extends FilterRule |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var bool When `true`, decoded objects will be converted into associative arrays |
25
|
|
|
*/ |
26
|
|
|
protected $assoc; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int Decode recursion depth |
30
|
|
|
*/ |
31
|
|
|
protected $depth; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int Bitmask of JSON decode options |
35
|
|
|
*/ |
36
|
|
|
protected $options; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set required params for JSON decoding |
40
|
|
|
* |
41
|
|
|
* @param bool $assoc When `true`, decoded objects will be converted into associative arrays |
42
|
|
|
* @param int $depth Decode recursion dept |
43
|
|
|
* @param int $options Bitmask of JSON decode options |
44
|
|
|
* @see http://php.net/manual/en/function.json-decode.php More information about the parameters |
45
|
|
|
*/ |
46
|
11 |
|
public function __construct($assoc, $depth, $options) |
47
|
|
|
{ |
48
|
11 |
|
$this->assoc = $assoc; |
49
|
11 |
|
$this->depth = $depth; |
50
|
11 |
|
$this->options = $options; |
51
|
11 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Decodes the value JSON |
55
|
|
|
* |
56
|
|
|
* @param mixed $value |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
11 |
|
public function filter($value) |
60
|
|
|
{ |
61
|
11 |
|
if (!is_string($value)) { |
62
|
2 |
|
return $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
return json_decode($value, $this->assoc, $this->depth, $this->options); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|