| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AccountMetaFactory.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01: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-02-21 20:34:24 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01: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-02-21 20:34:24 +01: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-02-21 20:34:24 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 06:27:30 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2018-02-21 20:34:24 +01:00
										 |  |  | use FireflyIII\Models\AccountMeta; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountMetaFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountMetaFactory | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-28 06:27:30 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Create update or delete meta data. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Account $account | 
					
						
							|  |  |  |      * @param string  $field | 
					
						
							|  |  |  |      * @param string  $value | 
					
						
							| 
									
										
										
										
											2018-07-28 06:27:30 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return AccountMeta|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function crud(Account $account, string $field, string $value): ?AccountMeta | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-03 06:48:53 +01:00
										 |  |  |         /** @var AccountMeta|null $entry */ | 
					
						
							| 
									
										
										
										
											2018-07-28 06:27:30 +02:00
										 |  |  |         $entry = $account->accountMeta()->where('name', $field)->first(); | 
					
						
							|  |  |  |         // must not be an empty string:
 | 
					
						
							|  |  |  |         if ('' !== $value) { | 
					
						
							|  |  |  |             // if $data has field and $entry is null, create new one:
 | 
					
						
							|  |  |  |             if (null === $entry) { | 
					
						
							| 
									
										
										
										
											2018-08-24 07:18:33 +02:00
										 |  |  |                 return $this->create(['account_id' => $account->id, 'name' => $field, 'data' => $value]); | 
					
						
							| 
									
										
										
										
											2018-07-28 06:27:30 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // if $data has field and $entry is not null, update $entry:
 | 
					
						
							| 
									
										
										
										
											2022-03-29 15:10:05 +02:00
										 |  |  |             $entry->data = $value; | 
					
						
							|  |  |  |             $entry->save(); | 
					
						
							| 
									
										
										
										
											2018-07-28 06:27:30 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-24 07:18:33 +02:00
										 |  |  |         if ('' === $value && null !== $entry) { | 
					
						
							| 
									
										
										
										
											2023-01-03 06:48:53 +01:00
										 |  |  |             $entry->delete(); | 
					
						
							| 
									
										
										
										
											2019-02-13 17:38:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-24 07:18:33 +02:00
										 |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2018-07-28 06:27:30 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $entry; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return AccountMeta|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function create(array $data): ?AccountMeta | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return AccountMeta::create($data); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |