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
Php Matcher

Php Matcher Laravel Package

coduo/php-matcher

Flexible PHP pattern-matching library for testing and validating complex data structures. Compare arrays, JSON and objects against readable expectations, with rich mismatch descriptions to pinpoint differences. Useful for API response assertions and custom validation rules.

View on GitHub
Deep Wiki
Context7

The easiest way to match data structures like JSON/PlainText/XML against readable patterns. Sandbox:

Frequently asked questions about Php Matcher
How do I integrate coduo/php-matcher into a Laravel project for API request validation?
Register the matcher as a service provider in `config/app.php` and bind it to the Laravel container. Use middleware to validate incoming requests by comparing `$request->all()` against a pattern before processing. For example, inject the matcher into a `ValidateRequestPattern` middleware and call `$matcher->match($data, $pattern)` in the `handle` method.
Can I use this package to validate Eloquent model attributes or database query results?
Yes, the matcher works with arrays, objects, and JSON, so you can validate Eloquent model attributes by passing `$model->toArray()` or transform database results (e.g., `DB::select()`) into a structured format before matching. For collections, loop through each item or use `collect($results)->every(fn($item) => $matcher->match($item, $pattern))`.
What Laravel versions and PHP versions does coduo/php-matcher support?
The package supports PHP 8.1+ and is optimized for Laravel 9+. It has no breaking changes in recent releases, so it works with Laravel 10 as well. Always check the package’s `composer.json` for the latest version constraints before installing.
How does the performance compare to native PHP array checks or regex for large datasets?
For most use cases, the matcher is optimized for readability and maintainability, not raw speed. Benchmark against native PHP or PCRE in performance-critical paths (e.g., real-time systems). Pre-compile patterns or cache them if you’re processing high volumes of data repeatedly.
Can I use this package to transform or extract data from complex JSON/XML structures?
Yes, the matcher is designed for both validation and transformation. Use patterns to define expected structures, and the library will highlight mismatches or extract nested values. For example, you can define a pattern like `['user.id' => 'integer', 'user.name' => 'string']` to validate and extract specific fields from JSON.
How do I handle mismatches or errors in production? Does it integrate with Laravel’s validation system?
The matcher provides rich mismatch descriptions to pinpoint differences. You can throw custom exceptions or integrate with Laravel’s `ValidationException` by wrapping the matcher’s output in a `Validator` facade. For logging, use Laravel’s logging system to record mismatches during validation.
Is there a way to use coduo/php-matcher in Laravel Artisan commands or queues?
Absolutely. Register the matcher as a service provider and inject it into Artisan commands or queue jobs. For example, in a command, resolve the matcher via the container (`$matcher = app(Matcher::class)`) and use it to validate input data before processing. Similarly, validate job payloads in `handle()` methods.
What are the alternatives to coduo/php-matcher for Laravel data validation?
Alternatives include Laravel’s built-in `Validator` facade for simple rules, `spatie/array-to-object` for array-to-object conversion, and `json-schema` for strict JSON validation. However, `coduo/php-matcher` stands out for its readable pattern syntax and rich mismatch feedback, making it ideal for complex nested structures.
How do I write tests for patterns in Laravel using this package?
Use PHPUnit assertions to test patterns. For example, assert that a response matches a pattern with `assertTrue($matcher->match($response, $expectedPattern))`. Store patterns as fixtures in your tests or dynamically generate them. You can also extend PHPUnit with custom assertions for matcher-specific logic.
Can I use coduo/php-matcher in non-Laravel PHP projects like Lumen or Symfony?
Yes, the package is standalone and works in any PHP 8.1+ project. In Lumen or Symfony, instantiate the matcher directly (`new Matcher()`) or register it as a service in your DI container. It doesn’t rely on Laravel-specific features, so integration is straightforward.
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.
terminal42/code-quality-tools
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