2018-02-19 12:44:46 -06:00
|
|
|
<?php
|
2018-03-19 06:09:12 -05:00
|
|
|
declare(strict_types=1);
|
2018-02-19 12:44:46 -06:00
|
|
|
/**
|
|
|
|
* PiggyBankFactory.php
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace FireflyIII\Factory;
|
|
|
|
|
|
|
|
|
|
|
|
use FireflyIII\Models\PiggyBank;
|
|
|
|
use FireflyIII\User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PiggyBankFactory
|
|
|
|
*/
|
|
|
|
class PiggyBankFactory
|
|
|
|
{
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $piggyBankId
|
|
|
|
* @param null|string $piggyBankName
|
|
|
|
*
|
|
|
|
* @return PiggyBank|null
|
|
|
|
*/
|
|
|
|
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
|
|
|
|
{
|
2018-04-02 07:42:07 -05:00
|
|
|
$piggyBankId = (int)$piggyBankId;
|
|
|
|
$piggyBankName = (string)$piggyBankName;
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\strlen($piggyBankName) === 0 && $piggyBankId === 0) {
|
2018-02-19 12:44:46 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// first find by ID:
|
|
|
|
if ($piggyBankId > 0) {
|
|
|
|
/** @var 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:
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\strlen($piggyBankName) > 0) {
|
2018-02-19 12:44:46 -06:00
|
|
|
/** @var 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
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return PiggyBank|null
|
|
|
|
*/
|
|
|
|
public function findByName(string $name): ?PiggyBank
|
|
|
|
{
|
|
|
|
$set = $this->user->piggyBanks()->get();
|
|
|
|
/** @var PiggyBank $piggy */
|
|
|
|
foreach ($set as $piggy) {
|
|
|
|
if ($piggy->name === $name) {
|
|
|
|
return $piggy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|