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

[3.0] Normalizers usage #139

Closed
tristanbes opened this issue Nov 29, 2017 · 5 comments
Closed

[3.0] Normalizers usage #139

tristanbes opened this issue Nov 29, 2017 · 5 comments

Comments

@tristanbes
Copy link
Contributor

tristanbes commented Nov 29, 2017

Hello,

I seen that 3.x uses a new concept to serialize data based on the Symfony serializer which is a great move IMHO.

If I already have a set of normalizer for my objects, BUT i don't want them to be used for Algolia (and use the one I provide instead), is there a way to do this ?

Can I use the normalizers key to provide only "whitelisted" normalizers ?

    - name: comments
      class: App\Entity\Comment
      normalizers:
        - App\Normalizers\CommentNormalizer
        - Symfony\Component\Serializer\Normalizer\CustomNormalizer
        - Algolia\SearchBundle\Normalizer\SearchableArrayNormalizer

Also, in that case I will have 2 normalizer for 1 object Foo

How can I ensure that my previous code still uses the Normalizer I wrote and not uses the one I wrote for Algolia service ?

@tristanbes tristanbes changed the title Normalizers Normalizers usage Nov 29, 2017
@tristanbes
Copy link
Contributor Author

tristanbes commented Nov 29, 2017

OR,

If I decide to go for 1 normalizer, how do I contextualize the information I want to give to Algolia ?

The use case is depending who is consuming/ the context of the serializer, give more or less data to Algolia than for the other contexts.

Stupid example:

class FooNormalizer implements NormalizerInterface, SerializerAwareInterface
{
    public function normalize($object, $format = null, array $context = [])
    {
        if ($isForAlgolia) {
            return [
                'isRead' => $object->isRead($this->tokenStorage->getToken()->getUser()),
                'name'   => $object->getTitle(),
          ];
        }
        return [
            'id'        => $object->getId(),
            'status'    => $object->getStatus(),
        ];
    } 
}
    

@tristanbes
Copy link
Contributor Author

And also, how to mimic the IndexedIf() annotation ?
I mean if the normalizer returns an empty array, it won't get indexed, right ?

@tristanbes
Copy link
Contributor Author

tristanbes commented Nov 29, 2017

Also, i'm really interesting in testing the v3, but my account has a lot of objects and I've a paying account, so can you do something to not consume my credits/quotas that'll be used for my production please ? (appId: HO2U66MC0P) @julienbourdeau

@julienbourdeau
Copy link
Contributor

julienbourdeau commented Nov 30, 2017

@tristanbes Can you create a new free app in your Algolia dashboard and email me at [email protected]? I will add you some free quota ;) Thanks a lot for trying.

About the normalizers, the package will loop through the normalizers in order and use the first one where the method supportsNormalization() returns true.

The searchableEntity will call the normalize() method with the searchableArray format, so you could have something like:

class FooNormalizer implements NormalizerInterface, SerializerAwareInterface
{
    public function normalize($object, $format = null, array $context = [])
    {
        if ('searchableArray' == $format) {
            return [
                'isRead' => $object->isRead($this->tokenStorage->getToken()->getUser()),
                'name'   => $object->getTitle(),
          ];
        }

        return [
            'id'        => $object->getId(),
            'status'    => $object->getStatus(),
        ];
    } 
}

Please let me know how that worked for your and I'll add this in the doc.

@julienbourdeau julienbourdeau changed the title Normalizers usage [3.0] Normalizers usage Nov 30, 2017
@julienbourdeau
Copy link
Contributor

I believe I can close this one, please let me know if I missed something. :)

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

2 participants