Completed
Push — master ( 9c73ab...3bd5c7 )
by Timothy
02:45
created

Json::isJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
0 ignored issues
show
Documentation introduced by
There is no parameter named $options=0. Did you maybe mean $options?

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 $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
27
	 * @param int $depth=512
0 ignored issues
show
Bug introduced by
There is no parameter named $depth=512. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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
	 * @param bool $assoc
73
 	 * @param int $depth
74
 	 * @param int $options
75
	 * @return mixed
76
	 */
77
	public static function decodeFile($filename, $assoc = TRUE, $depth = 512, $options = 0)
78
	{
79
		$json = file_get_contents($filename);
80
		return self::decode($json, $assoc, $depth, $options);
81
	}
82
83
	/**
84
	 * Determines whether a string is valid json
85
	 *
86
	 * @param  string  $string
87
	 * @return boolean
88
	 */
89
	public static function isJson($string)
90
	{
91
		return StringType::create($string)->isJson();
92
	}
93
94
	/**
95
	 * Call the json error functions to check for errors encoding/decoding
96
	 *
97
	 * @throws JsonException
98
	 */
99
	protected static function check_json_error()
100
	{
101
		$constant_map = [
102
			JSON_ERROR_NONE => 'JSON_ERROR_NONE',
103
			JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH',
104
			JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH',
105
			JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR',
106
			JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX',
107
			JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8',
108
			JSON_ERROR_RECURSION => 'JSON_ERROR_RECURSION',
109
			JSON_ERROR_INF_OR_NAN => 'JSON_ERROR_INF_OR_NAN',
110
			JSON_ERROR_UNSUPPORTED_TYPE => 'JSON_ERROR_UNSUPPORTED_TYPE'
111
		];
112
113
		$error = json_last_error();
114
		$message = json_last_error_msg();
115
116
		if (\JSON_ERROR_NONE !== $error)
117
		{
118
			throw new JsonException("{$constant_map[$error]} - {$message}", $error);
119
		}
120
	}
121
}
122
// End of JSON.php