Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 22 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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