|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Ion |
|
4
|
|
|
* |
|
5
|
|
|
* Building blocks for web development |
|
6
|
|
|
* |
|
7
|
|
|
* @package Ion |
|
8
|
|
|
* @author Timothy J. Warren |
|
9
|
|
|
* @copyright Copyright (c) 2015 - 2016 |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Aviat\Ion; |
|
14
|
|
|
|
|
15
|
|
|
use Aviat\Ion\Type\StringType; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Helper class for json convenience methods |
|
19
|
|
|
*/ |
|
20
|
|
|
class Json { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Encode data in json format |
|
24
|
|
|
* |
|
25
|
|
|
* @param mixed $data |
|
26
|
|
|
* @param int $options=0 |
|
|
|
|
|
|
27
|
|
|
* @param int $depth=512 |
|
|
|
|
|
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function encode($data, $options = 0, $depth = 512) |
|
31
|
|
|
{ |
|
32
|
|
|
$json = json_encode($data, $options, $depth); |
|
33
|
|
|
self::check_json_error(); |
|
34
|
|
|
return $json; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Encode data in json format and save to a file |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $filename |
|
41
|
|
|
* @param mixed $data |
|
42
|
|
|
* @param int $json_options - Options to pass to json_encode |
|
43
|
|
|
* @param int $file_options - Options to pass to file_get_contents |
|
44
|
|
|
* @return int |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function encodeFile($filename, $data, $json_options = 0, $file_options = 0) |
|
47
|
|
|
{ |
|
48
|
|
|
$json = self::encode($data, $json_options); |
|
49
|
|
|
return file_put_contents($filename, $json, $file_options); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Decode data from json |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $json |
|
56
|
|
|
* @param bool $assoc |
|
57
|
|
|
* @param int $depth |
|
58
|
|
|
* @param int $options |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function decode($json, $assoc = TRUE, $depth = 512, $options = 0) |
|
62
|
|
|
{ |
|
63
|
|
|
$data = json_decode($json, $assoc, $depth, $options); |
|
64
|
|
|
self::check_json_error(); |
|
65
|
|
|
return $data; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Decode json data loaded from the passed filename |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $filename |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function decodeFile($filename) |
|
75
|
|
|
{ |
|
76
|
|
|
$json = file_get_contents($filename); |
|
77
|
|
|
return self::decode($json); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Determines whether a string is valid json |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $string |
|
84
|
|
|
* @return boolean |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function isJson($string) |
|
87
|
|
|
{ |
|
88
|
|
|
return StringType::create($string)->isJson(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Call the json error functions to check for errors encoding/decoding |
|
93
|
|
|
* |
|
94
|
|
|
* @throws JsonException |
|
95
|
|
|
*/ |
|
96
|
|
|
protected static function check_json_error() |
|
97
|
|
|
{ |
|
98
|
|
|
$constant_map = [ |
|
99
|
|
|
JSON_ERROR_NONE => 'JSON_ERROR_NONE', |
|
100
|
|
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH', |
|
101
|
|
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH', |
|
102
|
|
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR', |
|
103
|
|
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX', |
|
104
|
|
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8', |
|
105
|
|
|
JSON_ERROR_RECURSION => 'JSON_ERROR_RECURSION', |
|
106
|
|
|
JSON_ERROR_INF_OR_NAN => 'JSON_ERROR_INF_OR_NAN', |
|
107
|
|
|
JSON_ERROR_UNSUPPORTED_TYPE => 'JSON_ERROR_UNSUPPORTED_TYPE' |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
$error = json_last_error(); |
|
111
|
|
|
$message = json_last_error_msg(); |
|
112
|
|
|
|
|
113
|
|
|
if (\JSON_ERROR_NONE !== $error) |
|
114
|
|
|
{ |
|
115
|
|
|
throw new JsonException("{$constant_map[$error]} - {$message}", $error); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
// End of JSON.php |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.