- Can I use this package to replace preg_match in Laravel validation rules?
- Yes, this package is designed to work seamlessly with Laravel’s custom validation rules. Replace `preg_match` calls with the fluent API (e.g., `Regex::match()->pattern('/.../')->capture('group')`) to enforce type safety and structured results. The structured `MatchResult` objects integrate cleanly with Laravel’s validation error handling when combined with custom `Rule` classes.
- Does this work with Laravel’s built-in Validator or only custom rules?
- While it integrates directly with custom `Rule` classes, the package doesn’t natively extend Laravel’s `Validator` facade. You’ll need to wrap results in a `ValidationException` manually or create a custom validator extension. For most use cases, custom rules (e.g., `extends Rule`) are the recommended approach to leverage its type safety.
- What Laravel versions does this package support?
- The package targets PHP 8.1+, which aligns with Laravel 9+ and 10. For older Laravel versions (8.x), you may encounter compatibility issues due to PHP 8.0’s lack of attributes or union types. Always check the [PHP Standard Library docs](https://php-standard-library.dev) for version-specific notes before adopting.
- How do I handle errors if a regex pattern is invalid?
- Unlike `preg_last_error()`, this package throws exceptions (e.g., `InvalidPatternException`) for malformed regex. Catch these exceptions and convert them to Laravel’s `ValidationException` or log them via `Log::error()`. The structured errors make debugging easier in Laravel’s monolithic request lifecycle compared to `preg_*` ambiguity.
- Will this slow down my Laravel application?
- The package introduces minimal overhead for most use cases, but complex regex (e.g., recursive patterns) may still hit PHP’s PCRE limits. Benchmark critical paths (e.g., API validation) against raw `preg_*` using tools like PHPBench. For trivial cases, the `Regex::raw()` fallback ensures no performance regression.
- Can I use typed capture groups in Blade templates?
- Not directly—Blade templates aren’t designed for complex logic like typed regex. Use this package in service layers (e.g., `FormRequest` validation) or controllers, then pass structured results to Blade. For simple cases, stick with `preg_match` in Blade or refactor logic to a helper class using this library.
- How do I migrate existing preg_match calls to this package?
- Start with a pilot module (e.g., user input validation) to validate the wrapper’s effectiveness. Use search/replace for simple cases (e.g., `preg_match('/.../', $str)` → `Regex::match()->pattern('/.../')->test($str)`), then update tests to handle structured `MatchResult` objects. For legacy codebases, consider a phased migration.
- Does this package support async/queue jobs in Laravel?
- Yes, it’s ideal for background jobs like CSV/JSON parsing or report generation. The structured return types simplify unit testing for queued logic, and the fluent API reduces boilerplate in job handlers. Just ensure your job’s dependencies are properly injected (e.g., via constructor or `handle()` method).
- Are there alternatives for type-safe regex in Laravel?
- For Laravel-specific needs, consider `spatie/laravel-regex` (simpler but less type-safe) or `symfony/validator` (for constraint-based validation). This package stands out by combining PHP’s type system with regex, but it lacks Laravel-native features like Blade directives. Choose based on whether you prioritize type safety or ecosystem integration.
- How do I test regex logic using this package?
- The structured `MatchResult` objects simplify unit testing. Assert against properties like `MatchResult::hasMatch()` or `MatchResult::getCapture('group')` in PHPUnit. For validation rules, test both success and failure cases by catching exceptions. The package’s design reduces flakiness from `preg_*`’s silent failures.