mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\MainBundle\Search\Entity;
|
|
|
|
use Chill\MainBundle\Repository\UserRepository;
|
|
use Chill\MainBundle\Search\SearchApiInterface;
|
|
use Chill\MainBundle\Search\SearchApiQuery;
|
|
|
|
use function array_map;
|
|
use function in_array;
|
|
|
|
class SearchUserApiProvider implements SearchApiInterface
|
|
{
|
|
private UserRepository $userRepository;
|
|
|
|
public function __construct(UserRepository $userRepository)
|
|
{
|
|
$this->userRepository = $userRepository;
|
|
}
|
|
|
|
public function getResult(string $key, array $metadata, float $pertinence)
|
|
{
|
|
return $this->userRepository->find($metadata['id']);
|
|
}
|
|
|
|
public function prepare(array $metadatas): void
|
|
{
|
|
$ids = array_map(static fn ($m) => $m['id'], $metadatas);
|
|
|
|
$this->userRepository->findBy(['id' => $ids]);
|
|
}
|
|
|
|
public function provideQuery(string $pattern, array $parameters): SearchApiQuery
|
|
{
|
|
$query = new SearchApiQuery();
|
|
$query
|
|
->setSelectKey('user')
|
|
->setSelectJsonbMetadata("jsonb_build_object('id', u.id)")
|
|
->setSelectPertinence('GREATEST(SIMILARITY(LOWER(UNACCENT(?)), u.label),
|
|
SIMILARITY(LOWER(UNACCENT(?)), u.usernamecanonical))', [$pattern, $pattern])
|
|
->setFromClause('users AS u')
|
|
->setWhereClauses('
|
|
SIMILARITY(LOWER(UNACCENT(?)), u.usernamecanonical) > 0.15
|
|
OR u.usernamecanonical LIKE \'%\' || LOWER(UNACCENT(?)) || \'%\'
|
|
OR SIMILARITY(LOWER(UNACCENT(?)), LOWER(UNACCENT(u.label))) > 0.15
|
|
OR u.label LIKE \'%\' || LOWER(UNACCENT(?)) || \'%\'
|
|
', [$pattern, $pattern, $pattern, $pattern]);
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function supportsResult(string $key, array $metadatas): bool
|
|
{
|
|
return 'user' === $key;
|
|
}
|
|
|
|
public function supportsTypes(string $pattern, array $types, array $parameters): bool
|
|
{
|
|
return in_array('user', $types, true);
|
|
}
|
|
}
|