File tree Expand file tree Collapse file tree 3 files changed +45
-61
lines changed Expand file tree Collapse file tree 3 files changed +45
-61
lines changed Original file line number Diff line number Diff line change 13
13
14
14
/**
15
15
* Decodes JSON data
16
- *
17
- * @author Sander Coolen <[email protected] >
16
+ *
17
+ * @author Sander Coolen <[email protected] >
18
18
*/
19
19
class JsonDecode implements DecoderInterface
20
20
{
21
- /**
22
- * @var bool
23
- */
24
- private $ associative = false ;
25
- /**
26
- * @var int
27
- */
28
- private $ recursionDepth = 512 ;
29
- /**
30
- * @var int
31
- */
21
+ private $ associative ;
22
+ private $ recursionDepth ;
32
23
private $ lastError = JSON_ERROR_NONE ;
33
-
34
- public function __construct ($ assoc = false , $ depth = 512 )
24
+
25
+ public function __construct ($ associative = false , $ depth = 512 )
35
26
{
36
- $ this ->associative = $ assoc ;
27
+ $ this ->associative = $ associative ;
37
28
$ this ->recursionDepth = $ depth ;
38
29
}
39
-
30
+
40
31
/**
41
32
* Returns the last decoding error (if any)
42
- *
43
- * @return int
33
+ *
34
+ * @return integer
35
+ *
44
36
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
45
37
*/
46
38
public function getLastError ()
47
39
{
48
40
return $ this ->lastError ;
49
41
}
50
-
42
+
51
43
/**
52
44
* Decodes a JSON string into PHP data
53
- *
45
+ *
54
46
* @param string $data JSON
55
- * @return mixed
47
+ *
48
+ * @return mixed
56
49
*/
57
50
public function decode ($ data , $ format )
58
51
{
59
52
$ decodedData = json_decode ($ data , $ this ->associative , $ this ->recursionDepth );
60
53
$ this ->lastError = json_last_error ();
61
-
54
+
62
55
return $ decodedData ;
63
56
}
64
-
65
- /**
57
+
58
+ /**
66
59
* {@inheritdoc}
67
60
*/
68
61
public function supportsDecoding ($ format )
69
62
{
70
63
return JsonEncoder::FORMAT === $ format ;
71
64
}
72
- }
65
+ }
Original file line number Diff line number Diff line change 18
18
*/
19
19
class JsonEncode implements EncoderInterface
20
20
{
21
- /**
22
- * @var int
23
- */
24
- private $ options = 0 ;
25
- /**
26
- * @var int
27
- */
21
+ private $ options ;
28
22
private $ lastError = JSON_ERROR_NONE ;
29
-
23
+
30
24
public function __construct ($ bitmask = 0 )
31
25
{
32
26
$ this ->options = $ bitmask ;
33
27
}
34
-
28
+
35
29
/**
36
30
* Returns the last encoding error (if any)
37
- *
38
- * @return int
39
- * @see http://php.net/manual/en/function.json-last-error.php json_last_error
31
+ *
32
+ * @return integer
33
+ *
34
+ * @see http://php.net/manual/en/function.json-last-error.php json_last_error
40
35
*/
41
36
public function getLastError ()
42
37
{
43
38
return $ this ->lastError ;
44
39
}
45
-
40
+
46
41
/**
47
42
* Encodes PHP data to a JSON string
48
- *
43
+ *
49
44
* @param mixed $data
50
- * @return string
45
+ *
46
+ * @return string
51
47
*/
52
48
public function encode ($ data , $ format )
53
49
{
54
50
$ encodedJson = json_encode ($ data , $ this ->options );
55
51
$ this ->lastError = json_last_error ();
56
-
52
+
57
53
return $ encodedJson ;
58
54
}
59
-
55
+
60
56
/**
61
57
* {@inheritdoc}
62
58
*/
63
59
public function supportsEncoding ($ format )
64
60
{
65
61
return JsonEncoder::FORMAT === $ format ;
66
62
}
67
- }
63
+ }
Original file line number Diff line number Diff line change 19
19
class JsonEncoder implements EncoderInterface, DecoderInterface
20
20
{
21
21
const FORMAT = 'json ' ;
22
-
22
+
23
23
/**
24
24
* @var JsonEncode
25
25
*/
26
26
protected $ encodingImpl ;
27
+
27
28
/**
28
29
* @var JsonDecode
29
30
*/
30
31
protected $ decodingImpl ;
31
32
32
33
public function __construct (JsonEncode $ encodingImpl = null , JsonDecode $ decodingImpl = null )
33
34
{
34
- if (null === $ encodingImpl ) {
35
- $ encodingImpl = new JsonEncode ;
36
- }
37
- $ this ->encodingImpl = $ encodingImpl ;
38
- if (null === $ decodingImpl ) {
39
- $ decodingImpl = new JsonDecode (true );
40
- }
41
- $ this ->decodingImpl = $ decodingImpl ;
35
+ $ this ->encodingImpl = null === $ encodingImpl ? new JsonEncode () : $ encodingImpl ;
36
+ $ this ->decodingImpl = null === $ decodingImpl ? new JsonDecode (true ) : $ decodingImpl ;
42
37
}
43
-
38
+
44
39
/**
45
40
* Returns the last encoding error (if any)
46
- *
47
- * @return int
41
+ *
42
+ * @return integer
48
43
*/
49
44
public function getLastEncodingError ()
50
45
{
51
46
return $ this ->encodingImpl ->getLastError ();
52
47
}
53
-
48
+
54
49
/**
55
50
* Returns the last decoding error (if any)
56
- *
57
- * @return int
51
+ *
52
+ * @return integer
58
53
*/
59
54
public function getLastDecodingError ()
60
55
{
61
56
return $ this ->decodingImpl ->getLastError ();
62
57
}
63
-
58
+
64
59
/**
65
60
* {@inheritdoc}
66
61
*/
@@ -92,4 +87,4 @@ public function supportsDecoding($format)
92
87
{
93
88
return self ::FORMAT === $ format ;
94
89
}
95
- }
90
+ }
You can’t perform that action at this time.
0 commit comments