Answer the question
In order to leave comments, you need to log in
Can't inject via method in Symfony 3.2, what's the right way?
There is a code:
app.stock_transformer:
class: AppBundle\DataTransformer\InboundApi\StockTransformer
calls:
- method: setLogger
arguments:
- '@logger'
Answer the question
In order to leave comments, you need to log in
# 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());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question