Skip to content

Commit

Permalink
Merge pull request #67 from mehmetbeyHZ/master
Browse files Browse the repository at this point in the history
Single quotes in json data | bug
  • Loading branch information
mavinoo authored Jun 19, 2021
2 parents 06ec708 + af35026 commit 46300f0
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Common/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function mysql_escape($fieldValue)
}

if(self::is_json($fieldValue)){
return $fieldValue;
return self::safeJson($fieldValue);
}

if (!empty($fieldValue) && is_string($fieldValue)) {
Expand All @@ -35,11 +35,40 @@ public static function mysql_escape($fieldValue)
return $fieldValue;
}

protected static function safeJsonString($fieldValue){
return str_replace(
["'"],
["''"],
$fieldValue
);
}

protected static function is_json($str): bool
{
if (!is_string($str)){
return false;
}
return json_decode($str, true) !== null;
}

protected static function safeJson($jsonData,$asArray = false){
$jsonData = json_decode($jsonData,true);
$safeJsonData = [];
if (!is_array($jsonData)){
return $jsonData;
}
foreach ($jsonData as $key => $value){
if (self::is_json($value)){
$safeJsonData[$key] = self::safeJson($value,true);
}elseif(is_string($value)){
$safeJsonData[$key] = self::safeJsonString($value);
}elseif(is_array($value)){
$safeJsonData[$key] = self::safeJson(json_encode($value),true);
}else{
$safeJsonData[$key] = $value;
}
}
return $asArray ? $safeJsonData : json_encode($safeJsonData);
}

}

0 comments on commit 46300f0

Please sign in to comment.