| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  |  * PiggyBankUpdateRequest.php | 
					
						
							| 
									
										
										
										
											2020-01-23 19:44:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Api\V1\Requests; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Rules\IsAssetAccountId; | 
					
						
							| 
									
										
										
										
											2019-08-21 05:15:47 +02:00
										 |  |  | use FireflyIII\Rules\LessThanPiggyTarget; | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  | use FireflyIII\Support\Request\ChecksLogin; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:34:00 +02:00
										 |  |  | use FireflyIII\Support\Request\ConvertsDataTypes; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:25:25 +02:00
										 |  |  | use Illuminate\Foundation\Http\FormRequest; | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  |  * Class PiggyBankUpdateRequest | 
					
						
							| 
									
										
										
										
											2019-09-04 17:39:39 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-04-12 04:53:18 +02:00
										 |  |  |  * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  | class PiggyBankUpdateRequest extends FormRequest | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  |     use ConvertsDataTypes, ChecksLogin; | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Get all data from the request. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAll(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-02 06:41:42 +01:00
										 |  |  |         // if the value isn't present, dont return it at all.
 | 
					
						
							|  |  |  |         // TODO this should be the way to collect fields for all API things.
 | 
					
						
							|  |  |  |         // TODO make sure piggy bank uses 'start_date' etc. until right up to DB update.
 | 
					
						
							|  |  |  |         // TODO can we configure this and return it from config?
 | 
					
						
							| 
									
										
										
										
											2020-12-02 17:12:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // TODO this is the way.
 | 
					
						
							| 
									
										
										
										
											2020-12-02 06:41:42 +01:00
										 |  |  |         $fields = [ | 
					
						
							|  |  |  |             'name'            => ['name', 'string'], | 
					
						
							|  |  |  |             'account_id'      => ['account_id', 'integer'], | 
					
						
							|  |  |  |             'targetamount'    => ['target_amount', 'string'], | 
					
						
							|  |  |  |             'current_amount'  => ['current_amount', 'string'], | 
					
						
							|  |  |  |             'startdate'       => ['start_date', 'date'], | 
					
						
							|  |  |  |             'targetdate'      => ['target_date', 'string'], | 
					
						
							|  |  |  |             'notes'           => ['notes', 'nlString'], | 
					
						
							|  |  |  |             'order'           => ['order', 'integer'], | 
					
						
							|  |  |  |             'object_group'    => ['object_group', 'string'], | 
					
						
							|  |  |  |             'object_group_id' => ['object_group_id', 'integer'], | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2020-12-02 17:12:58 +01:00
										 |  |  |         return $this->getAllData($fields); | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * The rules that the incoming request must be matched against. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function rules(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-12-02 06:41:42 +01:00
										 |  |  |         $piggyBank = $this->route()->parameter('piggyBank'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 13:31:54 +01:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             'name'           => 'between:1,255|uniquePiggyBankForUser:' . $piggyBank->id, | 
					
						
							|  |  |  |             'current_amount' => ['numeric', 'gte:0', new LessThanPiggyTarget], | 
					
						
							| 
									
										
										
										
											2020-12-02 06:41:42 +01:00
										 |  |  |             'target_amount'  => 'numeric|gt:0', | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |             'start_date'     => 'date|nullable', | 
					
						
							| 
									
										
										
										
											2018-09-09 20:40:26 +02:00
										 |  |  |             'target_date'    => 'date|nullable|after:start_date', | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |             'notes'          => 'max:65000', | 
					
						
							| 
									
										
										
										
											2020-12-02 06:41:42 +01:00
										 |  |  |             'account_id'     => ['belongsToUser:accounts', new IsAssetAccountId], | 
					
						
							| 
									
										
										
										
											2018-06-29 06:43:44 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |