Skip to content

Commit edfd65c

Browse files
authored
Merge pull request #10 from adhocore/develop
Develop
2 parents 2af3e68 + db48ab3 commit edfd65c

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ $someJsonText = file_get_contents('...');
4343

4444
// You can pass args like in `json_decode`
4545
(new Comment)->decode($someJsonText, $assoc = true, $depth = 512, $options = JSON_BIGINT_AS_STRING);
46+
47+
// Or you can use static alias of decode:
48+
Comment::parse($json, true);
4649
```

src/Comment.php

+22
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,17 @@ public function strip($json)
4848
$index += $charnext === '*/' ? 1 : 0;
4949
}
5050

51+
$this->reset();
52+
5153
return $return;
5254
}
5355

56+
protected function reset()
57+
{
58+
$this->comment = 0;
59+
$this->inStr = false;
60+
}
61+
5462
protected function inStringOrCommentEnd($prev, $char, $charnext)
5563
{
5664
if (0 === $this->comment && $char === '"' && $prev !== '\\') {
@@ -108,4 +116,18 @@ public function decode($json, $assoc = false, $depth = 512, $options = 0)
108116

109117
return $decoded;
110118
}
119+
120+
/**
121+
* Static alias of decode().
122+
*/
123+
public static function parse($json, $assoc = false, $depth = 512, $options = 0)
124+
{
125+
static $parser;
126+
127+
if (!$parser) {
128+
$parser = new static;
129+
}
130+
131+
return $parser->decode($json, $assoc, $depth, $options);
132+
}
111133
}

tests/CommentTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public function testDecodeThrows()
3636
(new Comment)->decode('{"a":1, /* comment */, "b":}', true);
3737
}
3838

39+
public function testParse()
40+
{
41+
$parsed = Comment::parse('{
42+
// comment
43+
"a//b":"/*value*/"
44+
/* also comment */
45+
}', true);
46+
47+
$this->assertNotEmpty($parsed);
48+
$this->assertInternalType('array', $parsed);
49+
$this->assertArrayHasKey('a//b', $parsed);
50+
$this->assertSame('/*value*/', $parsed['a//b']);
51+
}
52+
3953
public function theTests()
4054
{
4155
return [

0 commit comments

Comments
 (0)