Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 22 | Details | Compare with Previous | Last modification | View Log | RSS feed

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