GlobalSearcher.php 2.1KB

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