Skip to content

Commit cd450b3

Browse files
committed
Add lint method and custom exception
1 parent 58880ad commit cd450b3

File tree

8 files changed

+64
-15
lines changed

8 files changed

+64
-15
lines changed

bin/jsonlint

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<?php
33

44
/*
5-
* This file is part of the JsonLint package.
5+
* This file is part of the JSON Lint package.
66
*
77
* (c) Jordi Boggiano <[email protected]>
88
*
@@ -30,5 +30,8 @@ if (!is_readable($file)) {
3030
}
3131

3232
$parser = new JsonParser();
33-
$parser->parse(file_get_contents($file));
33+
if ($err = $parser->lint(file_get_contents($file))) {
34+
echo $err->getMessage().PHP_EOL;
35+
exit(1);
36+
}
3437
echo 'Valid JSON'.PHP_EOL;

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
bootstrap="tests/bootstrap.php"
1313
>
1414
<testsuites>
15-
<testsuite name="JsonLint Test Suite">
15+
<testsuite name="JSON Lint Test Suite">
1616
<directory>./tests/</directory>
1717
</testsuite>
1818
</testsuites>

src/Seld/JsonLint/JsonParser.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the JsonLint package.
4+
* This file is part of the JSON Lint package.
55
*
66
* (c) Jordi Boggiano <[email protected]>
77
*
@@ -19,10 +19,10 @@
1919
* Example:
2020
*
2121
* $parser = new JsonParser();
22-
* // returns parsed json, like json_decode does, but slower, throws exceptions on failure.
23-
* $parser->parse($json);
2422
* // returns null if it's valid json, or an error object
2523
* $parser->lint($json);
24+
* // returns parsed json, like json_decode does, but slower, throws exceptions on failure.
25+
* $parser->parse($json);
2626
*
2727
* Ported from https://github.com/zaach/jsonlint
2828
*/
@@ -110,6 +110,24 @@ class JsonParser
110110
16 => array(2, 6)
111111
);
112112

113+
/**
114+
* @param string $input JSON string
115+
* @return null|ParsingException null if no error is found, a ParsingException containing all details otherwise
116+
*/
117+
public function lint($input)
118+
{
119+
try {
120+
$this->parse($input);
121+
} catch (ParsingException $e) {
122+
return $e;
123+
}
124+
}
125+
126+
/**
127+
* @param string $input JSON string
128+
* @return mixed
129+
* @throws ParsingException
130+
*/
113131
public function parse($input)
114132
{
115133
$this->stack = array(0);
@@ -168,7 +186,7 @@ public function parse($input)
168186
}
169187
}
170188

171-
$errStr = 'Parse error on line ' . ($yylineno+1) . ":\n" . $this->lexer->showPosition() . "\nExpecting " . implode(', ', $expected);
189+
$errStr = 'Parse error on line ' . ($yylineno+1) . ":\n" . $this->lexer->showPosition() . "\nExpected one of: " . implode(', ', $expected);
172190
$this->parseError($errStr, array(
173191
'text' => $this->lexer->match,
174192
'token' => !empty($this->terminals_[$symbol]) ? $this->terminals_[$symbol] : $symbol,
@@ -181,7 +199,7 @@ public function parse($input)
181199
// just recovered from another error
182200
if ($recovering == 3) {
183201
if ($symbol == $EOF) {
184-
throw new \Exception($errStr ?: 'Parsing halted.');
202+
throw new ParsingException($errStr ?: 'Parsing halted.');
185203
}
186204

187205
// discard current lookahead and grab another
@@ -199,7 +217,7 @@ public function parse($input)
199217
break;
200218
}
201219
if ($state == 0) {
202-
throw new \Exception($errStr ?: 'Parsing halted.');
220+
throw new ParsingException($errStr ?: 'Parsing halted.');
203221
}
204222
$this->popStack(1);
205223
$state = $this->stack[count($this->stack)-1];
@@ -214,7 +232,7 @@ public function parse($input)
214232

215233
// this shouldn't happen, unless resolve defaults are off
216234
if (is_array($action[0]) && count($action) > 1) {
217-
throw new \Exception('Parse Error: multiple actions possible at state: ' . $state . ', token: ' . $symbol);
235+
throw new ParsingException('Parse Error: multiple actions possible at state: ' . $state . ', token: ' . $symbol);
218236
}
219237

220238
switch ($action[0]) {
@@ -276,7 +294,7 @@ public function parse($input)
276294

277295
protected function parseError($str, $hash)
278296
{
279-
throw new \Exception($str);
297+
throw new ParsingException($str, $hash);
280298
}
281299

282300
// $$ = $tokens // needs to be passed by ref?

src/Seld/JsonLint/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the JsonLint package.
4+
* This file is part of the JSON Lint package.
55
*
66
* (c) Jordi Boggiano <[email protected]>
77
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JSON Lint package.
5+
*
6+
* (c) Jordi Boggiano <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Seld\JsonLint;
13+
14+
class ParsingException extends \Exception
15+
{
16+
protected $details;
17+
18+
public function __construct($message, $details = array())
19+
{
20+
$this->details = $details;
21+
parent::__construct($message);
22+
}
23+
24+
public function getDetails()
25+
{
26+
return $this->details;
27+
}
28+
}

src/Seld/JsonLint/Undefined.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the JsonLint package.
4+
* This file is part of the JSON Lint package.
55
*
66
* (c) Jordi Boggiano <[email protected]>
77
*

tests/JsonParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the JsonLint package.
4+
* This file is part of the JSON Lint package.
55
*
66
* (c) Jordi Boggiano <[email protected]>
77
*

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the JsonLint package.
4+
* This file is part of the JSON Lint package.
55
*
66
* (c) Jordi Boggiano <[email protected]>
77
*

0 commit comments

Comments
 (0)