. */ /** @noinspection MultipleReturnStatementsInspection */ declare(strict_types=1); namespace FireflyIII\Factory; use FireflyIII\Models\Budget; use FireflyIII\User; /** * Class BudgetFactory. */ class BudgetFactory { private User $user; /** * @param int|null $budgetId * @param null|string $budgetName * * @return Budget|null */ public function find(?int $budgetId, ?string $budgetName): ?Budget { $budgetId = (int) $budgetId; $budgetName = (string) $budgetName; if (0 === $budgetId && '' === $budgetName) { return null; } // first by ID: if ($budgetId > 0) { /** @var Budget $budget */ $budget = $this->user->budgets()->find($budgetId); if (null !== $budget) { return $budget; } } if ('' !== $budgetName) { $budget = $this->findByName($budgetName); if (null !== $budget) { return $budget; } } return null; } /** * @param string $name * * @return Budget|null */ public function findByName(string $name): ?Budget { return $this->user->budgets()->where('name', $name)->first(); } /** * @param User $user */ public function setUser(User $user): void { $this->user = $user; } }