1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Bernardo Secades |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in all |
16
|
|
|
* copies or substantial portions of the Software. |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace BernardoSecades\Json; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author bernardosecades <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Json |
32
|
|
|
{ |
33
|
|
|
const ENCODE_OPERATION = 0; |
34
|
|
|
const DECODE_OPERATION = 1; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed $value |
38
|
|
|
* @param ArrayOption|null $options |
39
|
|
|
* @param int $depth |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
6 |
|
public static function encode($value, ArrayOption $options = null, $depth = 512) |
43
|
|
|
{ |
44
|
6 |
|
if (null === $options) { |
45
|
5 |
|
$options = new ArrayOption(); |
46
|
5 |
|
$options[] = Option::JSON_NONE(); |
47
|
5 |
|
} |
48
|
|
|
|
49
|
6 |
|
$data = json_encode($value, $options->getValueOptions(), $depth); |
50
|
|
|
|
51
|
6 |
|
self::handleError(self::ENCODE_OPERATION); |
52
|
|
|
|
53
|
5 |
|
return $data; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @param bool|false $assoc |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
10 |
|
public static function decode($value, $assoc = false) |
62
|
|
|
{ |
63
|
10 |
|
$data = json_decode($value, $assoc); |
64
|
|
|
|
65
|
10 |
|
self::handleError(self::DECODE_OPERATION); |
66
|
|
|
|
67
|
8 |
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param mixed $value |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
5 |
|
public static function isValid($value) |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
5 |
|
self::decode($value); |
78
|
4 |
|
return true; |
79
|
1 |
|
} catch (DecodeException $exception) { |
80
|
1 |
|
return false; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $operation |
86
|
|
|
* |
87
|
|
|
* @throws EncodeException |
88
|
|
|
* @throws DecodeException |
89
|
|
|
*/ |
90
|
16 |
|
protected static function handleError($operation) |
91
|
|
|
{ |
92
|
16 |
|
$errorCode = json_last_error(); |
93
|
|
|
|
94
|
16 |
|
if (Error::JSON_ERROR_NONE !== $errorCode) { |
95
|
3 |
|
$errorMsg = json_last_error_msg(); |
96
|
|
|
switch ($operation) { |
97
|
3 |
|
case self::ENCODE_OPERATION: |
98
|
1 |
|
throw new EncodeException($errorMsg, $errorCode); |
99
|
2 |
|
case self::DECODE_OPERATION: |
100
|
2 |
|
throw new DecodeException($errorMsg, $errorCode); |
101
|
|
|
// @codeCoverageIgnoreStart |
102
|
|
|
default: |
103
|
|
|
throw new \LogicException(sprintf('Operation "%d" is not allowed', $operation)); |
104
|
|
|
} // @codeCoverageIgnoreEnd |
105
|
|
|
} |
106
|
13 |
|
} |
107
|
|
|
} |
108
|
|
|
|