# JsonRequest
App\Service\JsonRequest:
calls:
- [setRequest, ['@request_stack']]
<?php
namespace App\Service;
use Exception;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Class JsonRequest
* @package App\Service
*/
class JsonRequest
{
private $json_object;
public function __construct()
{
}
/**
* @param string $key
* @param bool $default
* @return mixed
*/
public function get(string $key, $default = null)
{
try {
$properties = explode(".", $key);
$buf = $this->json_object;
foreach ($properties as $property) {
if (property_exists($buf, $property)) {
$buf = $buf->{$property};
} else {
return $default;
}
}
return $buf;
} catch (Exception $e) {
return $default;
}
}
/**
* @param string $key
* @return bool
*/
public function has(string $key)
{
return property_exists($this->json_object, $key);
}
/**
* @param RequestStack $request
*/
public function setRequest(RequestStack $request): void
{
$this->json_object = json_decode($request->getCurrentRequest()->getContent());
}
}