2018-02-19 12:44:46 -06:00
|
|
|
<?php
|
2018-05-11 03:08:34 -05:00
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
/**
|
|
|
|
* BudgetFactory.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/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-11 03:08:34 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Factory;
|
|
|
|
|
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\User;
|
2018-03-01 13:54:50 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2018-02-19 12:44:46 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BudgetFactory
|
|
|
|
*/
|
|
|
|
class BudgetFactory
|
|
|
|
{
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $budgetId
|
|
|
|
* @param null|string $budgetName
|
|
|
|
*
|
|
|
|
* @return Budget|null
|
|
|
|
*/
|
|
|
|
public function find(?int $budgetId, ?string $budgetName): ?Budget
|
|
|
|
{
|
2018-04-02 07:42:07 -05:00
|
|
|
$budgetId = (int)$budgetId;
|
|
|
|
$budgetName = (string)$budgetName;
|
2018-02-19 12:44:46 -06:00
|
|
|
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\strlen($budgetName) === 0 && $budgetId === 0) {
|
2018-02-19 12:44:46 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first by ID:
|
|
|
|
if ($budgetId > 0) {
|
2018-03-01 13:54:50 -06:00
|
|
|
/** @var Budget $budget */
|
|
|
|
$budget = $this->user->budgets()->find($budgetId);
|
2018-04-02 07:42:07 -05:00
|
|
|
if (null !== $budget) {
|
2018-02-19 12:44:46 -06:00
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\strlen($budgetName) > 0) {
|
2018-03-01 13:54:50 -06:00
|
|
|
$budget = $this->findByName($budgetName);
|
2018-04-02 07:42:07 -05:00
|
|
|
if (null !== $budget) {
|
2018-02-19 12:44:46 -06:00
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-01 13:54:50 -06:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Budget|null
|
|
|
|
*/
|
|
|
|
public function findByName(string $name): ?Budget
|
|
|
|
{
|
|
|
|
/** @var Collection $collection */
|
|
|
|
$collection = $this->user->budgets()->get();
|
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($collection as $budget) {
|
|
|
|
if ($budget->name === $name) {
|
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|