mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-01 14:36:13 +00:00
83 lines
3.1 KiB
PHP
83 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Chill\PersonBundle\Search;
|
|
|
|
use Chill\MainBundle\Search\Utils\ExtractDateFromPattern;
|
|
use Chill\MainBundle\Search\Utils\ExtractPhonenumberFromPattern;
|
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
|
use Chill\PersonBundle\Repository\PersonACLAwareRepositoryInterface;
|
|
use Chill\PersonBundle\Repository\PersonRepository;
|
|
use Chill\MainBundle\Search\SearchApiQuery;
|
|
use Chill\MainBundle\Search\SearchApiInterface;
|
|
use Symfony\Component\Security\Core\Security;
|
|
|
|
class SearchPersonApiProvider implements SearchApiInterface
|
|
{
|
|
private PersonRepository $personRepository;
|
|
private Security $security;
|
|
private AuthorizationHelperInterface $authorizationHelper;
|
|
private ExtractDateFromPattern $extractDateFromPattern;
|
|
private PersonACLAwareRepositoryInterface $personACLAwareRepository;
|
|
private ExtractPhonenumberFromPattern $extractPhonenumberFromPattern;
|
|
|
|
public function __construct(
|
|
PersonRepository $personRepository,
|
|
PersonACLAwareRepositoryInterface $personACLAwareRepository,
|
|
Security $security,
|
|
AuthorizationHelperInterface $authorizationHelper,
|
|
ExtractDateFromPattern $extractDateFromPattern,
|
|
ExtractPhonenumberFromPattern $extractPhonenumberFromPattern
|
|
) {
|
|
$this->personRepository = $personRepository;
|
|
$this->personACLAwareRepository = $personACLAwareRepository;
|
|
$this->security = $security;
|
|
$this->authorizationHelper = $authorizationHelper;
|
|
$this->extractDateFromPattern = $extractDateFromPattern;
|
|
$this->extractPhonenumberFromPattern = $extractPhonenumberFromPattern;
|
|
}
|
|
|
|
public function provideQuery(string $pattern, array $parameters): SearchApiQuery
|
|
{
|
|
$datesResult = $this->extractDateFromPattern->extractDates($pattern);
|
|
$phoneResult = $this->extractPhonenumberFromPattern->extractPhonenumber($datesResult->getFilteredSubject());
|
|
$filtered = $phoneResult->getFilteredSubject();
|
|
|
|
return $this->personACLAwareRepository->buildAuthorizedQuery(
|
|
$filtered,
|
|
null,
|
|
null,
|
|
count($datesResult->getFound()) > 0 ? $datesResult->getFound()[0] : null,
|
|
null,
|
|
null,
|
|
null,
|
|
null,
|
|
count($phoneResult->getFound()) > 0 ? $phoneResult->getFound()[0] : null
|
|
)
|
|
->setSelectKey("person")
|
|
->setSelectJsonbMetadata("jsonb_build_object('id', person.id)");
|
|
}
|
|
|
|
|
|
public function supportsTypes(string $pattern, array $types, array $parameters): bool
|
|
{
|
|
return \in_array('person', $types);
|
|
}
|
|
|
|
public function prepare(array $metadatas): void
|
|
{
|
|
$ids = \array_map(fn($m) => $m['id'], $metadatas);
|
|
|
|
$this->personRepository->findByIds($ids);
|
|
}
|
|
|
|
public function supportsResult(string $key, array $metadatas): bool
|
|
{
|
|
return $key === 'person';
|
|
}
|
|
|
|
public function getResult(string $key, array $metadata, float $pertinence)
|
|
{
|
|
return $this->personRepository->find($metadata['id']);
|
|
}
|
|
}
|