users = $users; $this->customers = $customers; $this->projects = $projects; $this->activities = $activities; } /** * {@inheritdoc} */ public function getFilters() { return [ new TwigFilter('user', [$this, 'getUser']), new TwigFilter('customer', [$this, 'getCustomer']), new TwigFilter('project', [$this, 'getProject']), new TwigFilter('activity', [$this, 'getActivity']), ]; } /** * @param int|User $user * @param bool $allowEmpty * @return User|null */ public function getUser($user, $allowEmpty = true) { if ($user instanceof User) { return $user; } $entity = $this->users->getById($user); if (null === $entity) { $entity = $this->users->loadUserByUsername($user); } if (null === $entity && false === $allowEmpty) { $entity = new User(); $entity->setUsername(self::UNKNOWN_NAME); } return $entity; } /** * @param int|Customer $customer * @param bool $allowEmpty * @return Customer|null */ public function getCustomer($customer, $allowEmpty = true) { if ($customer instanceof Customer) { return $customer; } $entity = $this->customers->getById($customer); if (null === $entity && false === $allowEmpty) { $entity = new Customer(); $entity->setName(self::UNKNOWN_NAME); } return $entity; } /** * @param int|Project $project * @param bool $allowEmpty * @return Project|null */ public function getProject($project, $allowEmpty = true) { if ($project instanceof Project) { return $project; } $entity = $this->projects->getById($project); if (null === $entity && false === $allowEmpty) { $entity = new Project(); $entity->setName(self::UNKNOWN_NAME); $entity->setCustomer((new Customer())->setName(self::UNKNOWN_NAME)); } return $entity; } /** * @param int|Activity $activity * @param bool $allowEmpty * @return Activity|null */ public function getActivity($activity, $allowEmpty = true) { if ($activity instanceof Activity) { return $activity; } $entity = $this->activities->getById($activity); if (null === $entity && false === $allowEmpty) { $entity = new Activity(); $entity->setName(self::UNKNOWN_NAME); } return $entity; } }