Browse Source

Eture des classe de recherche.

bastien 12 years ago
parent
commit
0780ca3b82

+ 113 - 0
src/Muzich/CoreBundle/Searcher/ElementSearcher.php View File

@@ -0,0 +1,113 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Searcher;
4
+
5
+class ElementSearcher extends Searcher implements SearcherInterface
6
+{
7
+  
8
+  /**
9
+   * Constante définissant si la recherche porte sur le réseau public
10
+   * ou sur le réseau personel de l'utilisateur.
11
+   */
12
+  const NETWORK_PUBLIC = 'network_public';
13
+  const NETWORK_PERSONAL = 'network_personal';
14
+  
15
+  /**
16
+   * Réseau sur lequel porte la recherche
17
+   * 
18
+   * @var string
19
+   */
20
+  protected $network = self::NETWORK_PUBLIC;
21
+  
22
+  /**
23
+   * Liste des tags utilisés lors de la recherche.
24
+   * 
25
+   * @var array
26
+   */
27
+  protected $tags = Array();
28
+  
29
+  /**
30
+   * Nombre limite de résultats retournés.
31
+   * TODO: Placer cette info dans la config.
32
+   * 
33
+   * @var int
34
+   */
35
+  protected $count = 20;
36
+  
37
+  /**
38
+   * Objet requete
39
+   * 
40
+   * @var  
41
+   */
42
+  protected $query = null;
43
+  
44
+  /**
45
+   * Liste des Element Résultats
46
+   * 
47
+   * @var array
48
+   */
49
+  protected $results = array();
50
+  
51
+  /**
52
+   * @see SearcherInterface
53
+   * @param array $params 
54
+   */
55
+  public function init($params)
56
+  {
57
+    // Control des parametres transmis.
58
+    $this->checkParams($params, array(
59
+      'tags' => "Muzich\CoreBundle\Searcher\ElementSearch::init(): \$params: Au moins un tag est nécéssaire"
60
+    ));
61
+    
62
+    // Mise a jour des attributs
63
+    $this->setAttributes(array(
64
+      'network', 'tags', 'count'
65
+    ), $params);
66
+    
67
+  }
68
+  
69
+  /**
70
+   * @see SearcherInterface
71
+   * @param array $params 
72
+   */
73
+  public function update($params)
74
+  {
75
+    // Mise a jour des attributs
76
+    $this->setAttributes(array(
77
+      'network', 'tags', 'count'
78
+    ), $params);
79
+  }
80
+  
81
+  /**
82
+   * @see SearcherInterface
83
+   */
84
+  public function constructQueryObject()
85
+  {
86
+    
87
+  }
88
+  
89
+  /**
90
+   * @see SearcherInterface
91
+   * @return 
92
+   */
93
+  public function getQueryObject()
94
+  {
95
+    if (!$this->query)
96
+    {
97
+      $this->constructQueryObject();
98
+    }
99
+    
100
+    return $this->query;
101
+  }
102
+  
103
+  public function getResults()
104
+  {
105
+    if (!$this->query)
106
+    {
107
+      $this->constructQueryObject();
108
+    }
109
+    
110
+    //...
111
+  }
112
+  
113
+}

+ 32 - 0
src/Muzich/CoreBundle/Searcher/Searcher.php View File

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Searcher;
4
+
5
+/**
6
+ * Objet utiliser pour cadrer la recherche.
7
+ * 
8
+ */
9
+class Searcher
10
+{
11
+  protected function checkParams($params, $neededs)
12
+  {
13
+    foreach ($neededs as $config_id => $message)
14
+    {
15
+      if (!array_key_exists($config_id, $params))
16
+      {
17
+        throw new \Exception($message);
18
+      }
19
+    }
20
+  }
21
+  
22
+  protected function setAttributes($params_ids, $params)
23
+  {
24
+    foreach ($params_ids as $param_id)
25
+    {
26
+      if (array_key_exists($param_id, $params))
27
+      {
28
+        $this->$param_id = $params[$param_id];
29
+      }
30
+    }
31
+  }
32
+}

+ 38 - 0
src/Muzich/CoreBundle/Searcher/SearcherInterface.php View File

@@ -0,0 +1,38 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Searcher;
4
+
5
+/**
6
+ * Interface pour les classes de recherche.
7
+ * 
8
+ */
9
+interface SearcherInterface
10
+{
11
+  
12
+  /**
13
+   * Initialisation de l'objet recherche.
14
+   */
15
+  public function init($params);
16
+  
17
+  /**
18
+   * Mise a jour des composant de la recherche.
19
+   */
20
+  public function update($params);
21
+  
22
+  /**
23
+   * Procédure qui construit la requete.
24
+   */
25
+  public function constructQueryObject();
26
+  
27
+  /**
28
+   * Récupération de l'objet requete, pour ajouter des JOIN 
29
+   * par exemple.
30
+   */
31
+  public function getQueryObject();
32
+  
33
+  /**
34
+   * Récupération des résultats.
35
+   */
36
+  public function getResults();
37
+  
38
+}

+ 9 - 0
src/Muzich/HomeBundle/Controller/HomeController.php View File

@@ -6,6 +6,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6 6
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7 7
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8 8
 
9
+use Muzich\CoreBundle\Searcher\ElementSearcher;
9 10
 
10 11
 class HomeController extends Controller
11 12
 {
@@ -16,6 +17,14 @@ class HomeController extends Controller
16 17
   public function indexAction()
17 18
   {        
18 19
     $user = $this->container->get('security.context')->getToken()->getUser();
20
+    $s = new ElementSearcher();
21
+    $s->init(array(
22
+      'network' => ElementSearcher::NETWORK_PUBLIC,
23
+      'tags' => array('toto', 'pipi'),
24
+      'count' => 30
25
+    ));
26
+    
27
+    die(var_dump($s));
19 28
     
20 29
     return array('user' => $user);
21 30
   }