2016-07-02 15:10:41 +00:00
|
|
|
<?php
|
2016-02-04 22:45:03 +00:00
|
|
|
|
2016-10-18 14:03:03 +00:00
|
|
|
namespace api\core;
|
2016-10-18 17:09:47 +00:00
|
|
|
use \error\core\Error;
|
2017-01-30 17:39:21 +00:00
|
|
|
use \error\core\Err;
|
2016-02-04 22:45:03 +00:00
|
|
|
|
2017-01-30 17:39:21 +00:00
|
|
|
class Response{
|
2016-02-04 22:45:03 +00:00
|
|
|
|
|
|
|
// Attributs prives utiles (initialisation)
|
|
|
|
private $data;
|
|
|
|
public $error;
|
|
|
|
|
|
|
|
|
|
|
|
/* CONSTRUCTEUR D'UNE REPONSE DE MODULE
|
|
|
|
*
|
2016-02-05 08:10:37 +00:00
|
|
|
* @error<ModuleError> Erreur passee par la requete (si existe)
|
|
|
|
*
|
2016-02-04 22:45:03 +00:00
|
|
|
*/
|
2017-01-30 17:39:21 +00:00
|
|
|
public function __construct($error=null){
|
|
|
|
if( !( $error instanceof Error ) )
|
|
|
|
$error = new Error(Err::Success);
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
$this->data = [];
|
2016-02-05 08:10:37 +00:00
|
|
|
$this->error = $error;
|
2016-02-04 22:45:03 +00:00
|
|
|
}
|
2017-01-30 17:39:21 +00:00
|
|
|
/* AJOUTE UNE DONNEE A LA REPONSE
|
2016-02-04 22:45:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
*
|
2016-02-04 23:07:03 +00:00
|
|
|
* @key<String> Le nom de la valeur a ajouter
|
2016-02-05 07:54:21 +00:00
|
|
|
* @value<mixed*> La valeur a ajouter
|
2016-02-04 22:45:03 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-02-04 23:07:03 +00:00
|
|
|
public function append($key, $value){
|
|
|
|
// Ajoute une entree pour la cle @key et de valeur @value
|
|
|
|
$this->data[$key] = $value;
|
2016-02-04 22:45:03 +00:00
|
|
|
|
2016-02-04 23:07:03 +00:00
|
|
|
return $this;
|
|
|
|
}
|
2016-02-04 22:45:03 +00:00
|
|
|
|
|
|
|
|
2016-02-04 23:07:03 +00:00
|
|
|
/* AJOUTE TOUTES LES DONNEES A LA REPONSE
|
2016-02-04 22:45:03 +00:00
|
|
|
*
|
2016-02-04 23:07:03 +00:00
|
|
|
* @dataset<Array> Le tableau associatif correspondant a la reponse
|
2016-02-04 22:45:03 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-02-04 23:07:03 +00:00
|
|
|
public function appendAll($dataset){
|
2016-02-12 23:04:00 +00:00
|
|
|
// Si ce n'est pas un tableau, on ne fais rien
|
2017-01-30 17:39:21 +00:00
|
|
|
if( !is_array($dataset) )
|
|
|
|
return $this;
|
2016-02-12 23:04:00 +00:00
|
|
|
|
|
|
|
// Si une valeur contient une erreur
|
2017-01-30 17:39:21 +00:00
|
|
|
if( array_key_exists('error', $dataset) && $dataset['error'] instanceof Error){
|
2016-02-12 23:04:00 +00:00
|
|
|
// On definit cette erreur
|
2017-01-30 17:39:21 +00:00
|
|
|
$this->error = $dataset['error'];
|
2016-02-12 23:04:00 +00:00
|
|
|
// On enleve cette entree des donnees
|
2017-01-30 17:39:21 +00:00
|
|
|
unset($dataset['error']);
|
2016-02-12 23:04:00 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:07:03 +00:00
|
|
|
// Ajoute une entree pour la cle @key et de valeur @value
|
|
|
|
$this->data = $dataset;
|
2016-02-04 22:45:03 +00:00
|
|
|
|
2016-02-04 23:07:03 +00:00
|
|
|
return $this;
|
2016-02-04 22:45:03 +00:00
|
|
|
}
|
2017-01-30 17:39:21 +00:00
|
|
|
/* RECUPERE UNE DONNEE DE LA REPONSE
|
2016-02-04 22:45:03 +00:00
|
|
|
|
|
|
|
|
2016-02-05 07:54:21 +00:00
|
|
|
*
|
|
|
|
* @key<String> Le nom de la valeur a recuperer
|
|
|
|
*
|
|
|
|
* @return value<mixed*> La valeur a cette cle
|
|
|
|
* @return error<null> Retourne NULL si aucune valeur pour cette cle
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function get($key){
|
|
|
|
// Si la valeur de cle @key n'existe pas, on retourne NULL
|
|
|
|
if( !isset($this->data[$key]) )
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Sinon, on retourne la valeur associee
|
|
|
|
return $this->data[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RECUPERE TOUTES LES DONNEES DE LA REPONSE
|
|
|
|
*
|
|
|
|
* @return data<Array> Les donnees de la reponse
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getAll(){
|
|
|
|
// Sinon, on retourne la valeur associee
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-04 23:07:03 +00:00
|
|
|
/* SERIALISATION A PARTIR DES DONNEES
|
2016-02-04 22:45:03 +00:00
|
|
|
*
|
2016-02-04 23:07:03 +00:00
|
|
|
* @return json<String> Retourne les donnees serialisees
|
2016-02-04 22:45:03 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-02-04 23:07:03 +00:00
|
|
|
public function serialize(){
|
2016-02-04 22:45:03 +00:00
|
|
|
|
2016-07-02 15:10:41 +00:00
|
|
|
// Code Http
|
2017-01-30 17:39:21 +00:00
|
|
|
$this->error->setHttpCode();
|
2016-07-02 15:10:41 +00:00
|
|
|
|
|
|
|
// Type de contenu
|
2017-01-30 17:39:21 +00:00
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
2016-07-02 15:10:41 +00:00
|
|
|
|
2016-02-08 20:46:11 +00:00
|
|
|
// On rajoute l'erreur au message
|
2016-07-04 13:45:29 +00:00
|
|
|
$returnData = array_merge([
|
2017-01-30 17:39:21 +00:00
|
|
|
'error' => $this->error->get(),
|
|
|
|
'ErrorDescription' => $this->error->explicit()
|
2016-07-04 13:45:29 +00:00
|
|
|
],
|
2016-07-02 15:10:41 +00:00
|
|
|
$this->data
|
|
|
|
);
|
2016-02-05 08:10:37 +00:00
|
|
|
|
|
|
|
return json_encode($returnData);
|
2016-02-04 22:45:03 +00:00
|
|
|
|
|
|
|
}
|
2017-01-30 17:39:21 +00:00
|
|
|
}
|
2016-02-04 22:45:03 +00:00
|
|
|
|
2016-07-02 15:10:41 +00:00
|
|
|
?>
|