UserAndGroupSearcher.php 2.0KB

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