em = $em; $this->authorizationHelper = $authorizationHelper; $this->tokenStorage = $tokenStorage; } public function matchPerson( Person $person, float $precision = 0.30, string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY, bool $addYearComparison = false, ) { $centers = $this->authorizationHelper->getReachableCenters( $this->tokenStorage->getToken()->getUser(), PersonVoter::SEE ); $query = $this->em->createQuery(); $qb = $this->em->createQueryBuilder(); $qb->select('p') ->from(Person::class, 'p') ->join('p.centerHistory', 'center_history') ->where('SIMILARITY(p.fullnameCanonical, UNACCENT(LOWER(:fullName))) >= :precision') ->andWhere($qb->expr()->in('center_history.center', ':centers')) ->andWhere($qb->expr()->andX( $qb->expr()->lte('center_history.startDate', 'CURRENT_DATE()'), $qb->expr()->orX( $qb->expr()->isNull('center_history.endDate'), $qb->expr()->gt('center_history.endDate', 'CURRENT_DATE()') ) )) ; $qb ->setParameter('fullName', $this->personRender->renderString($person, [])) ->setParameter('centers', $centers) ->setParameter('precision', $precision); if (null !== $person->getBirthdate()) { $qb->andWhere($qb->expr()->orX( $qb->expr()->eq('p.birthdate', ':personBirthdate'), $qb->expr()->isNull('p.birthdate') )); $qb->setParameter('personBirthdate', $person->getBirthdate()); } if (null !== $person->getId()) { $qb->andWhere($qb->expr()->neq('p.id', ':personId')); $qb->setParameter('personId', $person->getId()); $notDuplicatePersons = $this->personNotDuplicateRepository->findNotDuplicatePerson($person); if (\count($notDuplicatePersons)) { $qb->andWhere($qb->expr()->notIn('p.id', ':notDuplicatePersons')); $qb->setParameter('notDuplicatePersons', $notDuplicatePersons); } } switch ($orderBy) { case self::SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL: $qb->orderBy('p.fullnameCanonical', 'ASC'); break; case self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY: default: $qb->orderBy('SIMILARITY(p.fullnameCanonical, UNACCENT(LOWER(:fullName)))', 'DESC'); $qb->setParameter('fullName', $this->personRender->renderString($person, [])); } return $qb->getQuery()->getResult(); } }