Subversion Repositories eFlore/Applications.moissonnage

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

class ReponseHttp {

        private $mime = 'application/json';
        private $encodage = 'utf-8';
        private $codeHttp = RestServeur::HTTP_CODE_OK;
        
        private $corps = '';
        
        private $erreurs = array();

        
        public function __construct() {
                if (function_exists('json_decode') == false){
                        require_once (dirname(__FILE__).'/JSON.php');
                        function json_decode($content, $assoc = false){
                                if ($assoc) {
                                        $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
                                } else {
                                        $json = new Services_JSON;
                                }
                                return $json->decode($content);
                        }
                }

                if ( !function_exists('json_encode') ){
                        function json_encode($content){
                                $json = new Services_JSON;
                                return $json->encode($content);
                        }
                }
        }

        
        public function setResultatService($resultat) {
                $this->corps = $resultat;
        }

        public function getCorps() {
                if ($this->etreEnErreur()) {
                        $this->corps = $this->erreurs[0]['message'];
                } else {
                        $this->transformerReponseCorpsSuivantMime();
                }
                return $this->corps;
        }

        public function ajouterErreur(Exception $e) {
                $this->erreurs[] = array('entete' => $e->getCode(), 'message' => $e->getMessage());
        }

        public function emettreLesEntetes() {
                if ($this->etreEnErreur()) {
                        $codeHttp = $this->erreurs[0]['entete'];
                        $encodage = 'utf-8';
                        $mime = 'text/html';
                } else {
                        $codeHttp = $this->codeHttp;
                        $encodage = $this->encodage;
                        $mime = $this->mime;
                }
                header("Content-Type: $mime; charset=$encodage");
                RestServeur::envoyerEnteteStatutHttp($codeHttp);
        }

        private function etreEnErreur() {
                $enErreur = false;
                if (count($this->erreurs) > 0) {
                        $enErreur = true;
                }
                return $enErreur;
        }

        private function transformerReponseCorpsSuivantMime() {
                switch ($this->mime) {
                        case 'application/json' :
                                if (isset($_GET['callback'])) {
                                        $contenu = $_GET['callback'].'('.json_encode($this->corps).');';
                                } else {
                                        $contenu = json_encode($this->corps);
                                }
                                $this->corps = $contenu;
                                break;
                }
        }

}

?>