Subversion Repositories eFlore/Applications.moissonnage

Rev

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

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