Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 838 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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