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 support for explicit binding of pattern parameters #694

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Lukasss93
Copy link
Member

@Lukasss93 Lukasss93 commented Apr 18, 2024

This PR adds support for explicit binding of pattern parameters.
This is useful when you want to customize the resolution logic of a pattern parameter.

If you wish to define your own resolution logic for a pattern parameter,
you may use the bindParameter method of the Nutgram class.
The closure you pass to the bindParameter method will receive the value of the parameter and
should return the resolved value that should be injected into the handler.

Before this PR:

$bot = new Nutgram('your-token');

$bot->onCommand('ban {user}', function(Nutgram $bot, string $value){
   $user = User::find($value);
   $user->ban();
});

$bot->run();

After this PR:

$bot = new Nutgram('your-token');

$bot->bindParameter('user', function(string $value){
    return User::find($value);
});

$bot->onCommand('ban {user}', function(Nutgram $bot, ?User $user){
    $user?->ban();
});

$bot->run();

Inspired by https://laravel.com/docs/11.x/routing#customizing-the-resolution-logic

@Lukasss93 Lukasss93 added enhancement New feature or request poc Idea that could easily be discarded labels Apr 18, 2024
@Lukasss93 Lukasss93 self-assigned this Apr 18, 2024
Copy link

codeclimate bot commented Apr 18, 2024

Code Climate has analyzed commit 292ac46 and detected 1 issue on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 1

The test coverage on the diff in this pull request is 100.0% (60% is the threshold).

This pull request will bring the total coverage in the repository to 96.7%.

View more on Code Climate.

@sergix44 sergix44 marked this pull request as draft April 27, 2024 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request poc Idea that could easily be discarded
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant