Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Pattern Matcher

Pattern Matcher Laravel Package

alexeyshockov/pattern-matcher

Lightweight PHP pattern-matching utility by Alexey Shockov. Helps compare values against defined patterns and execute matching logic, enabling cleaner conditional flows than nested if/switch statements. Suitable for small libraries and framework-agnostic use.

View on GitHub
Deep Wiki
Context7

Pattern matching for PHP.

Frequently asked questions about Pattern Matcher
How can I use this package to simplify Laravel route handling or request validation?
This package replaces verbose `if-else` or `switch` blocks in Laravel controllers or middleware. For example, match request payloads against patterns like `match($request->input())->with(['version' => 'v2'])->routeTo('api/v2')` to dynamically dispatch logic. It’s especially useful for API versioning, request parsing, or complex validation rules where nested conditions clutter your code.
Does this work with Laravel’s service container or facades?
Yes, the package is framework-agnostic but integrates seamlessly with Laravel. You can bind it to the service container like any other Composer package (`$app->bind('matcher', fn() => new Matcher())`) and use it in controllers, services, or even facades. It has no Laravel-specific dependencies, so it won’t conflict with existing code.
What Laravel versions does this package support?
The package is pure PHP and requires PHP 8.0+ (for named arguments and match expressions), but it has no Laravel-specific dependencies. Test it in your Laravel 8.x, 9.x, or 10.x projects—it will work as long as your PHP version meets the minimum requirement. No framework-specific features are leveraged.
Can I use this for event dispatching in Laravel?
Absolutely. Pattern matching shines in event handling. For example, match an event payload like `match($event)->with(['type' => 'payment'])->dispatch(new ProcessPayment())` to conditionally dispatch listeners. This avoids deep nesting in event service providers and keeps your logic clean and reusable.
How does this compare to Laravel’s built-in `match()` expression (PHP 8.1+)?
This package offers more than PHP’s native `match()` by providing a fluent, composable API for complex patterns (e.g., nested guards, multiple conditions). While PHP 8.1’s `match()` is simpler for basic cases, this library excels for reusable, domain-specific matching logic—like a more powerful alternative to `switch` or `if` chains in Laravel.
Will this impact performance in production?
Pattern matching adds minimal overhead, but for critical paths, benchmark against native `if`/`switch` statements. The package is optimized for readability, not micro-optimizations. Use it where cleaner code justifies the trade-off, such as in controllers or services, rather than high-frequency loops or database queries.
Can I test this package in Laravel’s testing framework?
Yes, the package is unit-testable in isolation using PHPUnit. Mock dependencies or test patterns directly with assertions like `assertTrue($matcher->match($value)->with(['key' => 'value'])->isValid())`. For Laravel-specific tests, use the package in feature tests just like any other service or helper.
Are there alternatives like this for Laravel?
Few packages focus on functional pattern matching for Laravel. Spatie’s `match` is lightweight but lacks composability. This package stands out for its fluent API and Laravel-friendly use cases (e.g., routing, validation). If you’re using PHP 8.1+, you could also explore native `match()` expressions, but they lack the reusable, domain-specific design this package offers.
How do I define custom patterns or extend the matcher?
The package uses a simple API: define patterns with `match($value)->with([conditions])->do($callback)`. Extend it by creating custom matcher classes or macros. For example, add a `startsWith()` guard by extending the base `Matcher` class or using Laravel’s macro system to attach new methods globally.
Should I use this for database queries or Eloquent models?
Avoid using this for raw database queries—Eloquent’s query builder or raw SQL already optimize performance for such cases. Instead, use the package for application logic, like filtering collections after retrieval or matching model attributes in services. For example, match a user’s role to determine access logic before querying.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor