Search.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. protected $em;
  11. protected $repoFilm;
  12. /**
  13. * Search constructor.
  14. * @param EntityManagerInterface $em
  15. */
  16. public function __construct(EntityManagerInterface $em, FilmRepository $repoFilm)
  17. {
  18. $this->em = $em;
  19. $this->repoFilm = $repoFilm;
  20. }
  21. /**
  22. * @param string $query
  23. * @return \App\Entity\Film[]
  24. */
  25. public function search($query)
  26. {
  27. return \array_merge(
  28. $this->searchByFilm($query),
  29. $this->searchByRealisateur($query),
  30. $this->searchByGenre($query)
  31. );
  32. }
  33. /**
  34. * @param $query
  35. * @return array
  36. */
  37. protected function searchByFilm($query)
  38. {
  39. $q = $this->em
  40. ->createQuery('SELECT f from App:Film f WHERE f.titre like :titre')
  41. ->setParameter('titre', '%'.$query.'%')
  42. ->getResult();
  43. return $q;
  44. }
  45. /**
  46. * @param $query
  47. * @return array
  48. */
  49. protected function searchByRealisateur($query)
  50. {
  51. return $this->repoFilm->findFilmWithRealLike($query);
  52. }
  53. /**
  54. * @param $query
  55. * @return array
  56. */
  57. protected function searchByGenre($query)
  58. {
  59. return $this->repoFilm->findFilmWithGenreLike($query);
  60. }
  61. }