Skip to content

Commit

Permalink
Update Html.php
Browse files Browse the repository at this point in the history
  • Loading branch information
magefan authored Nov 1, 2024
1 parent 9b73ba8 commit 0e26ae9
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Model/Parser/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,21 @@ private function getProductIds(string $html): array

return $productIds;
}

private function json_validate($json, $depth = 512, $flags = 0)
{
// First character check to ensure the string starts with `{` or `[` (to improve perfrormace)
$trimmedJson = ltrim($json);
if (!is_string($json) || ($trimmedJson[0] !== '{' && $trimmedJson[0] !== '[')) {
if (!is_string($json)) {
return false;
}

try {
json_decode($json, false, $depth, $flags | JSON_THROW_ON_ERROR);
return true;
} catch (\JsonException $e) {
$trimmedJson = ltrim($json);
// First character check to ensure the string starts with `{` or `[` (to improve perfrormace)
if ($trimmedJson[0] !== '{' && $trimmedJson[0] !== '[') {
return false;
}

// Decode JSON and check for errors
json_decode($json, false, $depth, $flags);
return json_last_error() === JSON_ERROR_NONE;
}
}

0 comments on commit 0e26ae9

Please sign in to comment.