<?php
namespace App\Service;
use App\Service\JsonParserInterface;
use App\Exception\JsonException;
class JsonParser implements JsonParserInterface
{
public static function jsonDecode(string $string): string
{
$decodedJson = json_decode($string);
if (false === $decodedJson) {
throw new JsonException();
}
return $decodedJson;
}
}
<?php
try {
$decodedJson = JsonParser::jsonDecode($jsonToDecode);
} catch (JsonException $e) {
// Log, alert etc...
}
<?php
try {
$decodedJson = json_decode('bad json', null, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
// Log, alert etc...
}
<?php
namespace App\Service;
use App\Service\JsonParserInterface;
class JsonParser implements JsonParserInterface
{
/**
* @throws \JsonException
*/
public static function jsonDecode(string $string): string
{
return json_decode($string, null, 512, JSON_THROW_ON_ERROR);
}
}
<?php
namespace App\Service;
use App\Service\JsonParserInterface;
class JsonParser implements JsonParserInterface
{
/**
* @throws \JsonException
*/
public static function jsonDecode(string $string): string
{
return json_decode($string, flags: JSON_THROW_ON_ERROR);
}
}
<?php
function sum(
string $a,
string $b,
) {
return $a + $b;
}
<?php
class UserPDFDocumentConvertor
{
public function __construct(
protected UserInterface $userInterface,
protected DocumentManager $documentManager,
protected EntityManager $entityManager,
protected UserToSimpleUserMapper $userToSimpleUserMapper,
protected PDFConvertor $pdfConvertor,
) {}
}