. */ declare(strict_types=1); namespace FireflyIII\Factory; use FireflyIII\Models\Category; use FireflyIII\User; use Illuminate\Support\Collection; use Log; /** * Class CategoryFactory */ class CategoryFactory { /** @var User */ private $user; /** * @param string $name * * @return Category|null */ public function findByName(string $name): ?Category { /** @var Collection $collection */ $collection = $this->user->categories()->get(); /** @var Category $category */ foreach ($collection as $category) { if ($category->name === $name) { return $category; } } return null; } /** * @param int|null $categoryId * @param null|string $categoryName * * @return Category|null */ public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category { $categoryId = intval($categoryId); $categoryName = strval($categoryName); Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName)); if (strlen($categoryName) === 0 && $categoryId === 0) { return null; } // first by ID: if ($categoryId > 0) { /** @var Category $category */ $category = $this->user->categories()->find($categoryId); if (!is_null($category)) { return $category; } } if (strlen($categoryName) > 0) { $category = $this->findByName($categoryName); if (!is_null($category)) { return $category; } return Category::create( [ 'user_id' => $this->user->id, 'name' => $categoryName, ] ); } return null; } /** * @param User $user */ public function setUser(User $user) { $this->user = $user; } }