337 lines
12 KiB
PHP
337 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Resume;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
/**
|
|
* Store Resume Form Request
|
|
*
|
|
* Validates data for creating a new resume with comprehensive validation rules.
|
|
* Implements business logic validations and data sanitization.
|
|
*
|
|
* @author David Valera Melendez <david@valera-melendez.de>
|
|
* @since February 2025
|
|
*/
|
|
class StoreResumeRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Auth::check();
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'title' => [
|
|
'required',
|
|
'string',
|
|
'min:3',
|
|
'max:255',
|
|
'regex:/^[a-zA-Z0-9\s\-\_\.\,\!\?]+$/',
|
|
],
|
|
'description' => [
|
|
'nullable',
|
|
'string',
|
|
'max:1000',
|
|
],
|
|
'template' => [
|
|
'required',
|
|
'string',
|
|
'in:professional,modern,executive,minimal,technical',
|
|
],
|
|
'content' => [
|
|
'nullable',
|
|
'array',
|
|
],
|
|
'content.personal_info' => [
|
|
'nullable',
|
|
'array',
|
|
],
|
|
'content.personal_info.full_name' => [
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
],
|
|
'content.personal_info.title' => [
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
],
|
|
'content.personal_info.email' => [
|
|
'nullable',
|
|
'email',
|
|
'max:255',
|
|
],
|
|
'content.personal_info.phone' => [
|
|
'nullable',
|
|
'string',
|
|
'max:20',
|
|
],
|
|
'content.personal_info.location' => [
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
],
|
|
'content.personal_info.website' => [
|
|
'nullable',
|
|
'url',
|
|
'max:255',
|
|
],
|
|
'content.personal_info.linkedin' => [
|
|
'nullable',
|
|
'url',
|
|
'max:255',
|
|
],
|
|
'content.personal_info.github' => [
|
|
'nullable',
|
|
'url',
|
|
'max:255',
|
|
],
|
|
'content.summary' => [
|
|
'nullable',
|
|
'string',
|
|
'max:2000',
|
|
],
|
|
'content.experience' => [
|
|
'nullable',
|
|
'array',
|
|
'max:20',
|
|
],
|
|
'content.experience.*.company' => [
|
|
'required_with:content.experience',
|
|
'string',
|
|
'max:255',
|
|
],
|
|
'content.experience.*.position' => [
|
|
'required_with:content.experience',
|
|
'string',
|
|
'max:255',
|
|
],
|
|
'content.experience.*.start_date' => [
|
|
'required_with:content.experience',
|
|
'date',
|
|
'before_or_equal:today',
|
|
],
|
|
'content.experience.*.end_date' => [
|
|
'nullable',
|
|
'date',
|
|
'after:content.experience.*.start_date',
|
|
'before_or_equal:today',
|
|
],
|
|
'content.experience.*.current' => [
|
|
'boolean',
|
|
],
|
|
'content.experience.*.description' => [
|
|
'nullable',
|
|
'string',
|
|
'max:2000',
|
|
],
|
|
'content.education' => [
|
|
'nullable',
|
|
'array',
|
|
'max:10',
|
|
],
|
|
'content.education.*.institution' => [
|
|
'required_with:content.education',
|
|
'string',
|
|
'max:255',
|
|
],
|
|
'content.education.*.degree' => [
|
|
'required_with:content.education',
|
|
'string',
|
|
'max:255',
|
|
],
|
|
'content.education.*.field' => [
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
],
|
|
'content.education.*.start_date' => [
|
|
'required_with:content.education',
|
|
'date',
|
|
'before_or_equal:today',
|
|
],
|
|
'content.education.*.end_date' => [
|
|
'nullable',
|
|
'date',
|
|
'after:content.education.*.start_date',
|
|
'before_or_equal:today',
|
|
],
|
|
'content.skills' => [
|
|
'nullable',
|
|
'array',
|
|
],
|
|
'settings' => [
|
|
'nullable',
|
|
'array',
|
|
],
|
|
'settings.color_scheme' => [
|
|
'nullable',
|
|
'string',
|
|
'in:blue,green,red,purple,orange,gray,black',
|
|
],
|
|
'settings.font_family' => [
|
|
'nullable',
|
|
'string',
|
|
'in:Arial,Times New Roman,Calibri,Helvetica,Georgia',
|
|
],
|
|
'settings.font_size' => [
|
|
'nullable',
|
|
'integer',
|
|
'min:8',
|
|
'max:16',
|
|
],
|
|
'is_public' => [
|
|
'boolean',
|
|
],
|
|
'allow_comments' => [
|
|
'boolean',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom error messages for validation rules.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'title.required' => 'Resume title is required.',
|
|
'title.min' => 'Resume title must be at least 3 characters.',
|
|
'title.max' => 'Resume title cannot exceed 255 characters.',
|
|
'title.regex' => 'Resume title contains invalid characters.',
|
|
'description.max' => 'Description cannot exceed 1000 characters.',
|
|
'template.required' => 'Please select a resume template.',
|
|
'template.in' => 'Selected template is not available.',
|
|
'content.experience.max' => 'You cannot add more than 20 work experiences.',
|
|
'content.education.max' => 'You cannot add more than 10 education entries.',
|
|
'content.personal_info.email.email' => 'Please enter a valid email address.',
|
|
'content.personal_info.website.url' => 'Please enter a valid website URL.',
|
|
'content.personal_info.linkedin.url' => 'Please enter a valid LinkedIn URL.',
|
|
'content.personal_info.github.url' => 'Please enter a valid GitHub URL.',
|
|
'content.experience.*.company.required_with' => 'Company name is required for work experience.',
|
|
'content.experience.*.position.required_with' => 'Position title is required for work experience.',
|
|
'content.experience.*.start_date.required_with' => 'Start date is required for work experience.',
|
|
'content.experience.*.start_date.before_or_equal' => 'Start date cannot be in the future.',
|
|
'content.experience.*.end_date.after' => 'End date must be after start date.',
|
|
'content.experience.*.end_date.before_or_equal' => 'End date cannot be in the future.',
|
|
'content.education.*.institution.required_with' => 'Institution name is required for education.',
|
|
'content.education.*.degree.required_with' => 'Degree is required for education.',
|
|
'content.education.*.start_date.required_with' => 'Start date is required for education.',
|
|
'settings.font_size.min' => 'Font size must be at least 8.',
|
|
'settings.font_size.max' => 'Font size cannot exceed 16.',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'title' => 'resume title',
|
|
'description' => 'resume description',
|
|
'template' => 'resume template',
|
|
'content.personal_info.full_name' => 'full name',
|
|
'content.personal_info.title' => 'professional title',
|
|
'content.personal_info.email' => 'email address',
|
|
'content.personal_info.phone' => 'phone number',
|
|
'content.personal_info.location' => 'location',
|
|
'content.personal_info.website' => 'website',
|
|
'content.personal_info.linkedin' => 'LinkedIn profile',
|
|
'content.personal_info.github' => 'GitHub profile',
|
|
'content.summary' => 'professional summary',
|
|
'is_public' => 'public visibility',
|
|
'allow_comments' => 'allow comments',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'title' => trim($this->title),
|
|
'description' => $this->description ? trim($this->description) : null,
|
|
'is_public' => $this->boolean('is_public'),
|
|
'allow_comments' => $this->boolean('allow_comments'),
|
|
]);
|
|
|
|
if ($this->has('content.personal_info')) {
|
|
$personalInfo = $this->input('content.personal_info', []);
|
|
|
|
if (isset($personalInfo['email'])) {
|
|
$personalInfo['email'] = strtolower(trim($personalInfo['email']));
|
|
}
|
|
|
|
if (isset($personalInfo['full_name'])) {
|
|
$personalInfo['full_name'] = trim($personalInfo['full_name']);
|
|
}
|
|
|
|
$this->merge(['content' => array_merge($this->input('content', []), [
|
|
'personal_info' => $personalInfo
|
|
])]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure the validator instance.
|
|
*/
|
|
public function withValidator($validator): void
|
|
{
|
|
$validator->after(function ($validator) {
|
|
$userId = Auth::id();
|
|
$existingResume = \App\Models\Resume::where('user_id', $userId)
|
|
->where('title', $this->title)
|
|
->first();
|
|
|
|
if ($existingResume) {
|
|
$validator->errors()->add(
|
|
'title',
|
|
'You already have a resume with this title. Please choose a different title.'
|
|
);
|
|
}
|
|
|
|
if ($this->has('content.experience') && is_array($this->content['experience'])) {
|
|
foreach ($this->content['experience'] as $index => $experience) {
|
|
if (!empty($experience['start_date']) && !empty($experience['end_date'])) {
|
|
$startDate = Carbon::parse($experience['start_date']);
|
|
$endDate = Carbon::parse($experience['end_date']);
|
|
|
|
if ($startDate->greaterThan($endDate)) {
|
|
$validator->errors()->add(
|
|
"content.experience.{$index}.end_date",
|
|
'End date must be after start date.'
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Handle a failed validation attempt.
|
|
*/
|
|
protected function failedValidation($validator): void
|
|
{
|
|
logger()->warning('Resume creation validation failed', [
|
|
'user_id' => Auth::id(),
|
|
'title' => $this->input('title'),
|
|
'template' => $this->input('template'),
|
|
'errors' => $validator->errors()->toArray(),
|
|
]);
|
|
|
|
parent::failedValidation($validator);
|
|
}
|
|
}
|