Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 111 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
110 jpm 1
<?php
2
class ReponseHttp {
3
 
4
	private $resultatService = null;
5
	private $erreurs = array();
6
 
7
	public function setResultatService($resultat) {
8
		if (!($resultat instanceof ResultatService)) {
9
			$this->resultatService = new ResultatService();
10
			$this->resultatService->corps = $resultat;
11
		} else {
12
			$this->resultatService = $resultat;
13
		}
14
	}
15
 
16
	public function getCorps() {
17
		if ($this->etreEnErreur()) {
18
			$this->resultatService->corps = $this->erreursHttp[0]['message'];
19
		} else {
20
			$this->transformerReponseCorpsSuivantMime();
21
		}
22
		return $this->resultatService->corps;
23
	}
24
 
25
	public function ajouterErreur(Exception $e) {
26
		$this->erreurs[] = array('entete' => $e->getCode(), 'message' => $e->getMessage());
27
	}
28
 
29
	public function emettreLesEntetes() {
30
		$enteteHttp = new EnteteHttp();
31
		if ($this->etreEnErreur()) {
32
			$enteteHttp->code = $this->erreurs[0]['entete'];
33
			$enteteHttp->mime = 'text/html';
34
		} else {
35
			$enteteHttp->encodage = $this->resultatService->encodage;
36
			$enteteHttp->mime = $this->resultatService->mime;
37
		}
38
		header("Content-Type: $enteteHttp->mime; charset=$enteteHttp->encodage");
39
		RestServeur::envoyerEnteteStatutHttp($enteteHttp->code);
40
	}
41
 
42
	private function etreEnErreur() {
43
		$enErreur = false;
44
		if (count($this->erreurs) > 0) {
45
			$enErreur = true;
46
		}
47
		return $enErreur;
48
	}
49
 
50
	private function transformerReponseCorpsSuivantMime() {
51
		switch ($this->resultatService->mime) {
52
			case 'application/json' :
53
				$this->resultatService->corps = json_encode($this->resultatService->corps);
54
				break;
55
		}
56
	}
57
 
58
}
59
?>