UserAndGroupSearcher.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Muzich\CoreBundle\Searcher;
  3. use Symfony\Component\Validator\Constraints as Assert;
  4. use Doctrine\Bundle\DoctrineBundle\Registry;
  5. /**
  6. * Objet de recherche
  7. */
  8. class UserAndGroupSearcher extends Searcher implements SearcherInterface
  9. {
  10. /**
  11. * Chaine de caractère représentant la recherche.
  12. *
  13. * @var string
  14. * @Assert\NotBlank()
  15. * @Assert\Type("string")
  16. * @Assert\Length(min = 3)
  17. */
  18. protected $string;
  19. public function setString($string)
  20. {
  21. $this->string = $string;
  22. }
  23. public function getString()
  24. {
  25. return $this->string;
  26. }
  27. /**
  28. * @see SearcherInterface
  29. *
  30. * @return array
  31. */
  32. public function getParams()
  33. {
  34. return array(
  35. 'string' => $this->string
  36. );
  37. }
  38. /**
  39. * Retourne les user et groupes correspondant a la recherche
  40. *
  41. * @param Registry $doctrine
  42. * @return array
  43. */
  44. public function getResults(Registry $doctrine)
  45. {
  46. // On remplace le caratcère '%' au cas ou un malin l'insére.
  47. $string = str_replace('%', '#', $this->string);
  48. $users = $doctrine
  49. ->getRepository('MuzichCoreBundle:User')
  50. ->findByString($string)
  51. ->execute()
  52. ;
  53. $groups = $doctrine
  54. ->getRepository('MuzichCoreBundle:Group')
  55. ->findByString($string)
  56. ->execute()
  57. ;
  58. return array(
  59. 'users' => $users,
  60. 'groups' => $groups
  61. );
  62. }
  63. }