From 25d9bb8522af7b1ba272416a824d82f0ab15dbad Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Mon, 16 Mar 2020 23:34:14 +0300 Subject: [PATCH 1/3] Added formatting option. closes #3 --- src/JsonX.php | 226 ++++++++++++++++++++++++++-------- tests/JsonXTest.php | 288 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 463 insertions(+), 51 deletions(-) diff --git a/src/JsonX.php b/src/JsonX.php index e73744d..5b79405 100644 --- a/src/JsonX.php +++ b/src/JsonX.php @@ -37,9 +37,31 @@ * against the created instance. * * @author Ibrahim - * @since 1.2.1 + * @since 1.2.2 */ class JsonX { + /** + * New line character. + */ + private $NL = "\n"; + /** + * A number that represents the number of spaces in a tab. + * @var int + * @since 1.2.2 + */ + private $tabSize; + /** + * The number of tabs that have been pressed. + * @var int + * @since 1.2.2 + */ + private $currentTab; + /** + * + * @var string + * @since 1.2.2 + */ + private $tabStr; /** * An array that contains JSON special characters. * The array contains the following characters: @@ -94,24 +116,87 @@ class JsonX { * @since 1.0 */ private $attributes = []; + /** + * Creates new instance of the class. + * @param array|string $initialData Initial data which is used to initialize + * the object. It can be a string which looks like JSON or it can be an + * associative array. If it is an associative array, then the keys will be + * acting as properties and the value of each key will be the value of + * the property. + * @param boolean $isFormatted If this attribute is set to true, the generated + * JSON will be indented and have new lines (readable). + * @since 1.2.2 + */ + public function __construct($initialData=[],$isFormatted=false) { + $this->currentTab = 0; + if($isFormatted === true){ + $this->tabSize = 4; + $this->NL = "\n"; + } + else{ + $this->tabSize = 0; + $this->NL = ''; + } + $this->_initData($initialData); + } + /** + * + * @return string + * @since 1.2.2 + */ + private function _getTab() { + $tabLen = $this->tabSize*$this->currentTab; + if(strlen($this->tabStr) != $tabLen){ + $this->tabStr = ''; + for($x = 0 ; $x < $tabLen ; $x++){ + $this->tabStr .= ' '; + } + } + return $this->tabStr; + } + /** + * @since 1.2.2 + */ + private function _addTab() { + $this->currentTab++; + } + private function _reduceTab() { + if($this->currentTab > 0){ + $this->currentTab--; + } + } + /** + * + * @param array $data + * @since 1.2.2 + */ + private function _initData($data) { + if(gettype($data) == 'array'){ + foreach ($data as $key => $value){ + $this->add($key, $value); + } + } + } /** * Returns the data on the object as a JSON string. * @return string */ public function __toString() { - $retVal = '{'; + $retVal = $this->_getTab().'{'.$this->NL; + $this->_addTab(); $count = count($this->attributes); $index = 0; foreach ($this->attributes as $key => $val) { if ($index + 1 == $count) { - $retVal .= '"'.$key.'":'.$val; + $retVal .= $this->_getTab().'"'.$key.'":'.trim($val).$this->NL; } else { - $retVal .= '"'.$key.'":'.$val.', '; + $retVal .= $this->_getTab().'"'.$key.'":'.trim($val).', '.$this->NL; } $index++; } - $retVal .= '}'; + $this->_reduceTab(); + $retVal .= $this->_getTab().'}'; return $retVal; } @@ -188,11 +273,12 @@ public function add($key, $value, $options = [ * @param string $key The name of the key. * @param array $value The array that will be added. * @param boolean $asObject If this parameter is set to true, - * the array will be added as an object in JSON string. Default is true. + * the array will be added as an object in JSON string. Note that if the + * array is associative, each index will be added as an object. Default is false. * @return boolean The method will return false if the given key is invalid * or the given value is not an array. */ - public function addArray($key, $value,$asObject = true) { + public function addArray($key, $value,$asObject = false) { $keyValidated = JsonX::_isValidKey($key); if ($keyValidated !== false) { @@ -285,16 +371,25 @@ public function addObject($key, $val) { if ($keyValidated !== false && gettype($val) == 'object') { if (is_subclass_of($val, 'jsonx\JsonI')) { - $this->attributes[$keyValidated] = ''.$val->toJSON(); + $jsonXObj = $val->toJSON(); + $jsonXObj->currentTab = $this->currentTab + 1; + $jsonXObj->tabSize = $this->tabSize; + $jsonXObj->NL = $this->NL; + $this->attributes[$keyValidated] = ''.$jsonXObj; return true; } else { if ($val instanceof JsonX) { + $val->currentTab = $this->currentTab + 1; + $val->tabSize = $this->tabSize; + $val->NL = $this->NL; $this->attributes[$keyValidated] = $val; } else { $methods = get_class_methods($val); $count = count($methods); $json = new JsonX(); + $json->currentTab = $this->currentTab + 1; + $json->tabSize = $this->tabSize; $propNum = 0; set_error_handler(function() { @@ -446,21 +541,25 @@ public function toJSONString() { * @return string A JSON string that represents the array. * @since 1.0 */ - private function _arrayToJSONString($value,$asObject = false) { + private function _arrayToJSONString($value,$asObject = false,$isSubArray=false) { $keys = array_keys($value); $keysCount = count($keys); - if ($asObject === true) { - $arr = '{'; + $arr = '{'.$this->NL; } else { - $arr = '['; + $arr = '['.$this->NL; + } + if(!$isSubArray){ + $this->_addTab(); + } + if($keysCount > 0){ + $this->_addTab(); } - for ($x = 0 ; $x < $keysCount ; $x++) { if ($x + 1 == $keysCount) { - $comma = ''; + $comma = ''.$this->NL; } else { - $comma = ', '; + $comma = ', '.$this->NL; } $valueAtKey = $value[$keys[$x]]; $keyType = gettype($keys[$x]); @@ -471,39 +570,46 @@ private function _arrayToJSONString($value,$asObject = false) { //echo '$keyType = '.$keyType.'
'; //echo '$valueType = '.$valueType.'

'; if ($valueAtKey instanceof JsonI) { + $jsonXObj = $valueAtKey->toJSON(); + $jsonXObj->tabSize = $this->tabSize; + $jsonXObj->currentTab = $this->currentTab; + $jsonXObj->NL = $this->NL; if ($asObject === true) { - $arr .= '"'.$keys[$x].'":'.$valueAtKey->toJSON().$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":'.trim($jsonXObj).$comma; } else { - $arr .= $valueAtKey->toJSON().$comma; + $arr .= $this->_getTab().trim($jsonXObj).$comma; } } else { if ($valueAtKey instanceof JsonX) { + $valueAtKey->tabSize = $this->tabSize; + $valueAtKey->currentTab = $this->currentTab; + $valueAtKey->NL = $this->NL; if ($asObject === true) { - $arr .= '"'.$keys[$x].'":'.$valueAtKey.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":'.trim($valueAtKey).$comma; } else { - $arr .= $valueAtKey.$comma; + $arr .= $this->_getTab().trim($valueAtKey).$comma; } } else { if ($keyType == 'integer') { if ($valueType == 'integer' || $valueType == 'double') { if ($asObject === true) { if (is_nan($valueAtKey)) { - $arr .= '"'.$keys[$x].'":"NAN"'.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":"NAN"'.$comma; } else { if ($valueAtKey == INF) { - $arr .= '"'.$keys[$x].'":"INF"'.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":"INF"'.$comma; } else { - $arr .= '"'.$keys[$x].'":'.$valueAtKey.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":'.$valueAtKey.$comma; } } } else { if (is_nan($valueAtKey)) { - $arr .= '"NAN"'.$comma; + $arr .= $this->_getTab().'"NAN"'.$comma; } else { if ($valueAtKey == INF) { - $arr .= '"INF"'.$comma; + $arr .= $this->_getTab().'"INF"'.$comma; } else { - $arr .= $valueAtKey.$comma; + $arr .= $this->_getTab().$valueAtKey.$comma; } } } @@ -514,9 +620,9 @@ private function _arrayToJSONString($value,$asObject = false) { if ($asBool === true || $asBool === false) { $toAdd = $asBool === true ? 'true'.$comma : 'false'.$comma; - $arr .= '"'.$keys[$x].'":'.$toAdd; + $arr .= $this->_getTab().'"'.$keys[$x].'":'.$toAdd; } else { - $arr .= '"'.$keys[$x].'":"'.JsonX::escapeJSONSpecialChars($valueAtKey).'"'.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":"'.JsonX::escapeJSONSpecialChars($valueAtKey).'"'.$comma; } } else { $asBool = $this->_stringAsBoolean($valueAtKey); @@ -525,47 +631,53 @@ private function _arrayToJSONString($value,$asObject = false) { $toAdd = $asBool === true ? 'true'.$comma : 'false'.$comma; $arr .= $toAdd; } else { - $arr .= '"'.JsonX::escapeJSONSpecialChars($valueAtKey).'"'.$comma; + $arr .= $this->_getTab().'"'.JsonX::escapeJSONSpecialChars($valueAtKey).'"'.$comma; } } } else { if ($valueType == 'boolean') { if ($asObject === true) { if ($valueAtKey == true) { - $arr .= '"'.$keys[$x].'":true'.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":true'.$comma; } else { - $arr .= '"'.$keys[$x].'":false'.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":false'.$comma; } } else { if ($valueAtKey == true) { - $arr .= 'true'.$comma; + $arr .= $this->_getTab().'true'.$comma; } else { - $arr .= 'false'.$comma; + $arr .= $this->_getTab().'false'.$comma; } } } else { if ($valueType == 'array') { if ($asObject === true) { - $arr .= '"'.$keys[$x].'":'.$this->_arrayToJSONString($valueAtKey,$asObject).$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":'.$this->_arrayToJSONString($valueAtKey,$asObject,true).$comma; } else { - $arr .= $this->_arrayToJSONString($valueAtKey,$asObject).$comma; + $arr .= $this->_getTab().$this->_arrayToJSONString($valueAtKey,$asObject, true).$comma; } } else { if ($valueType == 'NULL') { if ($asObject === true) { - $arr .= '"'.$keys[$x].'":'.'null'.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":'.'null'.$comma; } else { - $arr .= 'null'.$comma; + $arr .= $this->_getTab().'null'.$comma; } } else { if ($valueType == 'object') { if ($asObject === true) { if ($valueAtKey instanceof JsonX) { - $arr .= '"'.$keys[$x].'":'.$valueAtKey.$comma; + $valueAtKey->currentTab = $this->currentTab; + $valueAtKey->tabSize = $this->tabSize; + $valueAtKey->NL = $this->NL; + $arr .= $this->_getTab().'"'.$keys[$x].'":'.trim($valueAtKey).$comma; } else { $methods = get_class_methods($valueAtKey); $count = count($methods); $json = new JsonX(); + $json->currentTab = $this->currentTab; + $json->tabSize = $this->tabSize; + $json->NL = $this->NL; $propNum = 0; set_error_handler(function() { @@ -583,15 +695,21 @@ private function _arrayToJSONString($value,$asObject = false) { } } } - $arr .= '"'.$keys[$x].'":'.$json.$comma; + $arr .= $this->_getTab().'"'.$keys[$x].'":'.trim($json).$comma; } } else { if ($valueAtKey instanceof JsonX) { - $arr .= $valueAtKey.$comma; + $valueAtKey->tabSize = $this->tabSize; + $valueAtKey->currentTab = $this->currentTab; + $valueAtKey->NL = $this->NL; + $arr .= $this->_getTab().$valueAtKey.$comma; } else { $methods = get_class_methods($valueAtKey); $count = count($methods); $json = new JsonX(); + $json->currentTab = $this->currentTab; + $json->tabSize = $this->tabSize; + $json->NL = $this->NL; $propNum = 0; set_error_handler(function() { @@ -609,7 +727,7 @@ private function _arrayToJSONString($value,$asObject = false) { } } } - $arr .= $json.$comma; + $arr .= $this->_getTab().trim($json).$comma; } } } @@ -620,7 +738,7 @@ private function _arrayToJSONString($value,$asObject = false) { } } else { if ($asObject === true) { - $arr .= '"'.$keys[$x].'":'; + $arr .= $this->_getTab().'"'.$keys[$x].'":'; $type = gettype($valueAtKey); if ($type == 'string') { @@ -643,16 +761,22 @@ private function _arrayToJSONString($value,$asObject = false) { $arr .= 'null'.$comma; } else { if ($type == 'array') { - $result = $this->_arrayToJSONString($valueAtKey, $asObject); + $result = $this->_arrayToJSONString($valueAtKey, $asObject, true); $arr .= $result.$comma; } else { if ($type == 'object') { if ($valueAtKey instanceof JsonX) { - $arr .= $valueAtKey.$comma; + $valueAtKey->currentTab = $this->currentTab; + $valueAtKey->tabSize = $this->tabSize; + $valueAtKey->NL = $this->NL; + $arr .= trim($valueAtKey).$comma; } else { $methods = get_class_methods($valueAtKey); $count = count($methods); $json = new JsonX(); + $json->currentTab = $this->currentTab; + $json->tabSize = $this->tabSize; + $json->NL = $this->NL; $propNum = 0; set_error_handler(function() { @@ -670,7 +794,7 @@ private function _arrayToJSONString($value,$asObject = false) { } } } - $arr .= $json.$comma; + $arr .= trim($json).$comma; } } else { $arr .= 'null'.$comma; @@ -682,6 +806,8 @@ private function _arrayToJSONString($value,$asObject = false) { } } else { $j = new JsonX(); + $j->currentTab = $this->currentTab; + $j->tabSize = $this->tabSize; $j->add($keys[$x], $valueAtKey); $arr .= $j.$comma; } @@ -689,13 +815,17 @@ private function _arrayToJSONString($value,$asObject = false) { } } } - + if($keysCount > 0){ + $this->_reduceTab(); + } if ($asObject === true) { - $arr .= '}'; + $arr .= $this->_getTab().'}'; } else { - $arr .= ']'; + $arr .= $this->_getTab().']'; + } + if(!$isSubArray){ + $this->_reduceTab(); } - return $arr; } /** diff --git a/tests/JsonXTest.php b/tests/JsonXTest.php index 4e96f81..7245ae7 100644 --- a/tests/JsonXTest.php +++ b/tests/JsonXTest.php @@ -186,7 +186,289 @@ public function testAddArray00() { $j = new JsonX(); $arr = []; $j->addArray('arr', $arr); - $this->assertEquals('{"arr":{}}',$j.''); + $this->assertEquals('{"arr":[]}',$j.''); + } + /** + * @test + */ + public function testFormat00() { + $j = new JsonX([],true); + $this->assertEquals("{\n}",$j.''); + } + /** + * @test + */ + public function testFormat01() { + $j = new JsonX([],true); + $j->addBoolean('hello'); + $this->assertEquals("{\n" + . ' "hello":true'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat02() { + $j = new JsonX([],true); + $j->addNumber('hello',66); + $this->assertEquals("{\n" + . ' "hello":66'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat03() { + $j = new JsonX([],true); + $j->addString('hello','world'); + $j->addString('hello2','another string'); + $this->assertEquals("{\n" + . ' "hello":"world", '."\n" + . ' "hello2":"another string"'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat04() { + $j = new JsonX([],true); + $j->addArray('hello-arr',[]); + $this->assertEquals("{\n" + . ' "hello-arr":['."\n" + . ' ]'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat05() { + $j = new JsonX([],true); + $j->addArray('hello-arr',[1,2,3,4]); + $this->assertEquals("{\n" + . ' "hello-arr":['."\n" + . ' 1, '."\n" + . ' 2, '."\n" + . ' 3, '."\n" + . ' 4'."\n" + . ' ]'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat06() { + $j = new JsonX([],true); + $j->addArray('hello-arr',[[],["hello world"]]); + $this->assertEquals("{\n" + . ' "hello-arr":['."\n" + . ' ['."\n" + . ' ], '."\n" + . ' ['."\n" + . ' "hello world"'."\n" + . ' ]'."\n" + . ' ]'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat07() { + $j = new JsonX([],true); + $j->addArray('hello-arr',[[],["hello world",["another sub","with two elements"]]]); + $this->assertEquals("{\n" + . ' "hello-arr":['."\n" + . ' ['."\n" + . ' ], '."\n" + . ' ['."\n" + . ' "hello world", '."\n" + . ' ['."\n" + . ' "another sub", '."\n" + . ' "with two elements"'."\n" + . ' ]'."\n" + . ' ]'."\n" + . ' ]'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat17() { + $j = new JsonX([],true); + $j->addArray('hello-arr',["my-j"=>new JsonX(),new JsonX(['hello'=>"world"])],true); + $this->assertEquals("{\n" + . ' "hello-arr":{'."\n" + . ' "my-j":{'."\n" + . ' }, '."\n" + . ' "0":{'."\n" + . ' "hello":"world"'."\n" + . ' }'."\n" + . ' }'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat16() { + $j = new JsonX([],true); + $j->addArray('hello-arr',[new JsonX(),new JsonX(['hello'=>"world"])]); + $this->assertEquals("{\n" + . ' "hello-arr":['."\n" + . ' {'."\n" + . ' }, '."\n" + . ' {'."\n" + . ' "hello":"world"'."\n" + . ' }'."\n" + . ' ]'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat08() { + $j = new JsonX([],true); + $j->addArray('hello-arr',[], true); + $this->assertEquals("{\n" + . ' "hello-arr":{'."\n" + . ' }'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat09() { + $j = new JsonX([],true); + $j->addArray('hello-arr',[1, 2, 3, "hello mr ali"], true); + $this->assertEquals("{\n" + . ' "hello-arr":{'."\n" + . ' "0":1, '."\n" + . ' "1":2, '."\n" + . ' "2":3, '."\n" + . ' "3":"hello mr ali"'."\n" + . ' }'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat10() { + $j = new JsonX([],true); + $j->addArray('hello-arr',["is-good"=>"You are good", 2, 3, "hello mr ali",[],["a sub with element","hello"=>'world']], true); + $this->assertEquals("{\n" + . ' "hello-arr":{'."\n" + . ' "is-good":"You are good", '."\n" + . ' "0":2, '."\n" + . ' "1":3, '."\n" + . ' "2":"hello mr ali", '."\n" + . ' "3":{'."\n" + . ' }, '."\n" + . ' "4":{'."\n" + . ' "0":"a sub with element", '."\n" + . ' "hello":"world"'."\n" + . ' }'."\n" + . ' }'."\n" + . "}",$j.''); + } + /** + * @test + */ + public function testFormat11() { + $j = new JsonX([],true); + $obj = new Obj1('Hello', 0, true, null, 'he'); + $j->addObject('object', $obj); + $this->assertEquals('{'."\n" + . ' "object":{'."\n" + . ' "property-00":"Hello", '."\n" + . ' "property-01":0, '."\n" + . ' "property-02":true'."\n" + . ' }'."\n" + . '}',$j.''); + } + /** + * @test + */ + public function testFormat12() { + $j = new JsonX([],true); + $obj = new Obj1('Hello', 0, true, null, 'he'); + $j->addArray('array', [$obj]); + $this->assertEquals('{'."\n" + . ' "array":['."\n" + . ' {'."\n" + . ' "property-00":"Hello", '."\n" + . ' "property-01":0, '."\n" + . ' "property-02":true'."\n" + . ' }'."\n" + . ' ]'."\n" + . '}',$j.''); + } + /** + * @test + */ + public function testFormat13() { + $j = new JsonX([],true); + $obj = new Obj1('Hello', 0, true, null, 'he'); + $j->addArray('array', [$obj], true); + $this->assertEquals('{'."\n" + . ' "array":{'."\n" + . ' "0":{'."\n" + . ' "property-00":"Hello", '."\n" + . ' "property-01":0, '."\n" + . ' "property-02":true'."\n" + . ' }'."\n" + . ' }'."\n" + . '}',$j.''); + } + /** + * @test + */ + public function testFormat14() { + $j = new JsonX([],true); + $obj = new Obj1('Hello', 0, true, null, 'he'); + $j->addArray('array', ["my-obj"=>$obj,"empty-arr"=>[]], true); + $this->assertEquals('{'."\n" + . ' "array":{'."\n" + . ' "my-obj":{'."\n" + . ' "property-00":"Hello", '."\n" + . ' "property-01":0, '."\n" + . ' "property-02":true'."\n" + . ' }, '."\n" + . ' "empty-arr":{'."\n" + . ' }'."\n" + . ' }'."\n" + . '}',$j.''); + } + /** + * @test + */ + public function testFormat15() { + $j = new JsonX([ + "hello"=>"world", + 'object'=>new Obj0('8', 7, '6', '5', 4), + 'null'=>null, + 'nan'=>NAN, + 'inf'=>INF, + 'bool'=>true, + 'number'=>667, + 'jsonx'=> new JsonX(['sub-json-x'=>new JsonX()]) + ], true); + $this->assertEquals('' + . '{'."\n" + . ' "hello":"world", '."\n" + . ' "object":{'."\n" + . ' "prop-0":"8", '."\n" + . ' "prop-1":7, '."\n" + . ' "prop-2":"6", '."\n" + . ' "prop-3":4'."\n" + . ' }, '."\n" + . ' "null":null, '."\n" + . ' "nan":"NAN", '."\n" + . ' "inf":"INF", '."\n" + . ' "bool":true, '."\n" + . ' "number":667, '."\n" + . ' "jsonx":{'."\n" + . ' "sub-json-x":{}'."\n" + . ' }'."\n" + . '}' + . '',$j.''); } /** * @test @@ -195,7 +477,7 @@ public function testAddArray01() { $j = new JsonX(); $arr = [1,"Hello",true,NAN,null,99.8,INF]; $j->addArray('arr', $arr); - $this->assertEquals('{"arr":{"0":1, "1":"Hello", "2":true, "3":"NAN", "4":null, "5":99.8, "6":"INF"}}',$j.''); + $this->assertEquals('{"arr":[1, "Hello", true, "NAN", null, 99.8, "INF"]}',$j.''); } /** * @test @@ -213,7 +495,7 @@ public function testAddArray03() { $j = new JsonX(); $arr = ["number" => 1,"Hello" => "world!","boolean" => true,NAN,null]; $j->addArray('arr', $arr); - $this->assertEquals('{"arr":{"number":1, "Hello":"world!", "boolean":true, "0":"NAN", "1":null}}',$j.''); + $this->assertEquals('{"arr":[{"number":1}, {"Hello":"world!"}, {"boolean":true}, "NAN", null]}',$j.''); } /** * @test From aca8a55dc98aaf87f709bd3cd21856c5f408b0a6 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Mon, 16 Mar 2020 23:37:59 +0300 Subject: [PATCH 2/3] Fixed coding standards --- src/JsonX.php | 151 +++++++------ tests/JsonXTest.php | 514 ++++++++++++++++++++++---------------------- 2 files changed, 339 insertions(+), 326 deletions(-) diff --git a/src/JsonX.php b/src/JsonX.php index 5b79405..1139724 100644 --- a/src/JsonX.php +++ b/src/JsonX.php @@ -40,28 +40,6 @@ * @since 1.2.2 */ class JsonX { - /** - * New line character. - */ - private $NL = "\n"; - /** - * A number that represents the number of spaces in a tab. - * @var int - * @since 1.2.2 - */ - private $tabSize; - /** - * The number of tabs that have been pressed. - * @var int - * @since 1.2.2 - */ - private $currentTab; - /** - * - * @var string - * @since 1.2.2 - */ - private $tabStr; /** * An array that contains JSON special characters. * The array contains the following characters: @@ -116,6 +94,28 @@ class JsonX { * @since 1.0 */ private $attributes = []; + /** + * The number of tabs that have been pressed. + * @var int + * @since 1.2.2 + */ + private $currentTab; + /** + * New line character. + */ + private $NL = "\n"; + /** + * A number that represents the number of spaces in a tab. + * @var int + * @since 1.2.2 + */ + private $tabSize; + /** + * + * @var string + * @since 1.2.2 + */ + private $tabStr; /** * Creates new instance of the class. * @param array|string $initialData Initial data which is used to initialize @@ -127,56 +127,18 @@ class JsonX { * JSON will be indented and have new lines (readable). * @since 1.2.2 */ - public function __construct($initialData=[],$isFormatted=false) { + public function __construct($initialData = [],$isFormatted = false) { $this->currentTab = 0; - if($isFormatted === true){ + + if ($isFormatted === true) { $this->tabSize = 4; $this->NL = "\n"; - } - else{ + } else { $this->tabSize = 0; $this->NL = ''; } $this->_initData($initialData); } - /** - * - * @return string - * @since 1.2.2 - */ - private function _getTab() { - $tabLen = $this->tabSize*$this->currentTab; - if(strlen($this->tabStr) != $tabLen){ - $this->tabStr = ''; - for($x = 0 ; $x < $tabLen ; $x++){ - $this->tabStr .= ' '; - } - } - return $this->tabStr; - } - /** - * @since 1.2.2 - */ - private function _addTab() { - $this->currentTab++; - } - private function _reduceTab() { - if($this->currentTab > 0){ - $this->currentTab--; - } - } - /** - * - * @param array $data - * @since 1.2.2 - */ - private function _initData($data) { - if(gettype($data) == 'array'){ - foreach ($data as $key => $value){ - $this->add($key, $value); - } - } - } /** * Returns the data on the object as a JSON string. * @return string @@ -535,26 +497,36 @@ public function hasKey($key) { public function toJSONString() { return $this.''; } + /** + * @since 1.2.2 + */ + private function _addTab() { + $this->currentTab++; + } /** * A helper method used to parse arrays. * @param array $value * @return string A JSON string that represents the array. * @since 1.0 */ - private function _arrayToJSONString($value,$asObject = false,$isSubArray=false) { + private function _arrayToJSONString($value,$asObject = false,$isSubArray = false) { $keys = array_keys($value); $keysCount = count($keys); + if ($asObject === true) { $arr = '{'.$this->NL; } else { $arr = '['.$this->NL; } - if(!$isSubArray){ + + if (!$isSubArray) { $this->_addTab(); } - if($keysCount > 0){ + + if ($keysCount > 0) { $this->_addTab(); } + for ($x = 0 ; $x < $keysCount ; $x++) { if ($x + 1 == $keysCount) { $comma = ''.$this->NL; @@ -574,6 +546,7 @@ private function _arrayToJSONString($value,$asObject = false,$isSubArray=false) $jsonXObj->tabSize = $this->tabSize; $jsonXObj->currentTab = $this->currentTab; $jsonXObj->NL = $this->NL; + if ($asObject === true) { $arr .= $this->_getTab().'"'.$keys[$x].'":'.trim($jsonXObj).$comma; } else { @@ -584,6 +557,7 @@ private function _arrayToJSONString($value,$asObject = false,$isSubArray=false) $valueAtKey->tabSize = $this->tabSize; $valueAtKey->currentTab = $this->currentTab; $valueAtKey->NL = $this->NL; + if ($asObject === true) { $arr .= $this->_getTab().'"'.$keys[$x].'":'.trim($valueAtKey).$comma; } else { @@ -815,19 +789,53 @@ private function _arrayToJSONString($value,$asObject = false,$isSubArray=false) } } } - if($keysCount > 0){ + + if ($keysCount > 0) { $this->_reduceTab(); } + if ($asObject === true) { $arr .= $this->_getTab().'}'; } else { $arr .= $this->_getTab().']'; } - if(!$isSubArray){ + + if (!$isSubArray) { $this->_reduceTab(); } + return $arr; } + /** + * + * @return string + * @since 1.2.2 + */ + private function _getTab() { + $tabLen = $this->tabSize * $this->currentTab; + + if (strlen($this->tabStr) != $tabLen) { + $this->tabStr = ''; + + for ($x = 0 ; $x < $tabLen ; $x++) { + $this->tabStr .= ' '; + } + } + + return $this->tabStr; + } + /** + * + * @param array $data + * @since 1.2.2 + */ + private function _initData($data) { + if (gettype($data) == 'array') { + foreach ($data as $key => $value) { + $this->add($key, $value); + } + } + } /** * Checks if the key is a valid key string. * The key is invalid if its an empty string. @@ -845,6 +853,11 @@ private static function _isValidKey($key) { return false; } + private function _reduceTab() { + if ($this->currentTab > 0) { + $this->currentTab--; + } + } private function _stringAsBoolean($str) { $lower = strtolower($str); $boolTypes = [ diff --git a/tests/JsonXTest.php b/tests/JsonXTest.php index 7245ae7..072bfc7 100644 --- a/tests/JsonXTest.php +++ b/tests/JsonXTest.php @@ -188,6 +188,119 @@ public function testAddArray00() { $j->addArray('arr', $arr); $this->assertEquals('{"arr":[]}',$j.''); } + /** + * @test + */ + public function testAddArray01() { + $j = new JsonX(); + $arr = [1,"Hello",true,NAN,null,99.8,INF]; + $j->addArray('arr', $arr); + $this->assertEquals('{"arr":[1, "Hello", true, "NAN", null, 99.8, "INF"]}',$j.''); + } + /** + * @test + */ + public function testAddArray02() { + $j = new JsonX(); + $arr = [1,1.5,"Hello",true,NAN,null,INF]; + $j->addArray('arr', $arr,false); + $this->assertEquals('{"arr":[1, 1.5, "Hello", true, "NAN", null, "INF"]}',$j.''); + } + /** + * @test + */ + public function testAddArray03() { + $j = new JsonX(); + $arr = ["number" => 1,"Hello" => "world!","boolean" => true,NAN,null]; + $j->addArray('arr', $arr); + $this->assertEquals('{"arr":[{"number":1}, {"Hello":"world!"}, {"boolean":true}, "NAN", null]}',$j.''); + } + /** + * @test + */ + public function testAddBoolean00() { + $j = new JsonX(); + $j->addBoolean('bool ', true); + $this->assertEquals('{"bool":true}',$j.''); + } + /** + * @test + */ + public function testAddNumber00() { + $j = new JsonX(); + $j->addNumber(' number', 33); + $this->assertEquals('{"number":33}',$j.''); + } + /** + * @test + */ + public function testAddObj00() { + $j = new JsonX(); + $obj = new Obj0('Hello', 0, true, null, 'he'); + $j->addObject('object', $obj); + $this->assertEquals('{"object":{"prop-0":"Hello", "prop-1":0, "prop-2":true, "prop-3":"he"}}',$j.''); + } + + /** + * @test + */ + public function testAddObj01() { + $j = new JsonX(); + $obj = new Obj1('Hello', 0, true, null, 'he'); + $j->addObject('object', $obj); + $this->assertEquals('{"object":{"property-00":"Hello", "property-01":0, "property-02":true}}',$j.''); + } + /** + * @test + */ + public function testAddStringTest00() { + $j = new JsonX(); + $this->assertFalse($j->addString('', 'Hello World!')); + $this->assertFalse($j->addString(' ', 'Hello World!')); + $this->assertFalse($j->addString("\n", 'Hello World!')); + $this->assertEquals('{}',$j.''); + } + /** + * @test + */ + public function testAddStringTest01() { + $j = new JsonX(); + $this->assertTrue($j->addString('hello', 'Hello World!')); + $this->assertEquals('{"hello":"Hello World!"}',$j.''); + } + /** + * @test + */ + public function testAddStringTest02() { + $j = new JsonX(); + $this->assertFalse($j->addString('invalid-boolean', 'falseX',true)); + } + /** + * @test + */ + public function testEscJSonSpecialChars00() { + $str = 'I\'m "Good".'; + $result = JsonX::escapeJSONSpecialChars($str); + $this->assertEquals('I\'m \"Good\".',$result); + } + /** + * @test + */ + public function testEscJSonSpecialChars01() { + $str = 'Path: "C:/Windows/Media/onestop.midi"\n'; + $result = JsonX::escapeJSONSpecialChars($str); + $this->assertEquals('Path: \"C:\/Windows\/Media\/onestop.midi\"\\\\n',$result); + } + /** + * @test + */ + public function testEscJSonSpecialChars02() { + $str = '\tI\'m good. But "YOU" are "Better".\r\n' + .'\\An inline comment is good.'; + $result = JsonX::escapeJSONSpecialChars($str); + $this->assertEquals('\\\\tI\'m good. But \"YOU\" are \"Better\".\\\\r\\\\n' + .'\\\\An inline comment is good.',$result); + } /** * @test */ @@ -202,8 +315,8 @@ public function testFormat01() { $j = new JsonX([],true); $j->addBoolean('hello'); $this->assertEquals("{\n" - . ' "hello":true'."\n" - . "}",$j.''); + .' "hello":true'."\n" + ."}",$j.''); } /** * @test @@ -212,8 +325,8 @@ public function testFormat02() { $j = new JsonX([],true); $j->addNumber('hello',66); $this->assertEquals("{\n" - . ' "hello":66'."\n" - . "}",$j.''); + .' "hello":66'."\n" + ."}",$j.''); } /** * @test @@ -223,9 +336,9 @@ public function testFormat03() { $j->addString('hello','world'); $j->addString('hello2','another string'); $this->assertEquals("{\n" - . ' "hello":"world", '."\n" - . ' "hello2":"another string"'."\n" - . "}",$j.''); + .' "hello":"world", '."\n" + .' "hello2":"another string"'."\n" + ."}",$j.''); } /** * @test @@ -234,9 +347,9 @@ public function testFormat04() { $j = new JsonX([],true); $j->addArray('hello-arr',[]); $this->assertEquals("{\n" - . ' "hello-arr":['."\n" - . ' ]'."\n" - . "}",$j.''); + .' "hello-arr":['."\n" + .' ]'."\n" + ."}",$j.''); } /** * @test @@ -245,13 +358,13 @@ public function testFormat05() { $j = new JsonX([],true); $j->addArray('hello-arr',[1,2,3,4]); $this->assertEquals("{\n" - . ' "hello-arr":['."\n" - . ' 1, '."\n" - . ' 2, '."\n" - . ' 3, '."\n" - . ' 4'."\n" - . ' ]'."\n" - . "}",$j.''); + .' "hello-arr":['."\n" + .' 1, '."\n" + .' 2, '."\n" + .' 3, '."\n" + .' 4'."\n" + .' ]'."\n" + ."}",$j.''); } /** * @test @@ -260,14 +373,14 @@ public function testFormat06() { $j = new JsonX([],true); $j->addArray('hello-arr',[[],["hello world"]]); $this->assertEquals("{\n" - . ' "hello-arr":['."\n" - . ' ['."\n" - . ' ], '."\n" - . ' ['."\n" - . ' "hello world"'."\n" - . ' ]'."\n" - . ' ]'."\n" - . "}",$j.''); + .' "hello-arr":['."\n" + .' ['."\n" + .' ], '."\n" + .' ['."\n" + .' "hello world"'."\n" + .' ]'."\n" + .' ]'."\n" + ."}",$j.''); } /** * @test @@ -276,50 +389,18 @@ public function testFormat07() { $j = new JsonX([],true); $j->addArray('hello-arr',[[],["hello world",["another sub","with two elements"]]]); $this->assertEquals("{\n" - . ' "hello-arr":['."\n" - . ' ['."\n" - . ' ], '."\n" - . ' ['."\n" - . ' "hello world", '."\n" - . ' ['."\n" - . ' "another sub", '."\n" - . ' "with two elements"'."\n" - . ' ]'."\n" - . ' ]'."\n" - . ' ]'."\n" - . "}",$j.''); - } - /** - * @test - */ - public function testFormat17() { - $j = new JsonX([],true); - $j->addArray('hello-arr',["my-j"=>new JsonX(),new JsonX(['hello'=>"world"])],true); - $this->assertEquals("{\n" - . ' "hello-arr":{'."\n" - . ' "my-j":{'."\n" - . ' }, '."\n" - . ' "0":{'."\n" - . ' "hello":"world"'."\n" - . ' }'."\n" - . ' }'."\n" - . "}",$j.''); - } - /** - * @test - */ - public function testFormat16() { - $j = new JsonX([],true); - $j->addArray('hello-arr',[new JsonX(),new JsonX(['hello'=>"world"])]); - $this->assertEquals("{\n" - . ' "hello-arr":['."\n" - . ' {'."\n" - . ' }, '."\n" - . ' {'."\n" - . ' "hello":"world"'."\n" - . ' }'."\n" - . ' ]'."\n" - . "}",$j.''); + .' "hello-arr":['."\n" + .' ['."\n" + .' ], '."\n" + .' ['."\n" + .' "hello world", '."\n" + .' ['."\n" + .' "another sub", '."\n" + .' "with two elements"'."\n" + .' ]'."\n" + .' ]'."\n" + .' ]'."\n" + ."}",$j.''); } /** * @test @@ -328,9 +409,9 @@ public function testFormat08() { $j = new JsonX([],true); $j->addArray('hello-arr',[], true); $this->assertEquals("{\n" - . ' "hello-arr":{'."\n" - . ' }'."\n" - . "}",$j.''); + .' "hello-arr":{'."\n" + .' }'."\n" + ."}",$j.''); } /** * @test @@ -339,34 +420,34 @@ public function testFormat09() { $j = new JsonX([],true); $j->addArray('hello-arr',[1, 2, 3, "hello mr ali"], true); $this->assertEquals("{\n" - . ' "hello-arr":{'."\n" - . ' "0":1, '."\n" - . ' "1":2, '."\n" - . ' "2":3, '."\n" - . ' "3":"hello mr ali"'."\n" - . ' }'."\n" - . "}",$j.''); + .' "hello-arr":{'."\n" + .' "0":1, '."\n" + .' "1":2, '."\n" + .' "2":3, '."\n" + .' "3":"hello mr ali"'."\n" + .' }'."\n" + ."}",$j.''); } /** * @test */ public function testFormat10() { $j = new JsonX([],true); - $j->addArray('hello-arr',["is-good"=>"You are good", 2, 3, "hello mr ali",[],["a sub with element","hello"=>'world']], true); + $j->addArray('hello-arr',["is-good" => "You are good", 2, 3, "hello mr ali",[],["a sub with element","hello" => 'world']], true); $this->assertEquals("{\n" - . ' "hello-arr":{'."\n" - . ' "is-good":"You are good", '."\n" - . ' "0":2, '."\n" - . ' "1":3, '."\n" - . ' "2":"hello mr ali", '."\n" - . ' "3":{'."\n" - . ' }, '."\n" - . ' "4":{'."\n" - . ' "0":"a sub with element", '."\n" - . ' "hello":"world"'."\n" - . ' }'."\n" - . ' }'."\n" - . "}",$j.''); + .' "hello-arr":{'."\n" + .' "is-good":"You are good", '."\n" + .' "0":2, '."\n" + .' "1":3, '."\n" + .' "2":"hello mr ali", '."\n" + .' "3":{'."\n" + .' }, '."\n" + .' "4":{'."\n" + .' "0":"a sub with element", '."\n" + .' "hello":"world"'."\n" + .' }'."\n" + .' }'."\n" + ."}",$j.''); } /** * @test @@ -376,12 +457,12 @@ public function testFormat11() { $obj = new Obj1('Hello', 0, true, null, 'he'); $j->addObject('object', $obj); $this->assertEquals('{'."\n" - . ' "object":{'."\n" - . ' "property-00":"Hello", '."\n" - . ' "property-01":0, '."\n" - . ' "property-02":true'."\n" - . ' }'."\n" - . '}',$j.''); + .' "object":{'."\n" + .' "property-00":"Hello", '."\n" + .' "property-01":0, '."\n" + .' "property-02":true'."\n" + .' }'."\n" + .'}',$j.''); } /** * @test @@ -391,14 +472,14 @@ public function testFormat12() { $obj = new Obj1('Hello', 0, true, null, 'he'); $j->addArray('array', [$obj]); $this->assertEquals('{'."\n" - . ' "array":['."\n" - . ' {'."\n" - . ' "property-00":"Hello", '."\n" - . ' "property-01":0, '."\n" - . ' "property-02":true'."\n" - . ' }'."\n" - . ' ]'."\n" - . '}',$j.''); + .' "array":['."\n" + .' {'."\n" + .' "property-00":"Hello", '."\n" + .' "property-01":0, '."\n" + .' "property-02":true'."\n" + .' }'."\n" + .' ]'."\n" + .'}',$j.''); } /** * @test @@ -408,14 +489,14 @@ public function testFormat13() { $obj = new Obj1('Hello', 0, true, null, 'he'); $j->addArray('array', [$obj], true); $this->assertEquals('{'."\n" - . ' "array":{'."\n" - . ' "0":{'."\n" - . ' "property-00":"Hello", '."\n" - . ' "property-01":0, '."\n" - . ' "property-02":true'."\n" - . ' }'."\n" - . ' }'."\n" - . '}',$j.''); + .' "array":{'."\n" + .' "0":{'."\n" + .' "property-00":"Hello", '."\n" + .' "property-01":0, '."\n" + .' "property-02":true'."\n" + .' }'."\n" + .' }'."\n" + .'}',$j.''); } /** * @test @@ -423,165 +504,84 @@ public function testFormat13() { public function testFormat14() { $j = new JsonX([],true); $obj = new Obj1('Hello', 0, true, null, 'he'); - $j->addArray('array', ["my-obj"=>$obj,"empty-arr"=>[]], true); + $j->addArray('array', ["my-obj" => $obj,"empty-arr" => []], true); $this->assertEquals('{'."\n" - . ' "array":{'."\n" - . ' "my-obj":{'."\n" - . ' "property-00":"Hello", '."\n" - . ' "property-01":0, '."\n" - . ' "property-02":true'."\n" - . ' }, '."\n" - . ' "empty-arr":{'."\n" - . ' }'."\n" - . ' }'."\n" - . '}',$j.''); + .' "array":{'."\n" + .' "my-obj":{'."\n" + .' "property-00":"Hello", '."\n" + .' "property-01":0, '."\n" + .' "property-02":true'."\n" + .' }, '."\n" + .' "empty-arr":{'."\n" + .' }'."\n" + .' }'."\n" + .'}',$j.''); } /** * @test */ public function testFormat15() { $j = new JsonX([ - "hello"=>"world", - 'object'=>new Obj0('8', 7, '6', '5', 4), - 'null'=>null, - 'nan'=>NAN, - 'inf'=>INF, - 'bool'=>true, - 'number'=>667, - 'jsonx'=> new JsonX(['sub-json-x'=>new JsonX()]) + "hello" => "world", + 'object' => new Obj0('8', 7, '6', '5', 4), + 'null' => null, + 'nan' => NAN, + 'inf' => INF, + 'bool' => true, + 'number' => 667, + 'jsonx' => new JsonX(['sub-json-x' => new JsonX()]) ], true); $this->assertEquals('' - . '{'."\n" - . ' "hello":"world", '."\n" - . ' "object":{'."\n" - . ' "prop-0":"8", '."\n" - . ' "prop-1":7, '."\n" - . ' "prop-2":"6", '."\n" - . ' "prop-3":4'."\n" - . ' }, '."\n" - . ' "null":null, '."\n" - . ' "nan":"NAN", '."\n" - . ' "inf":"INF", '."\n" - . ' "bool":true, '."\n" - . ' "number":667, '."\n" - . ' "jsonx":{'."\n" - . ' "sub-json-x":{}'."\n" - . ' }'."\n" - . '}' - . '',$j.''); - } - /** - * @test - */ - public function testAddArray01() { - $j = new JsonX(); - $arr = [1,"Hello",true,NAN,null,99.8,INF]; - $j->addArray('arr', $arr); - $this->assertEquals('{"arr":[1, "Hello", true, "NAN", null, 99.8, "INF"]}',$j.''); - } - /** - * @test - */ - public function testAddArray02() { - $j = new JsonX(); - $arr = [1,1.5,"Hello",true,NAN,null,INF]; - $j->addArray('arr', $arr,false); - $this->assertEquals('{"arr":[1, 1.5, "Hello", true, "NAN", null, "INF"]}',$j.''); - } - /** - * @test - */ - public function testAddArray03() { - $j = new JsonX(); - $arr = ["number" => 1,"Hello" => "world!","boolean" => true,NAN,null]; - $j->addArray('arr', $arr); - $this->assertEquals('{"arr":[{"number":1}, {"Hello":"world!"}, {"boolean":true}, "NAN", null]}',$j.''); - } - /** - * @test - */ - public function testAddBoolean00() { - $j = new JsonX(); - $j->addBoolean('bool ', true); - $this->assertEquals('{"bool":true}',$j.''); - } - /** - * @test - */ - public function testAddNumber00() { - $j = new JsonX(); - $j->addNumber(' number', 33); - $this->assertEquals('{"number":33}',$j.''); + .'{'."\n" + .' "hello":"world", '."\n" + .' "object":{'."\n" + .' "prop-0":"8", '."\n" + .' "prop-1":7, '."\n" + .' "prop-2":"6", '."\n" + .' "prop-3":4'."\n" + .' }, '."\n" + .' "null":null, '."\n" + .' "nan":"NAN", '."\n" + .' "inf":"INF", '."\n" + .' "bool":true, '."\n" + .' "number":667, '."\n" + .' "jsonx":{'."\n" + .' "sub-json-x":{}'."\n" + .' }'."\n" + .'}' + .'',$j.''); } /** * @test */ - public function testAddObj00() { - $j = new JsonX(); - $obj = new Obj0('Hello', 0, true, null, 'he'); - $j->addObject('object', $obj); - $this->assertEquals('{"object":{"prop-0":"Hello", "prop-1":0, "prop-2":true, "prop-3":"he"}}',$j.''); - } - - /** - * @test - */ - public function testAddObj01() { - $j = new JsonX(); - $obj = new Obj1('Hello', 0, true, null, 'he'); - $j->addObject('object', $obj); - $this->assertEquals('{"object":{"property-00":"Hello", "property-01":0, "property-02":true}}',$j.''); - } - /** - * @test - */ - public function testAddStringTest00() { - $j = new JsonX(); - $this->assertFalse($j->addString('', 'Hello World!')); - $this->assertFalse($j->addString(' ', 'Hello World!')); - $this->assertFalse($j->addString("\n", 'Hello World!')); - $this->assertEquals('{}',$j.''); - } - /** - * @test - */ - public function testAddStringTest01() { - $j = new JsonX(); - $this->assertTrue($j->addString('hello', 'Hello World!')); - $this->assertEquals('{"hello":"Hello World!"}',$j.''); - } - /** - * @test - */ - public function testAddStringTest02() { - $j = new JsonX(); - $this->assertFalse($j->addString('invalid-boolean', 'falseX',true)); - } - /** - * @test - */ - public function testEscJSonSpecialChars00() { - $str = 'I\'m "Good".'; - $result = JsonX::escapeJSONSpecialChars($str); - $this->assertEquals('I\'m \"Good\".',$result); - } - /** - * @test - */ - public function testEscJSonSpecialChars01() { - $str = 'Path: "C:/Windows/Media/onestop.midi"\n'; - $result = JsonX::escapeJSONSpecialChars($str); - $this->assertEquals('Path: \"C:\/Windows\/Media\/onestop.midi\"\\\\n',$result); + public function testFormat16() { + $j = new JsonX([],true); + $j->addArray('hello-arr',[new JsonX(),new JsonX(['hello' => "world"])]); + $this->assertEquals("{\n" + .' "hello-arr":['."\n" + .' {'."\n" + .' }, '."\n" + .' {'."\n" + .' "hello":"world"'."\n" + .' }'."\n" + .' ]'."\n" + ."}",$j.''); } /** * @test */ - public function testEscJSonSpecialChars02() { - $str = '\tI\'m good. But "YOU" are "Better".\r\n' - .'\\An inline comment is good.'; - $result = JsonX::escapeJSONSpecialChars($str); - $this->assertEquals('\\\\tI\'m good. But \"YOU\" are \"Better\".\\\\r\\\\n' - .'\\\\An inline comment is good.',$result); + public function testFormat17() { + $j = new JsonX([],true); + $j->addArray('hello-arr',["my-j" => new JsonX(),new JsonX(['hello' => "world"])],true); + $this->assertEquals("{\n" + .' "hello-arr":{'."\n" + .' "my-j":{'."\n" + .' }, '."\n" + .' "0":{'."\n" + .' "hello":"world"'."\n" + .' }'."\n" + .' }'."\n" + ."}",$j.''); } /** * @test From bbb9bd0afb584fe558b38276cd4ea49094e9f00b Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Fri, 20 Mar 2020 20:51:40 +0300 Subject: [PATCH 3/3] Create .gitattributes --- .gitattributes | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..778964a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +# Files and folders here will be not included when creating package +/tests export-ignore +/.github export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/phpunit.xml export-ignore \ No newline at end of file