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

Add collection macros for encoding and decoding #17

Open
Alig96 opened this issue May 21, 2019 · 1 comment
Open

Add collection macros for encoding and decoding #17

Alig96 opened this issue May 21, 2019 · 1 comment

Comments

@Alig96
Copy link

Alig96 commented May 21, 2019

I'm working with multiple arrays of encoded/decoded ids and found it helpful to register 2 macros for encoding and decoding the whole collection. Thought I would share to either be implemented or someone can use.

Register within a service provider:

        // Optimus decode
        Collection::macro('decode', function ($connection) {
            return $this->map(function ($value) use ($connection) {
                return $connection->decode($value);
            });
        });

        // Optimus encode
        Collection::macro('encode', function ($connection) {
            return $this->map(function ($value) use ($connection) {
                return $connection->encode($value);
            });
        });

Use by passing a optimus connection and the collection will return an encoded/decoded version;

$collection->encode(Optimus::connection('Main'));
$collection->decode(Optimus::connection('Main'));
@antonkomarev
Copy link
Member

Thanks for sharing it!

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