UserAndGroupSearcher.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Muzich\CoreBundle\Searcher;
  3. use Symfony\Component\Validator\Constraints as Assert;
  4. use Symfony\Bundle\DoctrineBundle\Registry;
  5. class UserAndGroupSearcher extends Searcher implements SearcherInterface
  6. {
  7. /**
  8. * Chaine de caractère représentant la recherche.
  9. *
  10. * @var string
  11. * @Assert\NotBlank()
  12. * @Assert\Type("string")
  13. * @Assert\MinLength(3)
  14. */
  15. protected $string;
  16. /**
  17. * @see SearcherInterface
  18. * @param array $params
  19. */
  20. public function init($params)
  21. {
  22. // Control des parametres transmis.
  23. $this->checkParams($params, array(
  24. 'string' => "Muzich\CoreBundle\Searcher\UserAndGroupSearch::init(): \$params: Un string est nécéssaire"
  25. ));
  26. // Mise a jour des attributs
  27. $this->setAttributes(array('string', 'min_lenght'), $params);
  28. }
  29. /**
  30. * @see SearcherInterface
  31. * @param array $params
  32. */
  33. public function update($params)
  34. {
  35. // Mise a jour des attributs
  36. $this->setAttributes(array(
  37. 'string', 'min_length'
  38. ), $params);
  39. }
  40. /**
  41. * @see SearcherInterface
  42. *
  43. * @return array
  44. */
  45. public function getParams()
  46. {
  47. return array(
  48. 'string' => $this->string,
  49. 'min_length' => $this->min_length
  50. );
  51. }
  52. public function getString()
  53. {
  54. return $this->string;
  55. }
  56. public function setString($string)
  57. {
  58. $this->string = $string;
  59. }
  60. /**
  61. * Retourne les user et groupes correspondant a la recherche
  62. *
  63. * @param Registry $doctrine
  64. * @return array
  65. */
  66. public function getResults(Registry $doctrine)
  67. {
  68. // On remplace le caratcère '%' au cas ou un malin l'insére.
  69. $string = str_replace('%', '#', $this->string);
  70. $users = $doctrine
  71. ->getRepository('MuzichCoreBundle:User')
  72. ->findByString($string)
  73. ->execute()
  74. ;
  75. $groups = $doctrine
  76. ->getRepository('MuzichCoreBundle:Group')
  77. ->findByString($string)
  78. ->execute()
  79. ;
  80. return array(
  81. 'users' => $users,
  82. 'groups' => $groups
  83. );
  84. }
  85. }