All checks were successful
Deploy api / deploy (push) Successful in 22s
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Requests\Api;
|
||
|
||
use Carbon\Carbon;
|
||
use Illuminate\Contracts\Validation\Validator;
|
||
use Illuminate\Foundation\Http\FormRequest;
|
||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||
|
||
/**
|
||
* 継続課金請求リクエストバリデーション
|
||
*/
|
||
class SubscriptionChargeRequest extends FormRequest
|
||
{
|
||
public function authorize(): bool
|
||
{
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* バリデーションルール
|
||
*/
|
||
public function rules(): array
|
||
{
|
||
return [
|
||
'member_id' => ['required', 'string', 'max:20'],
|
||
'amount' => ['required', 'integer', 'min:1'],
|
||
'execute_date' => ['required', 'string', 'regex:/^\d{8}$/'],
|
||
'contract_id' => ['required', 'string', 'max:20'],
|
||
'card_seq' => ['nullable', 'integer'],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* バリデーション失敗時のJSONエラーレスポンス
|
||
*/
|
||
protected function failedValidation(Validator $validator): void
|
||
{
|
||
throw new HttpResponseException(response()->json([
|
||
'error' => [
|
||
'code' => 'INVALID_REQUEST',
|
||
'message' => $validator->errors()->first(),
|
||
]
|
||
], 400));
|
||
}
|
||
|
||
/**
|
||
* 追加バリデーション(execute_dateの実在日チェック)
|
||
*/
|
||
public function withValidator($validator): void
|
||
{
|
||
$validator->after(function ($validator) {
|
||
$executeDate = $this->input('execute_date');
|
||
if (!$executeDate) {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
Carbon::createFromFormat('Ymd', $executeDate, 'Asia/Tokyo');
|
||
$errors = Carbon::getLastErrors() ?: ['warning_count' => 0, 'error_count' => 0];
|
||
if (($errors['warning_count'] ?? 0) > 0 || ($errors['error_count'] ?? 0) > 0) {
|
||
$validator->errors()->add('execute_date', 'execute_dateの日付が不正です。');
|
||
}
|
||
} catch (\Throwable) {
|
||
$validator->errors()->add('execute_date', 'execute_dateの日付が不正です。');
|
||
}
|
||
});
|
||
}
|
||
}
|