2212 |
arthur |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
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 |
*
|
|
|
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>
|
|
|
17 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
|
|
18 |
*/
|
|
|
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()) {
|
|
|
38 |
foreach ($this->erreurs as $erreur) {
|
|
|
39 |
$this->resultatService->corps .= $erreur['message']."\n";
|
|
|
40 |
}
|
|
|
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 |
|
|
|
80 |
}
|