Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"license": "MIT",
"type": "library",
"require": {
"php": ">=5.6.0"
"php": ">=7.1"
},
"require-dev": {
"phpdocumentor/phpdocumentor": "2.*",
Expand All @@ -29,7 +29,7 @@
},
"autoload": {
"psr-4": {
"t1st3\\": "src/t1st3/"
"t1st3\\JSONMin\\": "src/"
}
}
}
135 changes: 75 additions & 60 deletions src/t1st3/JSONMin/JSONMin.php → src/JSONMin.php
Original file line number Diff line number Diff line change
@@ -1,90 +1,106 @@
<?php
/**
* php-json-minify
* @package JSONMin
* @version 0.6.2
* @link https://github.com/t1st3/php-json-minify
* @author t1st3 <https://github.com/t1st3>
* @license https://github.com/t1st3/php-json-minify/blob/master/LICENSE.md MIT
* @copyright Copyright (c) 2014, t1st3
*
*
* Based on JSON.minify (https://github.com/getify/JSON.minify) by Kyle Simspon (https://github.com/getify)
* JSON.minify is released under MIT license.
*
*/
* php-json-minify
* @package JSONMin
* @version 0.6.2
* @link https://github.com/t1st3/php-json-minify
* @author t1st3 <https://github.com/t1st3>
* @license https://github.com/t1st3/php-json-minify/blob/master/LICENSE.md MIT
* @copyright Copyright (c) 2014, t1st3
*
*
* Based on JSON.minify (https://github.com/getify/JSON.minify) by Kyle Simspon (https://github.com/getify)
* JSON.minify is released under MIT license.
*
*/

namespace t1st3\JSONMin;

/**
* The JSONMin class
* @author t1st3 <https://github.com/t1st3>
* @since 0.1.0
*/
class JSONMin {
use function implode;
use function preg_match;
use function preg_replace;
use function strlen;
use function substr;

/**
* The JSONMin class
* @author t1st3 <https://github.com/t1st3>
* @since 0.1.0
*/
final class JSONMin
{
/**
* The original JSON string
* @var string $original_json The original JSON string
* @since 0.1.0
*/
protected $original_json = '';
* The original JSON string
* @var string $original_json The original JSON string
* @since 0.1.0
*/
private $original_json = '';

/**
* The minified JSON string
* @var string $minified_json The minified JSON string
* @since 0.1.0
*/
protected $minified_json = '';
* The minified JSON string
* @var string $minified_json The minified JSON string
* @since 0.1.0
*/
private $minified_json = '';

/**
* Constructor
* @name __construct
* @param string $json Some JSON to minify
* @since 0.1.0
* @return object the JSONMin object
*/
public function __construct ( $json ) {
* Constructor
* @name __construct
* @param string $json Some JSON to minify
* @since 0.1.0
* @return object the JSONMin object
*/
public function __construct(string $json)
{
$this->original_json = $json;
return $this;
}

/**
* Get the minified JSON
* @name getMin
* @since 0.1.0
* @return string Minified JSON string
*/
public function getMin ( ) {
* Get the minified JSON
* @name getMin
* @since 0.1.0
* @return string Minified JSON string
*/
public function getMin(): string
{
$this->minified_json = $this::minify($this->original_json);
return $this->minified_json;
}

/**
* Print the minified JSON
* @name printMin
* @since 0.1.0
* @return object the JSONMin object
*/
public function printMin ( ) {
* Print the minified JSON
* @name printMin
* @since 0.1.0
* @return object the JSONMin object
*/
public function printMin(): self
{
echo $this->getMin();
return $this;
}

/**
* Static minify function
* @name minify
* @param string $json Some JSON to minify
* @since 0.1.0
* @return string Minified JSON string
* @static
*/
public static function minify ($json) {
* Static minify function
* @name minify
* @param string $json Some JSON to minify
* @since 0.1.0
* @return string Minified JSON string
* @static
*/
public static function minify(string $json): string
{
$tokenizer = "/\"|(\/\*)|(\*\/)|(\/\/)|\n|\r/";
$in_string = false;
$in_multiline_comment = false;
$in_singleline_comment = false;
$tmp; $tmp2; $new_str = array(); $ns = 0; $from = 0; $lc; $rc; $lastIndex = 0;
$tmp = null;
$tmp2 = null;
$new_str = array();
$from = 0;
$lc = null;
$rc = null;
$lastIndex = 0;
while (preg_match($tokenizer,$json,$tmp,PREG_OFFSET_CAPTURE,$lastIndex)) {
$tmp = $tmp[0];
$lastIndex = $tmp[1] + strlen($tmp[0]);
Expand Down Expand Up @@ -123,10 +139,9 @@ public static function minify ($json) {
}
}
if (!isset($rc)) {
$rc = $json;
$rc = $json;
}
$new_str[] = $rc;
$new_str[] = preg_replace("/(\n|\r|\s)*/","",$rc);
return implode("",$new_str);
}
}
?>
67 changes: 53 additions & 14 deletions tests/JSONMinTest.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,70 @@
<?php
require_once(dirname(dirname(__FILE__)) . '/src/t1st3/JSONMin/JSONMin.php');

use PHPUnit\Framework\TestCase;
use t1st3\JSONMin\JSONMin as jsonMin;

class JSONMinTest extends TestCase
final class JSONMinTest extends TestCase
{
public function provideJson()
public function provideJson(): iterable
{
yield [
'{"a": "b"}',
'{"a":"b"}',
];

yield [
'[0, 1, 2, 3, 4, 5]',
'[0, 1, 2, 3, 4, 5]',
];
yield 'object' => [
'{"a": "b"}',
'{"a":"b"}',
];

yield 'array' => [
'[0, 1, 2, 3, 4, 5]',
'[0,1,2,3,4,5]',
];

yield 'single line comment' => [
'{
"a": "b",
// Single Line Comment
"c": "d"
}',
'{"a":"b","c":"d"}',
];

yield 'multi line comment' => [
'{
"a": "b",
/**
* Multi Line Comment
**/
"c": "d"
}',
'{"a":"b","c":"d"}',
];
}

/**
* @test
* @dataProvider provideJson
*/
public function testMinifies ($json, $expectedResult) {
public function minifies(string $json, string $expectedResult): void
{
$a = jsonMin::minify($json);
$this->assertEquals($expectedResult, $a);
}

/**
* @test
* @dataProvider provideJson
*/
public function getMinifies(string $json, string $expectedResult): void
{
$a = (new jsonMin($json))->getMin();
$this->assertEquals($expectedResult, $a);
}

/**
* @test
* @dataProvider provideJson
*/
public function echoMinifies(string $json, string $expectedResult): void
{
self::expectOutputString($expectedResult);
(new jsonMin($json))->printMin();
}
}
?>