2018-02-19 12:44:46 -06:00
|
|
|
<?php
|
2022-12-29 12:41:57 -06:00
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
/**
|
|
|
|
* PiggyBankFactory.php
|
2020-02-16 07:00:57 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-19 12:44:46 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-19 12:44:46 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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-19 12:44:46 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-19 12:44:46 -06:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-19 12:44:46 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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-19 12:44:46 -06:00
|
|
|
*/
|
2018-05-11 03:08:34 -05:00
|
|
|
declare(strict_types=1);
|
2018-02-19 12:44:46 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Factory;
|
2021-03-28 04:46:23 -05:00
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
use FireflyIII\Models\PiggyBank;
|
|
|
|
use FireflyIII\User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PiggyBankFactory
|
|
|
|
*/
|
|
|
|
class PiggyBankFactory
|
|
|
|
{
|
2020-10-23 12:11:25 -05:00
|
|
|
private User $user;
|
2018-09-06 05:29:32 -05:00
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
|
|
|
|
{
|
2022-12-29 12:41:57 -06:00
|
|
|
$piggyBankId = (int)$piggyBankId;
|
|
|
|
$piggyBankName = (string)$piggyBankName;
|
2018-07-06 12:06:08 -05:00
|
|
|
if ('' === $piggyBankName && 0 === $piggyBankId) {
|
2018-02-19 12:44:46 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// first find by ID:
|
|
|
|
if ($piggyBankId > 0) {
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|PiggyBank $piggyBank */
|
2018-03-01 13:54:50 -06:00
|
|
|
$piggyBank = $this->user->piggyBanks()->find($piggyBankId);
|
2018-04-02 07:42:07 -05:00
|
|
|
if (null !== $piggyBank) {
|
2018-02-19 12:44:46 -06:00
|
|
|
return $piggyBank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then find by name:
|
2019-02-13 10:38:41 -06:00
|
|
|
if ('' !== $piggyBankName) {
|
2023-12-20 12:35:52 -06:00
|
|
|
/** @var null|PiggyBank $piggyBank */
|
2018-03-01 13:54:50 -06:00
|
|
|
$piggyBank = $this->findByName($piggyBankName);
|
2018-04-02 07:42:07 -05:00
|
|
|
if (null !== $piggyBank) {
|
2018-02-19 12:44:46 -06:00
|
|
|
return $piggyBank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-01 13:54:50 -06:00
|
|
|
public function findByName(string $name): ?PiggyBank
|
|
|
|
{
|
2019-08-10 06:42:33 -05:00
|
|
|
return $this->user->piggyBanks()->where('piggy_banks.name', $name)->first();
|
2018-03-01 13:54:50 -06:00
|
|
|
}
|
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
public function setUser(User $user): void
|
2018-02-19 12:44:46 -06:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|