Skip to content

Commit

Permalink
Replace json_decode with Json:decode
Browse files Browse the repository at this point in the history
  • Loading branch information
LucWollants committed Aug 8, 2024
1 parent f031b82 commit 2e31d69
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/Event/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CultuurNet\UDB3\Event;

use CultuurNet\UDB3\Category;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Category\Category as Udb3ModelCategory;
use InvalidArgumentException;

Expand All @@ -23,7 +24,7 @@ public function __construct(string $id, string $label)

public static function fromJSONLDEvent(string $eventString): ?EventType
{
$event = json_decode($eventString, false);
$event = Json::decode($eventString);
foreach ($event->terms as $term) {
if ($term->domain === self::DOMAIN) {
return new self($term->id, $term->label);
Expand Down
11 changes: 6 additions & 5 deletions src/Event/ReadModel/JSONLD/EventLDProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
use CultuurNet\UDB3\Event\ValueObjects\Audience;
use CultuurNet\UDB3\Event\ValueObjects\LocationId;
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Media\Serialization\MediaObjectSerializer;
use CultuurNet\UDB3\Model\Place\ImmutablePlace;
use CultuurNet\UDB3\Model\Serializer\Place\NilLocationNormalizer;
Expand All @@ -87,6 +88,7 @@
use CultuurNet\UDB3\RecordedOn;
use CultuurNet\UDB3\SameAsForUitInVlaanderen;
use CultuurNet\UDB3\Theme;
use JsonException;

final class EventLDProjector extends OfferLDProjector implements
EventListener,
Expand Down Expand Up @@ -581,11 +583,10 @@ public function placeJSONLD(string $placeId): array
}

try {
$placeJSONLD = $this->placeService->getEntity(
$placeId
);

return (array) json_decode($placeJSONLD);
$placeJSONLD = $this->placeService->getEntity($placeId);
return (array)Json::decode($placeJSONLD);
} catch (JsonException $e) {
return [];
} catch (EntityNotFoundException $e) {
// In case the place can not be found at the moment, just add its ID
return [
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Request/Body/JsonRequestBodyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CultuurNet\UDB3\Http\ApiProblem\ApiProblem;
use CultuurNet\UDB3\Http\ApiProblem\SchemaError;
use CultuurNet\UDB3\Json;
use InvalidArgumentException;
use JsonException;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -20,7 +21,7 @@ public function parse(ServerRequestInterface $request): ServerRequestInterface
}

try {
$decoded = json_decode($body, false, 512, JSON_THROW_ON_ERROR);
$decoded = Json::decode($body);
} catch (JsonException $e) {
throw ApiProblem::bodyInvalidSyntax('JSON');
}
Expand Down
7 changes: 5 additions & 2 deletions src/Offer/ReadModel/JSONLD/OfferLDProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
use CultuurNet\UDB3\Facility;
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Media\Image;
use CultuurNet\UDB3\Media\Serialization\MediaObjectSerializer;
use CultuurNet\UDB3\Model\Serializer\ValueObject\MediaObject\VideoNormalizer;
Expand Down Expand Up @@ -61,6 +62,7 @@
use CultuurNet\UDB3\RecordedOn;
use CultuurNet\UDB3\SluggerInterface;
use DateTimeInterface;
use JsonException;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;

Expand Down Expand Up @@ -939,8 +941,9 @@ public function organizerJSONLD(string $organizerId): array
{
try {
$organizerJSONLD = $this->organizerRepository->fetch($organizerId)->getRawBody();

return (array)json_decode($organizerJSONLD);
return (array)Json::decode($organizerJSONLD);
} catch (JsonException $e) {
return [];
} catch (DocumentDoesNotExist $e) {
// In case the place can not be found at the moment, just add its ID
return [
Expand Down
2 changes: 1 addition & 1 deletion src/Role/ReadModel/Labels/RoleLabelsProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function applyLabelDetailsProjectedToJSONLD(LabelDetailsProjectedToJSONLD
return;
}

$roles = json_decode($document->getRawBody());
$roles = Json::decode($document->getRawBody());

foreach ($roles as $roleId) {
$role = $this->getDocument(new UUID($roleId));
Expand Down
3 changes: 2 additions & 1 deletion src/Security/Permission/Sapi3RoleConstraintVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Security\Permission;

use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Role\ReadModel\Constraints\UserConstraintsReadRepositoryInterface;
use CultuurNet\UDB3\Role\ValueObjects\Permission;
use GuzzleHttp\Psr7\Request;
Expand Down Expand Up @@ -115,7 +116,7 @@ private function search(string $query): int

$response = $this->httpClient->sendRequest($request);

$decodedResponse = json_decode(
$decodedResponse = Json::decode(
$response->getBody()->getContents()
);

Expand Down
6 changes: 3 additions & 3 deletions tests/Event/ReadModel/JSONLD/EventLDProjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function it_handles_copied_events_with_an_incorrect_place_type(): void

$recordedOn = '2022-01-20T13:25:21+01:00';

$jsonLD = json_decode(file_get_contents(__DIR__ . '/copied_event_with_place_type.json'));
$jsonLD = Json::decode(file_get_contents(__DIR__ . '/copied_event_with_place_type.json'));
$jsonLD->created = $recordedOn;
$jsonLD->modified = $recordedOn;
$jsonLD->playhead = 1;
Expand Down Expand Up @@ -488,7 +488,7 @@ public function it_handles_copy_event(): void
DateTime::fromString($recordedOn)
);

$expectedJsonLD = json_decode(file_get_contents(__DIR__ . '/copied_event.json'));
$expectedJsonLD = Json::decode(file_get_contents(__DIR__ . '/copied_event.json'));
$expectedJsonLD->created = $recordedOn;
$expectedJsonLD->modified = $recordedOn;
$expectedJsonLD->creator = '20a72430-7e3e-4b75-ab59-043156b3169c';
Expand Down Expand Up @@ -525,7 +525,7 @@ public function it_projects_copied_event_with_work_hours_removed(): void
DateTime::fromString($recordedOn)
);

$expectedJsonLD = json_decode(file_get_contents(__DIR__ . '/copied_event_without_working_hours.json'));
$expectedJsonLD = Json::decode(file_get_contents(__DIR__ . '/copied_event_without_working_hours.json'));
$expectedJsonLD->created = $recordedOn;
$expectedJsonLD->modified = $recordedOn;
$expectedJsonLD->creator = $userId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace CultuurNet\UDB3\Event\ReadModel\JSONLD\Specifications;

use CultuurNet\UDB3\Json;

trait EventSpecificationTestTrait
{
protected function getEventLdFromFile(string $fileName): \stdClass
Expand All @@ -12,6 +14,6 @@ protected function getEventLdFromFile(string $fileName): \stdClass
__DIR__ . '/../../../samples/' . $fileName
);

return json_decode($jsonEvent);
return Json::decode($jsonEvent);
}
}
2 changes: 1 addition & 1 deletion tests/Organizer/OrganizerLDProjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public function it_should_update_name_when_updating_from_udb2_and_keep_missing_t
$organizerId = 'someId';

$organizerJson = file_get_contents(__DIR__ . '/Samples/organizer_with_main_language.json');
$organizerJson = json_decode($organizerJson);
$organizerJson = Json::decode($organizerJson);
$organizerJson->name->en = 'English name';
$organizerJson = Json::encode($organizerJson);

Expand Down

0 comments on commit 2e31d69

Please sign in to comment.