2025-08-19 10:14:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shared validation patterns for consistent use across the application
|
|
|
|
|
*/
|
|
|
|
|
class ValidationPatterns
|
|
|
|
|
{
|
|
|
|
|
/**
|
2026-01-05 12:14:27 +00:00
|
|
|
* Pattern for names excluding all dangerous characters
|
|
|
|
|
*/
|
2026-01-19 17:50:56 +00:00
|
|
|
public const NAME_PATTERN = '/^[\p{L}\p{M}\p{N}\s\-_.@\/&]+$/u';
|
2025-08-19 10:14:48 +00:00
|
|
|
|
|
|
|
|
/**
|
2026-01-05 12:14:27 +00:00
|
|
|
* Pattern for descriptions excluding all dangerous characters with some additional allowed characters
|
2025-08-19 10:14:48 +00:00
|
|
|
*/
|
2026-01-19 17:50:56 +00:00
|
|
|
public const DESCRIPTION_PATTERN = '/^[\p{L}\p{M}\p{N}\s\-_.,!?()\'\"+=*@\/&]+$/u';
|
2025-08-19 10:14:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get validation rules for name fields
|
|
|
|
|
*/
|
|
|
|
|
public static function nameRules(bool $required = true, int $minLength = 3, int $maxLength = 255): array
|
|
|
|
|
{
|
|
|
|
|
$rules = [];
|
|
|
|
|
|
|
|
|
|
if ($required) {
|
|
|
|
|
$rules[] = 'required';
|
|
|
|
|
} else {
|
|
|
|
|
$rules[] = 'nullable';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rules[] = 'string';
|
|
|
|
|
$rules[] = "min:$minLength";
|
|
|
|
|
$rules[] = "max:$maxLength";
|
|
|
|
|
$rules[] = 'regex:'.self::NAME_PATTERN;
|
|
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get validation rules for description fields
|
|
|
|
|
*/
|
|
|
|
|
public static function descriptionRules(bool $required = false, int $maxLength = 255): array
|
|
|
|
|
{
|
|
|
|
|
$rules = [];
|
|
|
|
|
|
|
|
|
|
if ($required) {
|
|
|
|
|
$rules[] = 'required';
|
|
|
|
|
} else {
|
|
|
|
|
$rules[] = 'nullable';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rules[] = 'string';
|
|
|
|
|
$rules[] = "max:$maxLength";
|
|
|
|
|
$rules[] = 'regex:'.self::DESCRIPTION_PATTERN;
|
|
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get validation messages for name fields
|
|
|
|
|
*/
|
|
|
|
|
public static function nameMessages(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2026-01-19 17:50:56 +00:00
|
|
|
'name.regex' => "The name may only contain letters (including Unicode), numbers, spaces, and these characters: - _ . / @ &",
|
2025-08-19 10:14:48 +00:00
|
|
|
'name.min' => 'The name must be at least :min characters.',
|
|
|
|
|
'name.max' => 'The name may not be greater than :max characters.',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get validation messages for description fields
|
|
|
|
|
*/
|
|
|
|
|
public static function descriptionMessages(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2026-01-19 17:50:56 +00:00
|
|
|
'description.regex' => "The description may only contain letters (including Unicode), numbers, spaces, and common punctuation: - _ . , ! ? ( ) ' \" + = * / @ &",
|
2025-08-19 10:14:48 +00:00
|
|
|
'description.max' => 'The description may not be greater than :max characters.',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-05 12:14:27 +00:00
|
|
|
/**
|
2025-08-19 10:14:48 +00:00
|
|
|
* Get combined validation messages for both name and description fields
|
|
|
|
|
*/
|
|
|
|
|
public static function combinedMessages(): array
|
|
|
|
|
{
|
|
|
|
|
return array_merge(self::nameMessages(), self::descriptionMessages());
|
|
|
|
|
}
|
|
|
|
|
}
|