| 700 | gduche | 1 | <?php
 | 
        
           | 1815 | jpm | 2 | // declare(encoding='UTF-8');
 | 
        
           | 1599 | jpm | 3 | /**
 | 
        
           | 1793 | jpm | 4 |  * Classe créant la réponse HTTP pour les services de DEL.
 | 
        
           |  |  | 5 |  *
 | 
        
           |  |  | 6 |  * Vérifie qu'aucune erreur n'a été générée. Si une erreur existe, retourne le contenu de l'erreur.
 | 
        
           |  |  | 7 |  *
 | 
        
           | 1815 | jpm | 8 |  * @category  DEL
 | 
        
           |  |  | 9 |  * @package   Services
 | 
        
           |  |  | 10 |  * @package   Bibliotheque
 | 
        
           |  |  | 11 |  * @version   0.1
 | 
        
           |  |  | 12 |  * @author    Mathias CHOUET <mathias@tela-botanica.org>
 | 
        
           |  |  | 13 |  * @author    Jean-Pascal MILCENT <jpm@tela-botanica.org>
 | 
        
           |  |  | 14 |  * @author    Aurelien PERONNET <aurelien@tela-botanica.org>
 | 
        
           |  |  | 15 |  * @license   GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 | 
        
           |  |  | 16 |  * @license   CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 | 
        
           | 1793 | jpm | 17 |  * @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
 | 
        
           | 1815 | jpm | 18 |  */
 | 
        
           | 700 | gduche | 19 | class ReponseHttp {
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | 	private $resultatService = null;
 | 
        
           |  |  | 22 | 	private $erreurs = array();
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | 	public function __construct() {
 | 
        
           |  |  | 25 | 		$this->resultatService = new ResultatService();
 | 
        
           |  |  | 26 | 	}
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | 	public function setResultatService($resultat) {
 | 
        
           |  |  | 29 | 		if (!($resultat instanceof ResultatService)) {
 | 
        
           |  |  | 30 | 			$this->resultatService->corps = $resultat;
 | 
        
           |  |  | 31 | 		} else {
 | 
        
           |  |  | 32 | 			$this->resultatService = $resultat;
 | 
        
           |  |  | 33 | 		}
 | 
        
           |  |  | 34 | 	}
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 | 	public function getCorps() {
 | 
        
           |  |  | 37 | 		if ($this->etreEnErreur()) {
 | 
        
           | 1599 | jpm | 38 | 			foreach ($this->erreurs as $erreur) {
 | 
        
           |  |  | 39 | 				$this->resultatService->corps .= $erreur['message']."\n";
 | 
        
           |  |  | 40 | 			}
 | 
        
           | 700 | gduche | 41 | 		} else {
 | 
        
           |  |  | 42 | 			$this->transformerReponseCorpsSuivantMime();
 | 
        
           |  |  | 43 | 		}
 | 
        
           |  |  | 44 | 		return $this->resultatService->corps;
 | 
        
           |  |  | 45 | 	}
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | 	public function ajouterErreur(Exception $e) {
 | 
        
           |  |  | 48 | 		$this->erreurs[] = array('entete' => $e->getCode(), 'message' => $e->getMessage());
 | 
        
           |  |  | 49 | 	}
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 | 	public function emettreLesEntetes() {
 | 
        
           |  |  | 52 | 		$enteteHttp = new EnteteHttp();
 | 
        
           |  |  | 53 | 		if ($this->etreEnErreur()) {
 | 
        
           |  |  | 54 | 			$enteteHttp->code = $this->erreurs[0]['entete'];
 | 
        
           |  |  | 55 | 			$enteteHttp->mime = 'text/html';
 | 
        
           |  |  | 56 | 		} else {
 | 
        
           |  |  | 57 | 			$enteteHttp->encodage = $this->resultatService->encodage;
 | 
        
           |  |  | 58 | 			$enteteHttp->mime = $this->resultatService->mime;
 | 
        
           |  |  | 59 | 		}
 | 
        
           |  |  | 60 | 		header("Content-Type: $enteteHttp->mime; charset=$enteteHttp->encodage");
 | 
        
           |  |  | 61 | 		RestServeur::envoyerEnteteStatutHttp($enteteHttp->code);
 | 
        
           |  |  | 62 | 	}
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | 	private function etreEnErreur() {
 | 
        
           |  |  | 65 | 		$enErreur = false;
 | 
        
           |  |  | 66 | 		if (count($this->erreurs) > 0) {
 | 
        
           |  |  | 67 | 			$enErreur = true;
 | 
        
           |  |  | 68 | 		}
 | 
        
           |  |  | 69 | 		return $enErreur;
 | 
        
           |  |  | 70 | 	}
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | 	private function transformerReponseCorpsSuivantMime() {
 | 
        
           |  |  | 73 | 		switch ($this->resultatService->mime) {
 | 
        
           |  |  | 74 | 			case 'application/json' :
 | 
        
           |  |  | 75 | 				$this->resultatService->corps = json_encode($this->resultatService->corps);
 | 
        
           |  |  | 76 | 				break;
 | 
        
           |  |  | 77 | 		}
 | 
        
           |  |  | 78 | 	}
 | 
        
           |  |  | 79 |   | 
        
           | 1793 | jpm | 80 | }
 |