refactor(requests): share category cashflow direction resolution (#479)

## What

`StoreCategoryRequest` and `UpdateCategoryRequest` had identical 24-line
`prepareForValidation()` deriving `cashflow_direction` from category
type.

Moved to `app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection`
trait.

## Stats

- **-48 / +43 lines** (net small, removes a whole duplicated logic block
that must stay in sync)

## Checks

- `php artisan test --filter=Categor` — 104 passed (565 assertions)
- `vendor/bin/pint --dirty` — pass

Part of duplication-removal series (#475–#478).
This commit is contained in:
Víctor Falcón 2026-06-03 17:39:40 +02:00 committed by GitHub
parent cc63a86a1c
commit fe692e37c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 50 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace App\Http\Requests\Concerns;
use App\Enums\CategoryCashflowDirection;
use App\Enums\CategoryType;
trait ResolvesCategoryCashflowDirection
{
/**
* Derive the cashflow direction from the category type before validation runs.
*/
protected function prepareForValidation(): void
{
$type = CategoryType::tryFrom((string) $this->input('type'));
if (in_array($type, [CategoryType::Savings, CategoryType::Investment], true)) {
$this->merge([
'cashflow_direction' => CategoryCashflowDirection::Outflow->value,
]);
return;
}
if ($type !== CategoryType::Transfer) {
$this->merge([
'cashflow_direction' => CategoryCashflowDirection::Hidden->value,
]);
return;
}
$this->merge([
'cashflow_direction' => $this->input('cashflow_direction', CategoryCashflowDirection::Hidden->value),
]);
}
}

View File

@ -5,12 +5,15 @@ namespace App\Http\Requests\Settings;
use App\Enums\CategoryCashflowDirection;
use App\Enums\CategoryColor;
use App\Enums\CategoryType;
use App\Http\Requests\Concerns\ResolvesCategoryCashflowDirection;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreCategoryRequest extends FormRequest
{
use ResolvesCategoryCashflowDirection;
/**
* Determine if the user is authorized to make this request.
*/
@ -19,31 +22,6 @@ class StoreCategoryRequest extends FormRequest
return true;
}
protected function prepareForValidation(): void
{
$type = CategoryType::tryFrom((string) $this->input('type'));
if (in_array($type, [CategoryType::Savings, CategoryType::Investment], true)) {
$this->merge([
'cashflow_direction' => CategoryCashflowDirection::Outflow->value,
]);
return;
}
if ($type !== CategoryType::Transfer) {
$this->merge([
'cashflow_direction' => CategoryCashflowDirection::Hidden->value,
]);
return;
}
$this->merge([
'cashflow_direction' => $this->input('cashflow_direction', CategoryCashflowDirection::Hidden->value),
]);
}
/**
* Get the validation rules that apply to the request.
*

View File

@ -5,12 +5,15 @@ namespace App\Http\Requests\Settings;
use App\Enums\CategoryCashflowDirection;
use App\Enums\CategoryColor;
use App\Enums\CategoryType;
use App\Http\Requests\Concerns\ResolvesCategoryCashflowDirection;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateCategoryRequest extends FormRequest
{
use ResolvesCategoryCashflowDirection;
/**
* Determine if the user is authorized to make this request.
*/
@ -19,31 +22,6 @@ class UpdateCategoryRequest extends FormRequest
return true;
}
protected function prepareForValidation(): void
{
$type = CategoryType::tryFrom((string) $this->input('type'));
if (in_array($type, [CategoryType::Savings, CategoryType::Investment], true)) {
$this->merge([
'cashflow_direction' => CategoryCashflowDirection::Outflow->value,
]);
return;
}
if ($type !== CategoryType::Transfer) {
$this->merge([
'cashflow_direction' => CategoryCashflowDirection::Hidden->value,
]);
return;
}
$this->merge([
'cashflow_direction' => $this->input('cashflow_direction', CategoryCashflowDirection::Hidden->value),
]);
}
/**
* Get the validation rules that apply to the request.
*