Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 111 | Rev 365 | Go to most recent revision | Details | Compare with Previous | 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
 
111 jpm 7
	public function __construct() {
8
		$this->resultatService = new ResultatService();
9
	}
10
 
110 jpm 11
	public function setResultatService($resultat) {
12
		if (!($resultat instanceof ResultatService)) {
13
			$this->resultatService->corps = $resultat;
14
		} else {
15
			$this->resultatService = $resultat;
16
		}
17
	}
18
 
19
	public function getCorps() {
20
		if ($this->etreEnErreur()) {
111 jpm 21
			$this->resultatService->corps = $this->erreurs[0]['message'];
110 jpm 22
		} else {
23
			$this->transformerReponseCorpsSuivantMime();
24
		}
25
		return $this->resultatService->corps;
26
	}
27
 
28
	public function ajouterErreur(Exception $e) {
29
		$this->erreurs[] = array('entete' => $e->getCode(), 'message' => $e->getMessage());
30
	}
31
 
32
	public function emettreLesEntetes() {
33
		$enteteHttp = new EnteteHttp();
34
		if ($this->etreEnErreur()) {
35
			$enteteHttp->code = $this->erreurs[0]['entete'];
36
			$enteteHttp->mime = 'text/html';
37
		} else {
38
			$enteteHttp->encodage = $this->resultatService->encodage;
39
			$enteteHttp->mime = $this->resultatService->mime;
40
		}
41
		header("Content-Type: $enteteHttp->mime; charset=$enteteHttp->encodage");
42
		RestServeur::envoyerEnteteStatutHttp($enteteHttp->code);
43
	}
44
 
45
	private function etreEnErreur() {
46
		$enErreur = false;
47
		if (count($this->erreurs) > 0) {
48
			$enErreur = true;
49
		}
50
		return $enErreur;
51
	}
52
 
53
	private function transformerReponseCorpsSuivantMime() {
54
		switch ($this->resultatService->mime) {
55
			case 'application/json' :
312 jpm 56
				if (isset($_GET['callback'])) {
57
					$contenu = $_GET['callback'].'('.json_encode($donnees).');';
58
				} else {
59
					$contenu = json_encode($this->resultatService->corps);
60
				}
61
				$this->resultatService->corps = $contenu;
110 jpm 62
				break;
63
		}
64
	}
65
 
66
}
67
?>