Skip to content

Commit

Permalink
Replace json_encode with Json::encode
Browse files Browse the repository at this point in the history
  • Loading branch information
LucWollants committed Aug 8, 2024
1 parent ce61f48 commit f031b82
Show file tree
Hide file tree
Showing 44 changed files with 159 additions and 119 deletions.
4 changes: 3 additions & 1 deletion features/Steps/OwnershipSteps.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace CultuurNet\UDB3\Steps;

use CultuurNet\UDB3\Json;

trait OwnershipSteps
{
/**
Expand All @@ -17,7 +19,7 @@ public function iRequestOwnershipForOnTheOrganizerWithOrganizerIdAndSaveTheAs(
): void {
$this->requestOwnership(
'/ownerships',
$this->variableState->replaceVariables(json_encode([
$this->variableState->replaceVariables(Json::encode([
'itemId' => $organizerId,
'itemType' => 'organizer',
'ownerId' => $ownerId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Broadway\Domain\DomainMessage;
use Broadway\Serializer\Serializable;
use Broadway\Serializer\SerializationException;
use CultuurNet\UDB3\Json;

class EntireDomainMessageBodyFactory implements BodyFactoryInterface
{
Expand All @@ -23,7 +24,7 @@ public function createBody(DomainMessage $domainMessage): string
'recorded_on' => $domainMessage->getRecordedOn()->toString(),
];

return json_encode($data);
return Json::encode($data);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Broadway/AMQP/Message/Body/PayloadOnlyBodyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Broadway\Domain\DomainMessage;
use Broadway\Serializer\Serializable;
use Broadway\Serializer\SerializationException;
use CultuurNet\UDB3\Json;

class PayloadOnlyBodyFactory implements BodyFactoryInterface
{
public function createBody(DomainMessage $domainMessage): string
{
$this->guardSerializable($domainMessage->getPayload());

return json_encode(
return Json::encode(
$domainMessage->getPayload()->serialize()
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Http/Deserializer/Event/MajorInfoJSONDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use CultuurNet\UDB3\Http\Deserializer\Calendar\CalendarJSONDeserializer;
use CultuurNet\UDB3\Http\Deserializer\Calendar\CalendarJSONParser;
use CultuurNet\UDB3\Http\Deserializer\Theme\ThemeJSONDeserializer;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Model\ValueObject\Text\Title;

/**
Expand Down Expand Up @@ -49,9 +50,9 @@ public function deserialize(string $data): MajorInfo
$data = parent::deserialize($data);
$this->validator->validate($data);

$type = $this->typeDeserializer->deserialize(json_encode($data['type']));
$type = $this->typeDeserializer->deserialize(Json::encode($data['type']));

$calendar = $this->calendarDeserializer->deserialize(json_encode($data['calendar']));
$calendar = $this->calendarDeserializer->deserialize(Json::encode($data['calendar']));

$locationId = $data['location'];
if (is_array($locationId) && isset($locationId['id'])) {
Expand All @@ -61,7 +62,7 @@ public function deserialize(string $data): MajorInfo

$theme = null;
if (!empty($data['theme'])) {
$theme = $this->themeDeserializer->deserialize(json_encode($data['theme']));
$theme = $this->themeDeserializer->deserialize(Json::encode($data['theme']));
}

return new MajorInfo(
Expand Down
7 changes: 4 additions & 3 deletions src/Http/Deserializer/Place/MajorInfoJSONDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use CultuurNet\UDB3\Http\Deserializer\Calendar\CalendarJSONDeserializer;
use CultuurNet\UDB3\Http\Deserializer\Calendar\CalendarJSONParser;
use CultuurNet\UDB3\Http\Deserializer\Event\EventTypeJSONDeserializer;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Model\ValueObject\Text\Title;

/**
Expand Down Expand Up @@ -49,11 +50,11 @@ public function deserialize(string $data): MajorInfo
$data = parent::deserialize($data);
$this->validator->validate($data);

$type = $this->typeDeserializer->deserialize(json_encode($data['type']));
$type = $this->typeDeserializer->deserialize(Json::encode($data['type']));

$address = $this->addressDeserializer->deserialize(json_encode($data['address']));
$address = $this->addressDeserializer->deserialize(Json::encode($data['address']));

$calendar = $this->calendarDeserializer->deserialize(json_encode($data['calendar']));
$calendar = $this->calendarDeserializer->deserialize(Json::encode($data['calendar']));

return new MajorInfo(
new Title($data['name']),
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CultuurNet\UDB3\Http\Response;

use CultuurNet\UDB3\Http\ApiProblem\ApiProblem;
use CultuurNet\UDB3\Json;
use Fig\Http\Message\StatusCodeInterface;
use JsonException;
use Slim\Psr7\Factory\StreamFactory;
Expand All @@ -22,7 +23,7 @@ public function __construct($data, int $status = StatusCodeInterface::STATUS_OK,
{
try {
if (!is_string($data)) {
$data = json_encode($data, JSON_THROW_ON_ERROR);
$data = Json::encode($data);
}

$body = (new StreamFactory())->createStream($data);
Expand Down
3 changes: 2 additions & 1 deletion src/Offer/Commands/AddLabelToMultipleJSONDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CultuurNet\UDB3\Deserializer\JSONDeserializer;
use CultuurNet\UDB3\Deserializer\MissingValueException;
use CultuurNet\UDB3\Deserializer\NotWellFormedException;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Label;
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\LabelName;
use CultuurNet\UDB3\Offer\OfferIdentifierCollection;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function deserialize(string $data): AddLabelToMultiple

foreach ($data->offers as $offer) {
$offers = $offers->with(
$this->offerIdentifierDeserializer->deserialize(json_encode($offer))
$this->offerIdentifierDeserializer->deserialize(Json::encode($offer))
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Role/ReadModel/Labels/LabelRolesProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Role\ReadModel\Labels;

use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Label\Events\Created as LabelCreated;
use CultuurNet\UDB3\Label\ReadModels\JSON\Repository\Entity;
use CultuurNet\UDB3\Model\ValueObject\Identity\UUID;
Expand Down Expand Up @@ -71,7 +72,7 @@ private function createNewDocument(UUID $uuid)
{
$document = new JsonDocument(
$uuid->toString(),
json_encode([])
Json::encode([])
);
return $document;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Role/ReadModel/Labels/RoleLabelsProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Role\ReadModel\Labels;

use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Label\Events\LabelDetailsProjectedToJSONLD;
use CultuurNet\UDB3\Label\ReadModels\JSON\Repository\Entity;
use CultuurNet\UDB3\Label\ReadModels\JSON\Repository\ReadRepositoryInterface;
Expand Down Expand Up @@ -136,7 +137,7 @@ private function createNewDocument(UUID $uuid)
{
$document = new JsonDocument(
$uuid->toString(),
json_encode([])
Json::encode([])
);
return $document;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Role/ReadModel/Users/RoleUsersProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Role\ReadModel\Users;

use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Model\ValueObject\Identity\UUID;
use CultuurNet\UDB3\ReadModel\DocumentDoesNotExist;
use CultuurNet\UDB3\ReadModel\DocumentRepository;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function applyRoleCreated(RoleCreated $roleCreated): void
$this->repository->save(
new JsonDocument(
$roleCreated->getUuid()->toString(),
json_encode([])
Json::encode([])
)
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Role/ReadModel/Users/UserRolesProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CultuurNet\UDB3\Role\ReadModel\Users;

use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\ReadModel\DocumentDoesNotExist;
use CultuurNet\UDB3\ReadModel\DocumentRepository;
use CultuurNet\UDB3\ReadModel\JsonDocument;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function applyUserAdded(UserAdded $userAdded): void
} catch (DocumentDoesNotExist $e) {
$document = new JsonDocument(
$userId,
json_encode([])
Json::encode([])
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/User/ManagementToken/CacheRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\User\ManagementToken;

use CultuurNet\UDB3\Json;
use DateTimeImmutable;
use Doctrine\Common\Cache\Cache;

Expand Down Expand Up @@ -35,13 +36,12 @@ public function token(): ?ManagementToken

public function store(ManagementToken $token): void
{
$tokenAsJson = json_encode(
$tokenAsJson = Json::encode(
[
'token' => $token->getToken(),
'issuedAt' => $token->getIssuedAt()->format(DATE_ATOM),
'expiresIn' => $token->getExpiresIn(),
],
JSON_THROW_ON_ERROR
]
);

$this->cache->save(self::TOKEN_KEY, $tokenAsJson);
Expand Down
17 changes: 9 additions & 8 deletions tests/Contributor/ContributorEnrichedRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Contributor;

use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Model\ValueObject\Identity\UUID;
use CultuurNet\UDB3\Model\ValueObject\Web\EmailAddress;
use CultuurNet\UDB3\Model\ValueObject\Web\EmailAddresses;
Expand Down Expand Up @@ -80,15 +81,15 @@ public function it_add_contributors_if_user_has_permission(string $itemType): vo
);


$jsonLd = new JsonDocument($this->offerId, json_encode(['@type' => $itemType]));
$jsonLd = new JsonDocument($this->offerId, Json::encode(['@type' => $itemType]));
$this->documentRepository->save($jsonLd);

$fetchJsonLd = $this->contributorEnrichedRepository->fetch($this->offerId, false);

$this->assertEquals(
new JsonDocument(
$this->offerId,
json_encode([
Json::encode([
'@type' => $itemType,
'contributors' => [
'[email protected]',
Expand Down Expand Up @@ -123,15 +124,15 @@ public function it_does_not_add_contributors_if_there_are_none(string $itemType)
);


$jsonLd = new JsonDocument($this->offerId, json_encode(['@type' => $itemType]));
$jsonLd = new JsonDocument($this->offerId, Json::encode(['@type' => $itemType]));
$this->documentRepository->save($jsonLd);

$fetchJsonLd = $this->contributorEnrichedRepository->fetch($this->offerId, false);

$this->assertEquals(
new JsonDocument(
$this->offerId,
json_encode([
Json::encode([
'@type' => $itemType,
])
),
Expand Down Expand Up @@ -159,15 +160,15 @@ public function it_hides_contributors_if_user_has_no_permission(string $itemType
->with(new UUID($this->offerId));


$jsonLd = new JsonDocument($this->offerId, json_encode(['@type' => $itemType]));
$jsonLd = new JsonDocument($this->offerId, Json::encode(['@type' => $itemType]));
$this->documentRepository->save($jsonLd);

$fetchJsonLd = $this->contributorEnrichedRepository->fetch($this->offerId);

$this->assertEquals(
new JsonDocument(
$this->offerId,
json_encode([
Json::encode([
'@type' => $itemType,
])
),
Expand All @@ -184,7 +185,7 @@ public function it_does_not_save_contributors(string $itemType): void
$this->contributorEnrichedRepository->save(
new JsonDocument(
$this->offerId,
json_encode([
Json::encode([
'@type' => $itemType,
'contributors' => [
'[email protected]',
Expand All @@ -197,7 +198,7 @@ public function it_does_not_save_contributors(string $itemType): void
$this->assertEquals(
new JsonDocument(
$this->offerId,
json_encode(['@type' => $itemType])
Json::encode(['@type' => $itemType])
),
$this->contributorEnrichedRepository->fetch($this->offerId)
);
Expand Down
6 changes: 3 additions & 3 deletions tests/Event/GeoCoordinatesCommandHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use CultuurNet\UDB3\Geocoding\Coordinate\Latitude;
use CultuurNet\UDB3\Geocoding\Coordinate\Longitude;
use CultuurNet\UDB3\Geocoding\GeocodingService;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\Language;
use CultuurNet\UDB3\Model\ValueObject\Geography\CountryCode;
use CultuurNet\UDB3\ReadModel\DocumentRepository;
Expand Down Expand Up @@ -60,13 +61,12 @@ protected function createCommandHandler(EventStore $eventStore, EventBus $eventB
$documentRepository->expects($this->once())
->method('fetch')
->with(self::EVENT_ID)
->willReturn(new JsonDocument(self::EVENT_ID, json_encode([
->willReturn(new JsonDocument(self::EVENT_ID, Json::encode([
'name' => [
'nl' => 'Faith no More',
'fr' => 'Faith no More - a la francais',

],
], JSON_THROW_ON_ERROR)));
])));

return new GeoCoordinatesCommandHandler(
$eventRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CultuurNet\UDB3\EntityNotFoundException;
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
use CultuurNet\UDB3\Json;
use CultuurNet\UDB3\ReadModel\DocumentRepository;
use CultuurNet\UDB3\ReadModel\InMemoryDocumentRepository;
use CultuurNet\UDB3\ReadModel\JsonDocument;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function it_returns_a_null_production_when_the_event_does_not_belong_to_a
$eventId = Uuid::uuid4()->toString();
$originalJsonDocument = new JsonDocument(
$eventId,
json_encode((object) ['@id' => $eventId])
Json::encode((object) ['@id' => $eventId])
);


Expand All @@ -78,7 +79,7 @@ public function it_returns_production_data_when_event_belongs_to_a_production():
$eventId = Uuid::uuid4()->toString();
$originalJsonDocument = new JsonDocument(
$eventId,
json_encode((object) ['@id' => $eventId])
Json::encode((object) ['@id' => $eventId])
);

$otherEventId = Uuid::uuid4()->toString();
Expand Down Expand Up @@ -122,7 +123,7 @@ public function it_does_not_save_a_production_in_json_document(): void
$newProductionEnrichedEventRepository->save(
new JsonDocument(
$eventId,
json_encode([
Json::encode([
'@type' => 'Event',
'production' => [
'id' => Uuid::uuid4()->toString(),
Expand All @@ -140,7 +141,7 @@ public function it_does_not_save_a_production_in_json_document(): void
$this->assertEquals(
new JsonDocument(
$eventId,
json_encode(['@type' => 'Event'])
Json::encode(['@type' => 'Event'])
),
$inMemoryDocumentRepository->fetch($eventId)
);
Expand Down
Loading

0 comments on commit f031b82

Please sign in to comment.