ilario-pierbattista/reverse-regex
Generate example strings from regular expressions in PHP—useful for test data for forms, databases, and regex validation. Includes lexer/parser and random generators, supports literals, groups, classes, ranges, and quantifiers (with some Unicode/PCRE limits).
FormRequest or API validation rules (e.g., regex:/^[A-Z]{2}\d{4}$/ for Australian postcodes).CHECK constraints or unique regex patterns (e.g., SSNs, UUIDs).extends Action or extends Rule classes with complex regex logic.Pest::fake() or Pest::mock() to generate dynamic test data within test cases.Illuminate\Database\Eloquent\Factories\Factory to inject regex-generated attributes (e.g., ->state(fn () => ReverseRegex::generate('^[A-Z]{2}\d{4}$'))).test('POST /api/users')->assertValid() scenarios.\X{####} Unicode support), critical for:
Lexer + Parser + Generator) avoids Laravel-specific dependencies, making it easy to integrate:
ReverseRegexServiceProvider to bind the generator as a singleton or context-bound instance.RegexGenerator facade for fluent syntax (e.g., RegexGenerator::generate('regex')->times(10)).reverse-regex.php config file to Laravel’s testing directory for global test access."php": "^8.1") to block installation until upgrade is feasible.doctrine/lexer (already used in Laravel) and symfony/polyfill-mbstring (common in modern PHP stacks).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Unsupported Regex Features | Medium | Validate against your project’s regex patterns pre-integration. Fork if critical features (e.g., lookaheads) are missing. |
| PHPStan Strictness | Low | Adopt the package’s PHPStan baseline incrementally; exclude non-critical files initially. |
| Performance Overhead | Low | Benchmark generation speed for your use case (e.g., 10K postcodes). Cache generated data if needed. |
| Unicode Edge Cases | Medium | Test with \X{####} ranges and hex values in your locale-specific test suites. |
| Quantifier Limits | Low | Monitor for PHP_INT_MAX warnings with unbounded quantifiers (e.g., .*). Use {n,m} ranges instead. |
| Thread Safety | Low | Stateless design makes it safe for parallel test execution (e.g., Pest’s --parallel). |
\p{L}, lookarounds) that would require a fork?"TestCase trait) or per-test-case utility?"RegexGenerator facade?"// app/Providers/AppServiceProvider.php
public function register(): void
{
$this->app->singleton(RegexGenerator::class, fn () => new RegexGenerator());
}
// app/Facades/RegexGenerator.php
public static function generate(string $regex, int $seed = null): string
{
return app(RegexGenerator::class)->generate($regex, $seed);
}
Pest trait or helper:
// tests/TestHelpers/RegexTestData.php
function generateRegexData(string $regex, int $count = 1): array
{
return array_map(fn () => RegexGenerator::generate($regex), range(1, $count));
}
// database/factories/UserFactory.php
public function definition(): array
{
return [
'postcode' => fn () => RegexGenerator::generate('^[A-Z]{2}\d{4}$'),
];
}
test('valid postcode', function () {
$postcode = RegexGenerator::generate('^[A-Z]{2}\d{4}$');
$response = $this->post('/api/users', ['postcode' => $postcode]);
$response->assertValid();
});
test('invalid postcode format', function () {
$response = $this->post('/api/users', ['postcode' => '123']);
$response->assertInvalid(['postcode' => 'The postcode format is invalid.']);
});
composer.json to enforce "php": "^8.1".composer require ilario-pierbattista/reverse-regex
RegexTestData trait).{
"scripts": {
"cs-check": "php-cs-fixer fix --dry-run --diff",
"phpstan": "phpstan analyse --level=5"
}
}
cs-check and phpstan on PRs.| Component | Compatibility |
How can I help you explore Laravel packages today?