mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
add endoint /api/1.0/search.json?q=xxx with fake data
See https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/136
This commit is contained in:
85
src/Bundle/ChillMainBundle/Search/SearchApi.php
Normal file
85
src/Bundle/ChillMainBundle/Search/SearchApi.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Search;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Chill\MainBundle\Search\SearchProvider;
|
||||
use Symfony\Component\VarDumper\Resources\functions\dump;
|
||||
|
||||
/**
|
||||
* ***Warning*** This is an incomplete implementation ***Warning***
|
||||
*/
|
||||
class SearchApi
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
private SearchProvider $search;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, SearchProvider $search)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->search = $search;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Model/Result[]
|
||||
*/
|
||||
public function getResults(string $query, int $offset, int $maxResult): array
|
||||
{
|
||||
// **warning again**: this is an incomplete implementation
|
||||
$results = [];
|
||||
|
||||
foreach ($this->getPersons($query) as $p) {
|
||||
$results[] = new Model\Result((float)\rand(0, 100) / 100, $p);
|
||||
}
|
||||
foreach ($this->getThirdParties($query) as $t) {
|
||||
$results[] = new Model\Result((float)\rand(0, 100) / 100, $t);
|
||||
}
|
||||
|
||||
\usort($results, function(Model\Result $a, Model\Result $b) {
|
||||
return ($a->getRelevance() <=> $b->getRelevance()) * -1;
|
||||
});
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function countResults(string $query): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function getThirdParties(string $query)
|
||||
{
|
||||
$thirdPartiesIds = $this->em->createQuery('SELECT t.id FROM '.ThirdParty::class.' t')
|
||||
->getScalarResult();
|
||||
|
||||
$nbResults = rand(0, 15);
|
||||
if ($nbResults === 1) {
|
||||
$nbResults++;
|
||||
}
|
||||
$ids = \array_rand($thirdPartiesIds, $nbResults);
|
||||
|
||||
return $this->em->getRepository(ThirdParty::class)
|
||||
->findById($ids);
|
||||
}
|
||||
|
||||
private function getPersons(string $query)
|
||||
{
|
||||
$params = [
|
||||
SearchInterface::SEARCH_PREVIEW_OPTION => false
|
||||
];
|
||||
$search = $this->search->getResultByName($query, 'person_regular', 0, 50, $params, 'json');
|
||||
$ids = \array_map(function($r) { return $r['id']; }, $search['results']);
|
||||
|
||||
|
||||
if (count($ids) === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->em->getRepository(Person::class)
|
||||
->findById($ids)
|
||||
;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user