-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathJSON.php
More file actions
54 lines (46 loc) · 1.68 KB
/
JSON.php
File metadata and controls
54 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/*!
* JSON Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Handles checking for any errors that might have been encountered when loading JSON data
*
*/
namespace PatternLab;
use \PatternLab\Console;
use \PatternLab\Timer;
use \Seld\JsonLint;
class JSON {
protected static $errors = array(
JSON_ERROR_NONE => false,
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);
/**
* Returns the last error message when building a JSON file. Mimics json_last_error_msg() from PHP 5.5
* @param {String} the file that generated the error
*/
public static function hasError() {
$error = json_last_error();
$errorMessage = array_key_exists($error, self::$errors) ? self::$errors[$error] : "Unknown error ({$error})";
return $errorMessage;
}
/**
* Returns the last error message when building a JSON file. Mimics json_last_error_msg() from PHP 5.5
* @param {String} the file that generated the error
*/
public static function lastErrorMsg($file,$message,$data) {
Console::writeLine(PHP_EOL."<error>The JSON file, ".$file.", wasn't loaded. The error: ".$message."</error>");
if ($message == "Syntax error, malformed JSON") {
Console::writeLine("");
$parser = new JsonLint\JsonParser();
$error = $parser->lint($data);
Console::writeError($error->getMessage(), false, true);
}
}
}