Skip to content

Commit

Permalink
Merge pull request vaibhavpandeyvpz#9 from felixble/feature/vaibhavpa…
Browse files Browse the repository at this point in the history
…ndeyvpz#7-support-json-format

Accept reports in JSON-format - closes vaibhavpandeyvpz#7
  • Loading branch information
vaibhavpandeyvpz authored Jan 19, 2017
2 parents 39c1dd0 + 94cfb74 commit 58eec54
Showing 1 changed file with 63 additions and 5 deletions.
68 changes: 63 additions & 5 deletions classes/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public function save()
->execute();
if ($result->rowCount() == 1) {
$application = $result->fetchColumn(0);
$post = $request->request->all();
$data = array();
foreach ($post as $key => $value) {
$data[strtolower($key)] = $value;
}
$data = $this->extractDataFromRequest($request);
$data = array_replace($data, array(
'application_id' => $application,
'checksum' => hash('md5', $data['package_name'] . $data['stack_trace']),
Expand Down Expand Up @@ -59,4 +55,66 @@ public function save()
throw new BadRequestHttpException();
}
}

/**
* Extracts only the passed data from the request
* either in the JSON-format from the request body
* or as form-urlencoded string from the post-request.
*
* @param $request \Symfony\Component\HttpFoundation\Request
* @return array
*/
private function extractDataFromRequest($request)
{
if ($request->getContentType() == 'json') {
$source = json_decode($request->getContent(), true);
} else {
$source = $request->request->all();
}
$data = array();
foreach ($source as $key => $value) {
if (is_array($value)) {
$value = $this->keyValueArrayToString($value);
}
$data[strtolower($key)] = $value;
}
return $data;
}

/**
* Converts an array to a string with the
* format <code><key>=<value>\n</code>.
* <br><br>
* If the value is an array itself the array
* will be converted recursively the same way
* with the original key as a prefix like
* <code><key>.<sub-key>=<value>\n</code>.
*
* @param $keyValueArray
* @param string $keyPrefix
* @return string
*/
private function keyValueArrayToString($keyValueArray, $keyPrefix = '')
{
$result = '';
$first = true;
foreach($keyValueArray as $key => $value) {
if ($first) $first = false;
else $result .= "\n"; // double quote necessary!
if (is_array($value)) {
$prefix = $this->prepend($keyPrefix, $key);
$result .= $this->keyValueArrayToString($value, $prefix);
} else {
$result .= $this->prepend($keyPrefix, $key) . '=' . $value;
}
}
return $result;
}

private function prepend($prefix, $string, $delimiter = '.')
{
if ($prefix === '') return $string;
return $prefix . $delimiter . $string;
}

}

0 comments on commit 58eec54

Please sign in to comment.