ptut-vhost/build/api/core/Response.php

139 lines
3.0 KiB
PHP
Raw Normal View History

2018-02-17 17:18:58 +00:00
<?php
/**************************
* API Response *
* 08-12-2016 *
***************************
* Designed & Developed by *
* xdrm-brackets *
***************************
* https://xdrm.io/ *
**************************/
namespace api\core;
use \error\core\Error;
use \error\core\Err;
class Response{
// Attributs prives utiles (initialisation)
2018-03-11 15:14:12 +00:00
/** @var array */
2018-02-17 17:18:58 +00:00
private $data;
2018-03-11 15:14:12 +00:00
/** @var Error */
2018-02-17 17:18:58 +00:00
public $error;
2018-03-11 15:14:12 +00:00
/** CONSTRUCTEUR D'UNE REPONSE DE MODULE
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @param Error $error Erreur passee par la requete (si existe)
2018-02-17 17:18:58 +00:00
*
*/
2018-03-11 15:14:12 +00:00
public function __construct(?Error $error=null){
2018-02-17 17:18:58 +00:00
if( !( $error instanceof Error ) )
$error = new Error(Err::Success);
$this->data = [];
$this->error = $error;
}
2018-03-11 15:14:12 +00:00
/** AJOUTE UNE DONNEE A LA REPONSE
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @param String $key Le nom de la valeur a ajouter
* @param mixed $value La valeur a ajouter
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @return Response
2018-02-17 17:18:58 +00:00
*/
2018-03-11 15:14:12 +00:00
public function append(String $key, $value) : Response{
2018-02-17 17:18:58 +00:00
// Ajoute une entree pour la cle @key et de valeur @value
$this->data[$key] = $value;
return $this;
}
2018-03-11 15:14:12 +00:00
/** AJOUTE TOUTES LES DONNEES A LA REPONSE
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @param array $dataset Le tableau associatif correspondant a la reponse
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @return Response
2018-02-17 17:18:58 +00:00
*/
2018-03-11 15:14:12 +00:00
public function appendAll(array $dataset) : Response{
2018-02-17 17:18:58 +00:00
// Si ce n'est pas un tableau, on ne fais rien
if( !is_array($dataset) )
return $this;
// Si une valeur contient une erreur
if( array_key_exists('error', $dataset) && $dataset['error'] instanceof Error){
// On definit cette erreur
$this->error = $dataset['error'];
// On enleve cette entree des donnees
unset($dataset['error']);
}
// Ajoute une entree pour la cle @key et de valeur @value
$this->data = $dataset;
return $this;
}
2018-03-11 15:14:12 +00:00
/** RECUPERE UNE DONNEE DE LA REPONSE
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @param String $key Le nom de la valeur a recuperer
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @return mixed|null La valeur a cette cle, NULL si aucune valeur pour cette cle
2018-02-17 17:18:58 +00:00
*
*/
2018-03-11 15:14:12 +00:00
public function get(String $key){
2018-02-17 17:18:58 +00:00
// 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];
}
2018-03-11 15:14:12 +00:00
/** RECUPERE TOUTES LES DONNEES DE LA REPONSE
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @return array Les donnees de la reponse
2018-02-17 17:18:58 +00:00
*
*/
public function getAll(){
// Sinon, on retourne la valeur associee
return $this->data;
}
2018-03-11 15:14:12 +00:00
/** SERIALISATION A PARTIR DES DONNEES
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @return String Retourne les donnees serialisees
2018-02-17 17:18:58 +00:00
*
*/
2018-03-11 15:14:12 +00:00
public function serialize() : String{
2018-02-17 17:18:58 +00:00
// Code Http
$this->error->setHttpCode();
// Type de contenu
// header('Content-Type: application/json; charset=utf-8');
2018-02-17 17:18:58 +00:00
// On rajoute l'erreur au message
$returnData = array_merge([
'error' => $this->error->get(),
'ErrorDescription' => $this->error->explicit()
],
$this->data
);
return json_encode($returnData);
}
}
?>