| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | 	namespace modules\bataille\app\controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	use core\App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	class CentreRecherche { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 		private $coef_centre; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 		//-------------------------- BUILDER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 			$dbc1 = Bataille::getDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 			$query = $dbc1->select("coef_centre_recherche")->from("configuration")->where("ID_configuration", "=", 1)->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 			if ((is_array($query)) && (count($query) == 1)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 				foreach ($query as $obj) $this->coef_centre = $obj->coef_centre_recherche; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 			$query = $dbc1->select()->from("recherche") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 				->where("niveau_centre", "<=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 				->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			if ((is_array($query)) && (count($query) > 0)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 				foreach ($query as $obj) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 					$niveau = $this->getNiveauRecherche($obj->recherche, $obj->type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 					$cout = unserialize($obj->cout); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 					//si niveau == 0 ca veut dire que la recherche n'a pas encore été effectuée dans la base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 					if ($niveau > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 						$cout = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 							"eau" => $cout["eau"] * ($this->coef_centre * $niveau), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 							"electricite" => $cout["electricite"] * ($this->coef_centre * $niveau), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 							"fer" => $cout["fer"] * ($this->coef_centre * $niveau), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 							"fuel" => $cout["fuel"] * ($this->coef_centre * $niveau) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 						]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 					$recherhce[] = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 						"recherche" => $obj->recherche, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 						"type" => $obj->type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 						"niveau" => $niveau, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 						"cout" => $cout | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 					]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			Bataille::setValues(["centre_recherche" => $recherhce]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		 * @param $recherche | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		 * @param $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		 * fonction qui va cehrcher le niveau de la recherche actuelle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		 * renvoi 0 si elle n'a pas été trouvée | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  | 		 */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 60 |  | View Code Duplication | 		private function getNiveauRecherche($recherche, $type) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 			$dbc = App::getDb(); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 			$query = $dbc->select("niveau") | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 				->from("_bataille_centre_recherche") | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 				->where("ID_base", "=", Bataille::getIdBase(), "AND") | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 				->where("recherche", "=", $recherche, "AND") | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 				->where("type", "=", $type) | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 				->get(); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 			if ((is_array($query)) && (count($query) > 0)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 				foreach ($query as $obj) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 					return $obj->niveau; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 				} | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 			return 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		//-------------------------- END BUILDER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		//-------------------------- GETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		 * @param $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		 * @return array|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		 * permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		 * d'un type donné | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		public function getAllRechercheType($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			$dbc = App::getDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			$query = $dbc->select()->from("_bataille_centre_recherche")->where("type", "=", $type)->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			if ((is_array($query)) && (count($query) > 0)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 				foreach ($query as $obj) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 					$recherche[] = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 						"niveau" => $obj->niveau, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 						"recherche" => $obj->recherche | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 					]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 				return $recherche; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		//-------------------------- END GETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		//-------------------------- SETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		//-------------------------- END SETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | 		 | 
            
                                                        
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.