mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-28 17:44:58 +00:00
person: move similar person matcher to PersonACLEwareRepository
This commit is contained in:
@@ -14,10 +14,13 @@ namespace Chill\PersonBundle\Repository;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Repository\CountryRepository;
|
||||
use Chill\MainBundle\Search\ParsingException;
|
||||
use Chill\MainBundle\Search\SearchApi;
|
||||
use Chill\MainBundle\Search\SearchApiQuery;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
||||
use Chill\PersonBundle\Templating\Entity\PersonRender;
|
||||
use Chill\PersonBundle\Repository\PersonNotDuplicateRepository;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\NonUniqueResultException;
|
||||
@@ -42,16 +45,24 @@ final class PersonACLAwareRepository implements PersonACLAwareRepositoryInterfac
|
||||
|
||||
private Security $security;
|
||||
|
||||
private PersonRender $personRender;
|
||||
|
||||
private PersonNotDuplicateRepository $personNotDuplicateRepository;
|
||||
|
||||
public function __construct(
|
||||
Security $security,
|
||||
EntityManagerInterface $em,
|
||||
CountryRepository $countryRepository,
|
||||
AuthorizationHelper $authorizationHelper
|
||||
AuthorizationHelper $authorizationHelper,
|
||||
PersonRender $personRender,
|
||||
PersonNotDuplicateRepository $personNotDuplicateRepository
|
||||
) {
|
||||
$this->security = $security;
|
||||
$this->em = $em;
|
||||
$this->countryRepository = $countryRepository;
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->personRender = $personRender;
|
||||
$this->personNotDuplicateRepository = $personNotDuplicateRepository;
|
||||
}
|
||||
|
||||
public function buildAuthorizedQuery(
|
||||
@@ -322,4 +333,70 @@ final class PersonACLAwareRepository implements PersonACLAwareRepositoryInterfac
|
||||
}, $authorizedCenters)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws NonUniqueResultException
|
||||
* @throws ParsingException
|
||||
*
|
||||
* @return array|Person[]
|
||||
*/
|
||||
public function findMatchingPersons(
|
||||
Person $person,
|
||||
float $precision = 0.15,
|
||||
string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY
|
||||
): array {
|
||||
$query = $this->matchPerson($person, $precision, $orderBy);
|
||||
$authorizedQuery = $this->addAuthorizations($query);
|
||||
|
||||
return $this->fetchQueryPerson($authorizedQuery);
|
||||
}
|
||||
|
||||
public function matchPerson(
|
||||
Person $person,
|
||||
float $precision = 0.15,
|
||||
string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY
|
||||
): SearchApiQuery {
|
||||
|
||||
$fullName = $this->personRender->renderString($person, []);
|
||||
|
||||
$query = new SearchApiQuery();
|
||||
$query->setFromClause('chill_person_person AS person');
|
||||
$query->andWhereClause(
|
||||
"SIMILARITY(person.fullnameCanonical, UNACCENT(LOWER(?))) >= ?",
|
||||
[$fullName, $precision]
|
||||
);
|
||||
|
||||
if (null !== $person->getId()) {
|
||||
$query->andWhereClause(
|
||||
"person.id != ?",
|
||||
[$person->getId()]
|
||||
);
|
||||
|
||||
$notDuplicatePersons = $this->personNotDuplicateRepository->findNotDuplicatePerson($person);
|
||||
|
||||
if (count($notDuplicatePersons)) {
|
||||
$query->andWhereClause(
|
||||
"person.id NOT IN (?)",
|
||||
[$notDuplicatePersons]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
switch ($orderBy) {
|
||||
case self::SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL:
|
||||
$query->setSelectPertinence('person.fullnameCanonical');
|
||||
break;
|
||||
|
||||
case self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY:
|
||||
default:
|
||||
$query->setSelectPertinence(
|
||||
'SIMILARITY(person.fullnameCanonical, UNACCENT(LOWER(?)))',
|
||||
[$fullName]
|
||||
);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -17,6 +17,10 @@ use DateTimeInterface;
|
||||
|
||||
interface PersonACLAwareRepositoryInterface
|
||||
{
|
||||
public const SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL = 'alphabetical';
|
||||
|
||||
public const SIMILAR_SEARCH_ORDER_BY_SIMILARITY = 'similarity';
|
||||
|
||||
public function buildAuthorizedQuery(
|
||||
?string $default = null,
|
||||
?string $firstname = null,
|
||||
@@ -61,4 +65,13 @@ interface PersonACLAwareRepositoryInterface
|
||||
?string $phonenumber = null,
|
||||
?string $city = null
|
||||
): array;
|
||||
|
||||
/**
|
||||
* @return array|Person[]
|
||||
*/
|
||||
public function findMatchingPersons(
|
||||
Person $person,
|
||||
float $precision = 0.15,
|
||||
string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY
|
||||
): array;
|
||||
}
|
||||
|
Reference in New Issue
Block a user