Subversion Repositories eFlore/Projets.eflore-projets

Rev

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
 
111 jpm 7
	public function __construct() {
8
		$this->resultatService = new ResultatService();
365 aurelien 9
		if ( !function_exists('json_decode') ){
10
			require_once (dirname(__FILE__).'/JSON.php');
11
			function json_decode($content, $assoc=false){
12
				if ( $assoc ){
13
					$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
14
				} else {
15
					$json = new Services_JSON;
16
				}
17
				return $json->decode($content);
18
			}
19
		}
420 jpm 20
 
365 aurelien 21
		if ( !function_exists('json_encode') ){
22
			function json_encode($content){
23
				$json = new Services_JSON;
24
				return $json->encode($content);
25
			}
26
		}
111 jpm 27
	}
28
 
110 jpm 29
	public function setResultatService($resultat) {
30
		if (!($resultat instanceof ResultatService)) {
31
			$this->resultatService->corps = $resultat;
32
		} else {
33
			$this->resultatService = $resultat;
34
		}
35
	}
36
 
37
	public function getCorps() {
38
		if ($this->etreEnErreur()) {
111 jpm 39
			$this->resultatService->corps = $this->erreurs[0]['message'];
110 jpm 40
		} else {
41
			$this->transformerReponseCorpsSuivantMime();
42
		}
43
		return $this->resultatService->corps;
44
	}
45
 
46
	public function ajouterErreur(Exception $e) {
47
		$this->erreurs[] = array('entete' => $e->getCode(), 'message' => $e->getMessage());
48
	}
49
 
50
	public function emettreLesEntetes() {
51
		$enteteHttp = new EnteteHttp();
52
		if ($this->etreEnErreur()) {
53
			$enteteHttp->code = $this->erreurs[0]['entete'];
54
			$enteteHttp->mime = 'text/html';
55
		} else {
56
			$enteteHttp->encodage = $this->resultatService->encodage;
57
			$enteteHttp->mime = $this->resultatService->mime;
58
		}
59
		header("Content-Type: $enteteHttp->mime; charset=$enteteHttp->encodage");
60
		RestServeur::envoyerEnteteStatutHttp($enteteHttp->code);
61
	}
62
 
63
	private function etreEnErreur() {
64
		$enErreur = false;
65
		if (count($this->erreurs) > 0) {
66
			$enErreur = true;
67
		}
68
		return $enErreur;
69
	}
70
 
71
	private function transformerReponseCorpsSuivantMime() {
72
		switch ($this->resultatService->mime) {
73
			case 'application/json' :
312 jpm 74
				if (isset($_GET['callback'])) {
420 jpm 75
					$contenu = $_GET['callback'].'('.json_encode($this->resultatService->corps).');';
312 jpm 76
				} else {
77
					$contenu = json_encode($this->resultatService->corps);
78
				}
79
				$this->resultatService->corps = $contenu;
110 jpm 80
				break;
81
		}
82
	}
83
 
84
}
85
?>