. */ declare(strict_types=1); namespace FireflyIII\Factory; use FireflyIII\Models\Account; use FireflyIII\Models\AccountMeta; /** * Class AccountMetaFactory */ class AccountMetaFactory { /** * Create update or delete meta data. */ public function crud(Account $account, string $field, string $value): ?AccountMeta { /** @var null|AccountMeta $entry */ $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) { return $this->create(['account_id' => $account->id, 'name' => $field, 'data' => $value]); } // if $data has field and $entry is not null, update $entry: $entry->data = $value; $entry->save(); } if ('' === $value && null !== $entry) { $entry->delete(); return null; } return $entry; } public function create(array $data): ?AccountMeta { return AccountMeta::create($data); } }