Skip to content

Commit

Permalink
Run CS Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Feb 11, 2023
1 parent b41e083 commit c08ed0f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 60 deletions.
9 changes: 6 additions & 3 deletions webfiori/json/CaseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -171,6 +173,7 @@ private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFo
$retVal .= $to.$char;
}
$isNumFound = true;

return $retVal;
}
}
110 changes: 54 additions & 56 deletions webfiori/json/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -203,6 +203,7 @@ public function add(string $key, $value, $arrayAsObj = false) {
$this->addObject($key, $value) ||
$this->addNull($key);
}

return true;
}
/**
Expand Down Expand Up @@ -448,7 +449,7 @@ public static function decode($jsonStr) {

return $jsonXObj;
}

throw new JsonException(json_last_error_msg(), json_last_error());
}
/**
Expand Down Expand Up @@ -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'.
*
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion webfiori/json/JsonException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
*
* @author Ibrahim
*/
class JsonException extends Exception {}
class JsonException extends Exception {
}

0 comments on commit c08ed0f

Please sign in to comment.