diff --git a/app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection.php b/app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection.php new file mode 100644 index 00000000..55419017 --- /dev/null +++ b/app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection.php @@ -0,0 +1,37 @@ +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), + ]); + } +} diff --git a/app/Http/Requests/Settings/StoreCategoryRequest.php b/app/Http/Requests/Settings/StoreCategoryRequest.php index 8bd2c4c0..921062f9 100644 --- a/app/Http/Requests/Settings/StoreCategoryRequest.php +++ b/app/Http/Requests/Settings/StoreCategoryRequest.php @@ -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. * diff --git a/app/Http/Requests/Settings/UpdateCategoryRequest.php b/app/Http/Requests/Settings/UpdateCategoryRequest.php index 4fb35df8..a9be4cd7 100644 --- a/app/Http/Requests/Settings/UpdateCategoryRequest.php +++ b/app/Http/Requests/Settings/UpdateCategoryRequest.php @@ -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. *