Search.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Service;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use App\Repository\FilmRepository;
  5. /**
  6. * Faire des recherches de films
  7. */
  8. class Search
  9. {
  10. /**
  11. * Search constructor.
  12. * @param EntityManagerInterface $em
  13. */
  14. public function __construct(
  15. protected EntityManagerInterface $em,
  16. protected FilmRepository $repoFilm
  17. )
  18. {
  19. }
  20. /**
  21. * @param string $query
  22. * @return \App\Entity\Film[]
  23. */
  24. public function search(string $query): array
  25. {
  26. return \array_merge(
  27. $this->searchByFilm($query),
  28. $this->searchByRealisateur($query),
  29. $this->searchByGenre($query)
  30. );
  31. }
  32. /**
  33. * @param $query
  34. * @return array
  35. */
  36. protected function searchByFilm(string $query): array
  37. {
  38. $q = $this->em
  39. ->createQuery('SELECT f from App:Film f WHERE f.titre like :titre')
  40. ->setParameter('titre', '%'.$query.'%')
  41. ->getResult();
  42. return $q;
  43. }
  44. /**
  45. * @param $query
  46. * @return array
  47. */
  48. protected function searchByRealisateur(string $query): array
  49. {
  50. return $this->repoFilm->findFilmWithRealLike($query);
  51. }
  52. /**
  53. * @param $query
  54. * @return array
  55. */
  56. protected function searchByGenre(string $query): array
  57. {
  58. return $this->repoFilm->findFilmWithGenreLike($query);
  59. }
  60. }