Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,9 @@
<argument type="service" id="meilisearch.client" />
<tag name="console.command" />
</service>

<service id="Meilisearch\Bundle\Services\UnixTimestampNormalizer">
<tag name="serializer.normalizer" />
</service>
</services>
</container>
1 change: 1 addition & 0 deletions src/SearchableEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function getIndexUid(): string
public function getSearchableArray(): array
{
$context = [
'meilisearch' => true,
'fieldsMapping' => $this->entityMetadata->fieldMappings,
];

Expand Down
33 changes: 33 additions & 0 deletions src/Services/UnixTimestampNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Bundle\Services;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class UnixTimestampNormalizer implements NormalizerInterface
{
/**
* @param \DateTimeInterface $object
*/
public function normalize($object, string $format = null, array $context = []): int
{
return $object->getTimestamp();
}

/**
* @param mixed $data
*/
public function supportsNormalization($data, string $format = null, array $context = []): bool
{
return $data instanceof \DateTimeInterface && true === ($context['meilisearch'] ?? null);
}

public function getSupportedTypes(?string $format): array
{
return [
\DateTimeInterface::class => true, // @codeCoverageIgnore
];
}
}
4 changes: 2 additions & 2 deletions tests/Integration/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,13 @@ public function testImportsDummyWithCustomGroups(): void
'objectID' => 1,
'id' => 1,
'name' => 'Dummy 1',
'createdAt' => '2024-04-04T07:32:01+00:00',
'createdAt' => 1712215921,
],
[
'objectID' => 2,
'id' => 2,
'name' => 'Dummy 2',
'createdAt' => '2024-04-04T07:32:02+00:00',
'createdAt' => 1712215922,
],
], $this->client->index('sf_phpunit__dummy_custom_groups')->getDocuments()->getResults());
}
Expand Down
8 changes: 2 additions & 6 deletions tests/Unit/SerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class SerializationTest extends KernelTestCase
public function testSimpleEntityToSearchableArray(): void
{
$datetime = new \DateTime();
$dateNormalizer = static::getContainer()->get('serializer.normalizer.datetime');
// This way we can test that DateTime's are serialized with DateTimeNormalizer
// And not the default ObjectNormalizer
$serializedDateTime = $dateNormalizer->normalize($datetime, Searchable::NORMALIZATION_FORMAT);

$post = new Post(
[
Expand Down Expand Up @@ -51,12 +47,12 @@ public function testSimpleEntityToSearchableArray(): void
'id' => 12,
'title' => 'a simple post',
'content' => 'some text',
'publishedAt' => $serializedDateTime,
'publishedAt' => $datetime->getTimestamp(),
'comments' => [
[
'id' => null,
'content' => 'a great comment',
'publishedAt' => $serializedDateTime,
'publishedAt' => $datetime->getTimestamp(),
],
],
];
Expand Down