em = $em; $this->authorizationHelper = $authorizationHelper; $this->tokenStorage = $tokenStorage; $this->personNotDuplicateRepository = $personNotDuplicateRepository; $this->personRender = $personRender; } public function matchPerson( Person $person, float $precision = 0.15, string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY, bool $addYearComparison = false ) { $centers = $this->authorizationHelper->getReachableCenters( $this->tokenStorage->getToken()->getUser(), new Role(PersonVoter::SEE) ); $query = $this->em->createQuery(); $dql = 'SELECT p from ChillPersonBundle:Person p ' . ' WHERE (' . ' SIMILARITY(p.fullnameCanonical, UNACCENT(LOWER(:fullName))) >= :precision ' . ' ) ' . ' AND p.center IN (:centers)'; if ($person->getId() !== null) { $dql .= ' AND p.id != :personId '; $notDuplicatePersons = $this->personNotDuplicateRepository->findNotDuplicatePerson($person); $query->setParameter('personId', $person->getId()); if (count($notDuplicatePersons)) { $dql .= ' AND p.id not in (:notDuplicatePersons)'; $query->setParameter('notDuplicatePersons', $notDuplicatePersons); } } switch ($orderBy) { case self::SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL: $dql .= ' ORDER BY p.fullnameCanonical ASC '; break; case self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY: default: $dql .= ' ORDER BY SIMILARITY(p.fullnameCanonical, UNACCENT(LOWER(:fullName))) DESC '; } $query = $query ->setDQL($dql) ->setParameter('fullName', $this->personRender->renderString($person, [])) ->setParameter('centers', $centers) ->setParameter('precision', $precision); return $query->getResult(); } }