apie/dateformat-to-regex
Converts PHP date() format strings into “simple” regular expressions for validating date/time strings. Generates regex that matches the format pattern (not full calendar validation, e.g., may accept 30 February). Includes a static DateFormatToRegex::formatToRegex() helper.
DateTime::ATOM) and machine-verifiable regex patterns, reducing reliance on DateTime parsing (which can be error-prone for malformed input).Validator::extend()) can leverage this package to create custom regex-based date validators without reinventing the wheel. Example:
Validator::extend('date_format', function ($attribute, $value, $parameters, $validator) {
$regex = DateFormatToRegex::formatToRegex($parameters[0]);
return preg_match($regex, $value) === 1;
});
DateTime::createFromFormat() fallback).DateFormatToRegex::formatToRegex()) with no Laravel-specific dependencies, making it easy to integrate into existing validation logic.apie/core (self-versioned) could introduce hidden dependencies or versioning conflicts. Audit apie/core for Laravel compatibility (e.g., no PSR-15 middleware assumptions).d/m/Y vs. Y-m-d).spatie/laravel-date-format-regex) to add tests/docs.date_format validator or libraries like symfony/mime (for RFC-compliant dates) may suffice for simpler use cases.date/date_format validators, or supplement them (e.g., for custom formats like DD-MMM-YYYY)?Validator::fail() or return false silently?apie/core updates or PHP 8.3+ requirements in a legacy Laravel app?FormRequest classes or Validator::extend().DateFormatRegexRule.Y-m-d H:i:s, d/m/Y).Y-m-d, does it match ^\d{4}-\d{2}-\d{2}$?).class DateFormatRegexValidator {
public static function validate(string $value, string $format): bool {
$regex = DateFormatToRegex::formatToRegex($format);
return preg_match($regex, $value) === 1;
}
}
date_format validators incrementally, monitoring false positives/negatives.rector/rector to upgrade syntax.apie/core is a minor risk—check for Laravel-specific assumptions (e.g., service container bindings).app/Validators/CustomDateValidator.php).date_format validators in FormRequest classes.Illuminate\Support\Facades\Cache) for performance.apie/core for breaking changes. Consider vendor patching or forking if the package stagnates.\Log::debug('Failed date validation', [
'value' => $request->input('date'),
'format' => $format,
'regex' => DateFormatToRegex::formatToRegex($format),
]);
DateTime::createFromFormat() if regex fails).Y-m-d):
$regex = Cache::remember("date_regex_{$format}", now()->addHours(1), fn() =>
DateFormatToRegex::formatToRegex($format)
);
WHERE clauses (e.g., REGEXP in MySQL), ensure the database can handle regex operations efficiently.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Regex allows invalid dates (e.g., "30 Feb") | Data corruption or business logic errors | Add DateTime::createFromFormat() fallback. |
| PHP 8.3+ requirement blocks upgrade | Deployment delays | Use Rector or implement a polyfill. |
apie/core updates break compatibility |
Integration failures | Fork the package or pin versions strictly. |
| Regex too permissive (e.g., matches non-date strings) | False positives in validation | Combine with additional checks (e.g., strtotime). |
| High regex compilation overhead | Increased latency | Cache compiled patterns. |
Y-m-d → ^\d{4}-\d{2}-\d{2}$).date_format or Carbon.How can I help you explore Laravel packages today?