. */ declare(strict_types=1); namespace FireflyIII\Api\V1\Requests\Models\Recurrence; use FireflyIII\Models\Recurrence; use FireflyIII\Rules\BelongsUser; use FireflyIII\Rules\IsBoolean; use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ConvertsDataTypes; use FireflyIII\Support\Request\GetRecurrenceData; use FireflyIII\Validation\CurrencyValidation; use FireflyIII\Validation\RecurrenceValidation; use FireflyIII\Validation\TransactionValidation; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Validator; /** * Class UpdateRequest */ class UpdateRequest extends FormRequest { use ConvertsDataTypes; use RecurrenceValidation; use TransactionValidation; use CurrencyValidation; use GetRecurrenceData; use ChecksLogin; /** * Get all data from the request. * * @return array */ public function getAll(): array { // this is the way: $fields = [ 'title' => ['title', 'convertString'], 'description' => ['description', 'convertString'], 'first_date' => ['first_date', 'date'], 'repeat_until' => ['repeat_until', 'date'], 'nr_of_repetitions' => ['nr_of_repetitions', 'convertInteger'], 'apply_rules' => ['apply_rules', 'boolean'], 'active' => ['active', 'boolean'], 'notes' => ['notes', 'convertString'], ]; $reps = $this->getRepetitionData(); $transactions = $this->getTransactionData(); $return = [ 'recurrence' => $this->getAllData($fields), ]; if (null !== $reps) { $return['repetitions'] = $reps; } if (null !== $transactions) { $return['transactions'] = $transactions; } return $return; } /** * Returns the repetition data as it is found in the submitted data. * * @return array|null */ private function getRepetitionData(): ?array { $return = []; // repetition data: /** @var array|null $repetitions */ $repetitions = $this->get('repetitions'); if (null === $repetitions) { return null; } /** @var array $repetition */ foreach ($repetitions as $repetition) { $current = []; if (array_key_exists('type', $repetition)) { $current['type'] = $repetition['type']; } if (array_key_exists('moment', $repetition)) { $current['moment'] = (string)$repetition['moment']; } if (array_key_exists('skip', $repetition)) { $current['skip'] = (int)$repetition['skip']; } if (array_key_exists('weekend', $repetition)) { $current['weekend'] = (int)$repetition['weekend']; } $return[] = $current; } if (0 === count($return)) { return null; } return $return; } /** * Returns the transaction data as it is found in the submitted data. It's a complex method according to code * standards but it just has a lot of ??-statements because of the fields that may or may not exist. * * @return array|null */ private function getTransactionData(): ?array { $return = []; // transaction data: /** @var array|null $transactions */ $transactions = $this->get('transactions'); if (null === $transactions) { return null; } /** @var array $transaction */ foreach ($transactions as $transaction) { $return[] = $this->getSingleTransactionData($transaction); } return $return; } /** * The rules that the incoming request must be matched against. * * @return array */ public function rules(): array { /** @var Recurrence $recurrence */ $recurrence = $this->route()->parameter('recurrence'); return [ 'title' => sprintf('between:1,255|uniqueObjectForUser:recurrences,title,%d', $recurrence->id), 'description' => 'between:1,65000', 'first_date' => 'date', 'apply_rules' => [new IsBoolean()], 'active' => [new IsBoolean()], 'repeat_until' => 'nullable|date', 'nr_of_repetitions' => 'nullable|numeric|between:1,31', 'repetitions.*.type' => 'in:daily,weekly,ndom,monthly,yearly', 'repetitions.*.moment' => 'between:0,10', 'repetitions.*.skip' => 'nullable|numeric|between:0,31', 'repetitions.*.weekend' => 'nullable|numeric|min:1|max:4', 'transactions.*.description' => 'between:1,255', 'transactions.*.amount' => 'numeric|gt:0', 'transactions.*.foreign_amount' => 'nullable|numeric|gt:0', 'transactions.*.currency_id' => 'nullable|numeric|exists:transaction_currencies,id', 'transactions.*.currency_code' => 'nullable|min:3|max:3|exists:transaction_currencies,code', 'transactions.*.foreign_currency_id' => 'nullable|numeric|exists:transaction_currencies,id', 'transactions.*.foreign_currency_code' => 'nullable|min:3|max:3|exists:transaction_currencies,code', 'transactions.*.source_id' => ['numeric', 'nullable', new BelongsUser()], 'transactions.*.source_name' => 'between:1,255|nullable', 'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser()], 'transactions.*.destination_name' => 'between:1,255|nullable', // new and updated fields: 'transactions.*.budget_id' => ['nullable', 'mustExist:budgets,id', new BelongsUser()], 'transactions.*.budget_name' => ['between:1,255', 'nullable', new BelongsUser()], 'transactions.*.category_id' => ['nullable', 'mustExist:categories,id', new BelongsUser()], 'transactions.*.category_name' => 'between:1,255|nullable', 'transactions.*.piggy_bank_id' => ['nullable', 'numeric', 'mustExist:piggy_banks,id', new BelongsUser()], 'transactions.*.piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser()], 'transactions.*.tags' => 'nullable|between:1,64000', ]; } /** * Configure the validator instance. * * @param Validator $validator * * @return void */ public function withValidator(Validator $validator): void { $validator->after( function (Validator $validator) { //$this->validateOneRecurrenceTransaction($validator); //$this->validateOneRepetitionUpdate($validator); $this->validateRecurrenceRepetition($validator); $this->validateRepetitionMoment($validator); $this->validateForeignCurrencyInformation($validator); $this->valUpdateAccountInfo($validator); } ); } }