Skip to content

Commit

Permalink
Merge pull request #12 from myLocalInfluence/feat/check-if-json-or-b64
Browse files Browse the repository at this point in the history
fix: Check If Json & B64 Encoded
  • Loading branch information
kangoo13 authored Dec 23, 2020
2 parents 17617bf + 6474519 commit dec6dd0
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/PubSub/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ public function getJobId()
*/
public function getRawBody()
{
$data = $this->job->data();
if (!empty($data) && $this->isBase64Encoded($data)) {
$data = base64_decode($data);
}
if (!empty($data) && $this->isJson($data)) {
$data = json_decode($data, true);
}
$fullData = [
'data' => json_decode(base64_decode($this->job->data()), true) ?? [],
'data' => $data ?? [],
'attributes' => $this->job->attributes() ?? []
];
$newArray = [
Expand All @@ -85,6 +92,22 @@ public function getRawBody()
return json_encode($newArray);
}

public function isJson(string $string) : bool
{
json_decode($string);

return (json_last_error() == JSON_ERROR_NONE);
}

public function isBase64Encoded(string $data) : bool
{
if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $data)) {
return true;
} else {
return false;
}
}

/**
* Delete the job from the queue.
*
Expand Down

0 comments on commit dec6dd0

Please sign in to comment.