Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open up Paginator::$documents for read #2692

Open
signor-pedro opened this issue Apr 19, 2024 · 0 comments
Open

Open up Paginator::$documents for read #2692

signor-pedro opened this issue Apr 19, 2024 · 0 comments

Comments

@signor-pedro
Copy link

signor-pedro commented Apr 19, 2024

Description
In Elasticsearch\Paginator, the results of the query stored in $this->documents are private readonly.

Since the property is readonly anyway, would you be willing to make it public readonly?

A lot of the time, Elasticsearch returns metadata such as aggregations, necessary for building feature-rich search experiences.

It is a total waste of time to implement a custom Elasticsearch provider. The native one is great and does a lot of heavy lifting. If the property was open for read, I could simply decorate the normalizer, see example.

Example

#[AsDecorator('api_platform.hydra.normalizer.collection')]
class ElasticsearchNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
    public function __construct(
        private NormalizerInterface $normalizer,
    ) {
    }

    public function normalize(mixed $object, string $format = null, array $context = []): array
    {
        if (!$object instanceof Elasticsearch\Paginator) {
            throw new \LogicException();
        }

        return array_merge(
            $this->normalizer->normalize($object, $format, $context), // produces a valid hydra:collection
            ['aggregations => $object->documents['aggregations']], // metadata returned by Elasticsearch read from the raw result
        ));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant