From 3241531d39206a3c69d245d95b5f7c9bb6bade18 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Sat, 11 Feb 2023 21:49:10 +0300 Subject: [PATCH 1/6] Added Support for Storing Json Output --- tests/webfiori/tests/json/JsonTest.php | 20 +++++++++ webfiori/json/Json.php | 57 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/tests/webfiori/tests/json/JsonTest.php b/tests/webfiori/tests/json/JsonTest.php index b2f29bd..72e28e9 100644 --- a/tests/webfiori/tests/json/JsonTest.php +++ b/tests/webfiori/tests/json/JsonTest.php @@ -7,6 +7,26 @@ use PHPUnit\Framework\TestCase; class JsonTest extends TestCase { + /** + * @test + */ + public function testToJsonFile00() { + $json = new Json(); + $json->toJsonFile('json-file', ROOT.DIRECTORY_SEPARATOR.'json-output'); + $outputPath = ROOT.DIRECTORY_SEPARATOR.'json-output'.DIRECTORY_SEPARATOR.'json-file.json'; + $this->assertTrue(file_exists($outputPath)); + } + /** + * @test + * @depends testToJsonFile00 + */ + public function testToJsonFile01() { + $outputPath = ROOT.DIRECTORY_SEPARATOR.'json-output'.DIRECTORY_SEPARATOR.'json-file.json'; + $this->expectException(\Exception::class); + $this->expectExceptionMessage("File already exist: '$outputPath'"); + $json = new Json(); + $json->toJsonFile('json-file', ROOT.DIRECTORY_SEPARATOR.'json-output'); + } /** * @test */ diff --git a/webfiori/json/Json.php b/webfiori/json/Json.php index ed17eb0..c32a460 100644 --- a/webfiori/json/Json.php +++ b/webfiori/json/Json.php @@ -25,6 +25,7 @@ namespace webfiori\json; use InvalidArgumentException; +use Exception; /** * A class that can be used to create well formatted JSON strings. * @@ -481,6 +482,62 @@ public static function escapeJSONSpecialChars($string) { return $escapedJson; } + /** + * Attempt to write the generated JSON to a .json file. + * + * @param string $fileName The name of the file at which JSON output will be + * sent to. If the file does not exist, the method will attempt to create it. + * + * @param string $path The folder in file system that the file will be created + * at. If does not exist, the method will attempt to create it. + * + * @param bool $override If a file exist in the specified location with same + * name and this parameter is set to true, the method will override existing + * file by deleting it and creating new one. + * + * @throws Exception + */ + public function toJsonFile(string $fileName, string $path, bool $override = false) { + $nameTrim = trim($fileName); + + if (strlen($nameTrim) == 0) { + + throw new Exception('Invalid file name: '.$fileName, -1); + } + $pathTrimmed = trim(str_replace('\\', DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $path))); + + if (strlen($pathTrimmed) == 0) { + + throw new Exception('Invalid file path: '.$path, -1); + } + + if (!is_dir($pathTrimmed)) { + if (!mkdir($pathTrimmed, 0777 , true)) { + + throw new Exception("Unable to create directory '$pathTrimmed'", -1); + } + } + + $fixedName = explode('.', $fileName)[0]; + $fullPath = $path.DIRECTORY_SEPARATOR.$fixedName.'.json'; + + $isExist = file_exists($fullPath); + + if ($isExist && !$override) { + + throw new Exception("File already exist: '$fullPath'", -1);; + } else if ($isExist && $override) { + unlink($fullPath); + } + $resource = fopen($fullPath, 'wb'); + + if (!is_resource($resource)) { + + throw new Exception("Unable to open file for writing: '$fullPath'", -1);; + } + fwrite($resource, $this->toJSONString()); + fclose($resource); + } /** * Reads JSON data from a file and convert it to an object of type 'Json'. * From 83214dca172db3b7e8fe093a22b88c6bfbfccd1b Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Sat, 11 Feb 2023 22:14:46 +0300 Subject: [PATCH 2/6] Update loader.php --- tests/loader.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/loader.php b/tests/loader.php index 20e6dc1..e31d2f1 100644 --- a/tests/loader.php +++ b/tests/loader.php @@ -40,6 +40,7 @@ } define('ROOT', $rootDir); echo 'Root Directory: \''.$rootDir.'\'.'."\n"; +require_once $rootDir.'webfiori'.$DS.'json'.$DS.'JsonException.php'; require_once $rootDir.'webfiori'.$DS.'json'.$DS.'JsonConverter.php'; require_once $rootDir.'webfiori'.$DS.'json'.$DS.'CaseConverter.php'; require_once $rootDir.'webfiori'.$DS.'json'.$DS.'Property.php'; From db982985ed5dbf3088d574a8b58264bb04e002f7 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Sat, 11 Feb 2023 22:14:51 +0300 Subject: [PATCH 3/6] Update JsonTest.php --- tests/webfiori/tests/json/JsonTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/webfiori/tests/json/JsonTest.php b/tests/webfiori/tests/json/JsonTest.php index 72e28e9..5fbbd3b 100644 --- a/tests/webfiori/tests/json/JsonTest.php +++ b/tests/webfiori/tests/json/JsonTest.php @@ -630,11 +630,11 @@ public function testDecode06() { * @test */ public function testDecode07() { + $this->expectException(\webfiori\json\JsonException::class); + $this->expectExceptionMessage('Syntax error'); + $this->expectExceptionCode(4); $jsonStr = '{prop-1:1}'; $decoded = Json::decode($jsonStr); - $this->assertTrue(gettype($decoded) == 'array'); - $this->assertEquals(4,$decoded['error-code']); - $this->assertEquals('Syntax error',$decoded['error-message']); } /** * @test @@ -699,8 +699,6 @@ public function testDecode08() { */ public function testFromFile00() { $this->assertNull(Json::fromJsonFile(ROOT.DIRECTORY_SEPARATOR.'not-exist.json')); - $arr = Json::fromJsonFile(ROOT.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Obj0.php'); - $this->assertTrue(gettype($arr) == 'array'); $jsonx = Json::fromJsonFile(ROOT.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'composer.json'); $this->assertTrue($jsonx instanceof Json); $packagesArr = $jsonx->get('packages'); From 153e078a7e051b8f0b5c609ba32116d31e075cdf Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Sat, 11 Feb 2023 22:14:57 +0300 Subject: [PATCH 4/6] Update Json.php --- webfiori/json/Json.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/webfiori/json/Json.php b/webfiori/json/Json.php index c32a460..1510386 100644 --- a/webfiori/json/Json.php +++ b/webfiori/json/Json.php @@ -448,11 +448,8 @@ public static function decode($jsonStr) { return $jsonXObj; } - - return [ - 'error-code' => json_last_error(), - 'error-message' => json_last_error_msg() - ]; + + throw new JsonException(json_last_error_msg(), json_last_error()); } /** * Escape JSON special characters from string. @@ -502,20 +499,18 @@ public function toJsonFile(string $fileName, string $path, bool $override = fals if (strlen($nameTrim) == 0) { - throw new Exception('Invalid file name: '.$fileName, -1); + throw new JsonException('Invalid file name: '.$fileName, -1); } $pathTrimmed = trim(str_replace('\\', DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $path))); if (strlen($pathTrimmed) == 0) { - throw new Exception('Invalid file path: '.$path, -1); + throw new JsonException('Invalid file path: '.$path, -1); } - if (!is_dir($pathTrimmed)) { - if (!mkdir($pathTrimmed, 0777 , true)) { + if (!is_dir($pathTrimmed) && !mkdir($pathTrimmed, 0777 , true)) { - throw new Exception("Unable to create directory '$pathTrimmed'", -1); - } + throw new JsonException("Unable to create directory '$pathTrimmed'", -1); } $fixedName = explode('.', $fileName)[0]; @@ -525,7 +520,7 @@ public function toJsonFile(string $fileName, string $path, bool $override = fals if ($isExist && !$override) { - throw new Exception("File already exist: '$fullPath'", -1);; + throw new JsonException("File already exist: '$fullPath'", -1);; } else if ($isExist && $override) { unlink($fullPath); } @@ -533,7 +528,7 @@ public function toJsonFile(string $fileName, string $path, bool $override = fals if (!is_resource($resource)) { - throw new Exception("Unable to open file for writing: '$fullPath'", -1);; + throw new JsonException("Unable to open file for writing: '$fullPath'", -1);; } fwrite($resource, $this->toJSONString()); fclose($resource); From b41e083d84ebc63a2a30f39f0321afb2982dce70 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Sat, 11 Feb 2023 22:15:02 +0300 Subject: [PATCH 5/6] Create JsonException.php --- webfiori/json/JsonException.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 webfiori/json/JsonException.php diff --git a/webfiori/json/JsonException.php b/webfiori/json/JsonException.php new file mode 100644 index 0000000..87b41d6 --- /dev/null +++ b/webfiori/json/JsonException.php @@ -0,0 +1,10 @@ + Date: Sat, 11 Feb 2023 22:18:33 +0300 Subject: [PATCH 6/6] Run CS Fixer --- webfiori/json/CaseConverter.php | 9 ++- webfiori/json/Json.php | 110 ++++++++++++++++---------------- webfiori/json/JsonException.php | 3 +- 3 files changed, 62 insertions(+), 60 deletions(-) diff --git a/webfiori/json/CaseConverter.php b/webfiori/json/CaseConverter.php index b7902f2..9d7a7fb 100644 --- a/webfiori/json/CaseConverter.php +++ b/webfiori/json/CaseConverter.php @@ -124,10 +124,10 @@ private static function _toSnakeOrKebab($value, $from, $to) { $retVal = ''; $isNumFound = false; $snakeOrKebabFound = false; - + for ($x = 0 ; $x < strlen($attr1) ; $x++) { $char = $attr1[$x]; - + if ($char == $to) { $snakeOrKebabFound = true; $retVal .= $char; @@ -143,7 +143,7 @@ private static function _toSnakeOrKebab($value, $from, $to) { private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) { $isUpper = self::_isUpper($char); $retVal = ''; - + if (($isUpper || $isNumFound) && $x != 0 && !$snakeOrKebabFound) { $retVal .= $to.strtolower($char); } else if ($isUpper && $x == 0) { @@ -156,11 +156,13 @@ private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFoun } $snakeOrKebabFound = false; $isNumFound = false; + return $retVal; } private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) { $retVal = ''; + if ($x == 0) { $isNumFound = true; $retVal .= $char; @@ -171,6 +173,7 @@ private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFo $retVal .= $to.$char; } $isNumFound = true; + return $retVal; } } diff --git a/webfiori/json/Json.php b/webfiori/json/Json.php index 1510386..81d538d 100644 --- a/webfiori/json/Json.php +++ b/webfiori/json/Json.php @@ -24,8 +24,8 @@ */ namespace webfiori\json; -use InvalidArgumentException; use Exception; +use InvalidArgumentException; /** * A class that can be used to create well formatted JSON strings. * @@ -203,6 +203,7 @@ public function add(string $key, $value, $arrayAsObj = false) { $this->addObject($key, $value) || $this->addNull($key); } + return true; } /** @@ -448,7 +449,7 @@ public static function decode($jsonStr) { return $jsonXObj; } - + throw new JsonException(json_last_error_msg(), json_last_error()); } /** @@ -479,60 +480,6 @@ public static function escapeJSONSpecialChars($string) { return $escapedJson; } - /** - * Attempt to write the generated JSON to a .json file. - * - * @param string $fileName The name of the file at which JSON output will be - * sent to. If the file does not exist, the method will attempt to create it. - * - * @param string $path The folder in file system that the file will be created - * at. If does not exist, the method will attempt to create it. - * - * @param bool $override If a file exist in the specified location with same - * name and this parameter is set to true, the method will override existing - * file by deleting it and creating new one. - * - * @throws Exception - */ - public function toJsonFile(string $fileName, string $path, bool $override = false) { - $nameTrim = trim($fileName); - - if (strlen($nameTrim) == 0) { - - throw new JsonException('Invalid file name: '.$fileName, -1); - } - $pathTrimmed = trim(str_replace('\\', DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $path))); - - if (strlen($pathTrimmed) == 0) { - - throw new JsonException('Invalid file path: '.$path, -1); - } - - if (!is_dir($pathTrimmed) && !mkdir($pathTrimmed, 0777 , true)) { - - throw new JsonException("Unable to create directory '$pathTrimmed'", -1); - } - - $fixedName = explode('.', $fileName)[0]; - $fullPath = $path.DIRECTORY_SEPARATOR.$fixedName.'.json'; - - $isExist = file_exists($fullPath); - - if ($isExist && !$override) { - - throw new JsonException("File already exist: '$fullPath'", -1);; - } else if ($isExist && $override) { - unlink($fullPath); - } - $resource = fopen($fullPath, 'wb'); - - if (!is_resource($resource)) { - - throw new JsonException("Unable to open file for writing: '$fullPath'", -1);; - } - fwrite($resource, $this->toJSONString()); - fclose($resource); - } /** * Reads JSON data from a file and convert it to an object of type 'Json'. * @@ -717,6 +664,57 @@ public function setPropsStyle($style) { } } } + /** + * Attempt to write the generated JSON to a .json file. + * + * @param string $fileName The name of the file at which JSON output will be + * sent to. If the file does not exist, the method will attempt to create it. + * + * @param string $path The folder in file system that the file will be created + * at. If does not exist, the method will attempt to create it. + * + * @param bool $override If a file exist in the specified location with same + * name and this parameter is set to true, the method will override existing + * file by deleting it and creating new one. + * + * @throws Exception + */ + public function toJsonFile(string $fileName, string $path, bool $override = false) { + $nameTrim = trim($fileName); + + if (strlen($nameTrim) == 0) { + throw new JsonException('Invalid file name: '.$fileName, -1); + } + $pathTrimmed = trim(str_replace('\\', DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $path))); + + if (strlen($pathTrimmed) == 0) { + throw new JsonException('Invalid file path: '.$path, -1); + } + + if (!is_dir($pathTrimmed) && !mkdir($pathTrimmed, 0777 , true)) { + throw new JsonException("Unable to create directory '$pathTrimmed'", -1); + } + + $fixedName = explode('.', $fileName)[0]; + $fullPath = $path.DIRECTORY_SEPARATOR.$fixedName.'.json'; + + $isExist = file_exists($fullPath); + + if ($isExist && !$override) { + throw new JsonException("File already exist: '$fullPath'", -1); + } else { + if ($isExist && $override) { + unlink($fullPath); + } + } + $resource = fopen($fullPath, 'wb'); + + if (!is_resource($resource)) { + throw new JsonException("Unable to open file for writing: '$fullPath'", -1); + } + fwrite($resource, $this->toJSONString()); + fclose($resource); + } /** * Creates and returns a well formatted JSON string that will be created using * provided data. diff --git a/webfiori/json/JsonException.php b/webfiori/json/JsonException.php index 87b41d6..f3056bb 100644 --- a/webfiori/json/JsonException.php +++ b/webfiori/json/JsonException.php @@ -7,4 +7,5 @@ * * @author Ibrahim */ -class JsonException extends Exception {} +class JsonException extends Exception { +}