Files
KanbanGit/app/Http/Requests/StoreProjectTicketRequest.php
T
Nico Schröder 9895fc36db
CI/CD / test_and_check_app (push) Failing after 3m8s
CI/CD / Publish immutable image (push) Skipped
CI/CD / test_and_check_app (pull_request) Failing after 3m8s
CI/CD / Publish immutable image (pull_request) Skipped
fix: make project template inheritance granular and safe
2026-09-08 10:25:52 +00:00

123 lines
4.0 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Actions\Tickets\Support\AcceptanceCriteriaChecklistMapper;
use App\Models\Project;
use App\Models\Ticket;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class StoreProjectTicketRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return $this->user() !== null;
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$acceptanceCriteria = $this->input('acceptance_criteria');
if ($acceptanceCriteria !== null && ! is_array($acceptanceCriteria)) {
return;
}
$this->merge([
'acceptance_criteria' => AcceptanceCriteriaChecklistMapper::normalize(
$acceptanceCriteria,
),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
/** @var Project $project */
$project = $this->route('project');
return [
'acceptance_criteria' => ['nullable', 'array'],
'acceptance_criteria.*' => ['array'],
'acceptance_criteria.*.id' => ['nullable', 'integer'],
'acceptance_criteria.*.text' => ['required', 'string'],
'acceptance_criteria.*.checked' => ['required', 'boolean'],
'acceptance_criteria.*.indent' => ['required', 'integer', 'min:0', 'max:'.AcceptanceCriteriaChecklistMapper::MAX_INDENT_LEVEL],
'assignee_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
'description' => ['nullable', 'string'],
'parent_ticket_id' => [
'nullable',
'integer',
Rule::exists('tickets', 'id')->where('project_id', $project->id),
],
'ticket_priority_id' => [
'required',
'integer',
Rule::exists('ticket_priorities', 'id')->where(
fn ($query) => $query->where('project_id', $project->id),
),
],
'ticket_status_id' => [
'required',
'integer',
Rule::exists('ticket_statuses', 'id')->where(
fn ($query) => $query->where('project_id', $project->id),
),
],
'ticket_type_id' => [
'required',
'integer',
Rule::exists('ticket_types', 'id')->where(
fn ($query) => $query->where('project_id', $project->id),
),
],
'story_points' => ['nullable', 'integer', 'min:0'],
'title' => ['required', 'string', 'max:255'],
];
}
/**
* @return array<int, callable(Validator): void>
*/
public function after(): array
{
return [
function (Validator $validator): void {
/** @var array<string, mixed> $validated */
$validated = $this->safe()->all();
/** @var Project $project */
$project = $this->route('project');
$parentTicketId = Arr::get($validated, 'parent_ticket_id');
if ($parentTicketId === null) {
return;
}
$parentTicket = Ticket::query()
->where('project_id', $project->id)
->with('ticketType')
->find($parentTicketId);
if ($parentTicket !== null && ! $parentTicket->ticketType?->is_parent) {
$validator->errors()->add('parent_ticket_id', 'Only tickets with a parent-enabled type can be selected as a parent.');
}
},
];
}
}