Skip to content

Commit 3d642f4

Browse files
committed
feat: everything
1 parent 4fa0d55 commit 3d642f4

9 files changed

+286
-1
lines changed

.editorconfig

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; http://editorconfig.org
2+
;
3+
; Sublime: https://github.com/sindresorhus/editorconfig-sublime
4+
; Phpstorm: https://plugins.jetbrains.com/plugin/7294-editorconfig
5+
6+
root = true
7+
8+
[*]
9+
indent_style = space
10+
indent_size = 2
11+
end_of_line = lf
12+
charset = utf-8
13+
trim_trailing_whitespace = true
14+
insert_final_newline = true
15+
16+
[{*.md,*.php,composer.json,composer.lock}]
17+
indent_size = 4

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# standards
2+
/.cache/
3+
/.env
4+
/.idea/
5+
/vendor/
6+
composer.lock

.travis.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: php
2+
3+
php:
4+
# The adhocore/json-comment should run fine with php 5.4+, but phpunit 5.7 requires php 5.6+
5+
# - 5.4
6+
# - 5.5
7+
- 5.6
8+
- 7.0
9+
- 7.1
10+
11+
install:
12+
- composer install --prefer-dist
13+
14+
script:
15+
- vendor/bin/phpunit --coverage-text

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Jitendra Adhikari
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "adhocore/json-comment",
3+
"description": "Lightweight JSON comment stripper library for PHP",
4+
"type": "library",
5+
"keywords": [
6+
"json", "comment", "strip-comment"
7+
],
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "Jitendra Adhikari",
12+
"email": "[email protected]"
13+
}
14+
],
15+
"autoload": {
16+
"psr-4": {
17+
"Ahc\\Json\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Ahc\\Json\\Test\\": "tests/"
23+
}
24+
},
25+
"require": {
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit": "^5.7.0"
29+
}
30+
}

phpunit.xml.dist

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
<testsuites>
15+
<testsuite name="JSON Comment Suite">
16+
<directory>./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist processUncoveredFilesFromWhitelist="true">
21+
<directory suffix=".php">./src</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use Ahc\Json\Comment;
1414

1515
// The JSON string!
1616
$someJsonText = '{...}';
17-
$someJsonText = file_get_contents('...'');
17+
$someJsonText = file_get_contents('...');
1818

1919
// Strip only!
2020
(new Comment)->strip($someJsonText);

src/Comment.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Ahc\Json;
4+
5+
/**
6+
* JSON comment stripper.
7+
*
8+
* @author Jitendra Adhikari <[email protected]>
9+
*/
10+
class Comment
11+
{
12+
/**
13+
* Strip comments from JSON string.
14+
*
15+
* @param string $json
16+
*
17+
* @return string The comment stripped JSON.
18+
*/
19+
public function strip($json)
20+
{
21+
if (!preg_match('%\/(\/|\*)%', $json)) {
22+
return $json;
23+
}
24+
25+
$index = 0;
26+
$comment = $inStr = false;
27+
$return = $char = '';
28+
29+
while (isset($json[$index])) {
30+
$prev = $char;
31+
$char = $json[$index];
32+
33+
if ($char === '"' && $prev !== '\\') {
34+
$inStr = !$inStr;
35+
}
36+
37+
if (!$inStr) {
38+
$next = isset($json[$index + 1]) ? $json[$index + 1] : '';
39+
40+
if (!$comment && $char . $next === '//') {
41+
$comment = 'single';
42+
} elseif (!$comment && $char . $next === '/*') {
43+
$comment = 'multi';
44+
}
45+
46+
if ($comment) {
47+
if (($comment === 'single' && $char == "\n") ||
48+
($comment === 'multi' && $char . $next == "*/")
49+
) {
50+
// Cosmetic fix only!
51+
if ($comment === 'single') {
52+
$return = rtrim($return) . $char;
53+
}
54+
$comment = false;
55+
}
56+
57+
$index += $char . $next === '*/' ? 2 : 1;
58+
59+
continue;
60+
}
61+
}
62+
63+
$return .= $char;
64+
65+
++$index;
66+
}
67+
68+
return $return;
69+
}
70+
71+
/**
72+
* Strip comments and decode JSON string.
73+
*
74+
* @param string $json
75+
* @param bool|boolean $assoc
76+
* @param int|integer $depth
77+
* @param int|integer $options
78+
*
79+
* @see http://php.net/json_decode [JSON decode native function]
80+
*
81+
* @return mixed
82+
*/
83+
public function decode($json, $assoc = false, $depth = 512, $options = 0)
84+
{
85+
return json_decode($this->strip($json), $assoc, $depth, $options);
86+
}
87+
}

tests/CommentTest.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Ahc\Json\Test;
4+
5+
use Ahc\Json\Comment;
6+
7+
/** @coversDefaultClass \Ahc\Json\Comment */
8+
class CommentTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @dataProvider theTests
12+
* @covers \Ahc\Json\Comment::strip
13+
*/
14+
public function testStrip($json, $expect)
15+
{
16+
$this->assertSame($expect, (new Comment)->strip($json));
17+
}
18+
19+
/**
20+
* @dataProvider theTests
21+
* @covers \Ahc\Json\Comment::decode
22+
*/
23+
public function testDecode($json)
24+
{
25+
$actual = (new Comment)->decode($json, true);
26+
27+
$this->assertNotEmpty($actual);
28+
$this->assertArrayHasKey('a', $actual);
29+
$this->assertArrayHasKey('b', $actual);
30+
31+
$this->assertSame(JSON_ERROR_NONE, json_last_error());
32+
}
33+
34+
public function theTests()
35+
{
36+
return [
37+
'without comment' => [
38+
'json' => '{"a":1,"b":2}',
39+
'expect' => '{"a":1,"b":2}',
40+
],
41+
'single line comment' => [
42+
'json' => '{"a":1,
43+
// comment
44+
"b":2,
45+
// comment
46+
"c":3}',
47+
'expect' => '{"a":1,
48+
"b":2,
49+
"c":3}',
50+
],
51+
'single line comment at end' => [
52+
'json' => '{"a":1,
53+
"b":2,// comment
54+
"c":3}',
55+
'expect' => '{"a":1,
56+
"b":2,
57+
"c":3}',
58+
],
59+
'real multiline comment' => [
60+
'json' => '{"a":1,
61+
/*
62+
* comment
63+
*/
64+
"b":2, "c":3}',
65+
'expect' => '{"a":1,
66+
' . '
67+
"b":2, "c":3}',
68+
],
69+
'inline multiline comment' => [
70+
'json' => '{"a":1,
71+
/* comment */ "b":2, "c":3}',
72+
'expect' => '{"a":1,
73+
"b":2, "c":3}',
74+
],
75+
'inline multiline comment at end' => [
76+
'json' => '{"a":1, "b":2, "c":3/* comment */}',
77+
'expect' => '{"a":1, "b":2, "c":3}',
78+
],
79+
'comment inside string' => [
80+
'json' => '{"a": "a//b", "b":"a/* not really comment */b"}',
81+
'expect' => '{"a": "a//b", "b":"a/* not really comment */b"}',
82+
],
83+
];
84+
}
85+
}

0 commit comments

Comments
 (0)