PSR12
This commit is contained in:
87
src/Rest.php
87
src/Rest.php
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/** DomFramework
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
@@ -11,55 +12,57 @@ namespace Domframework;
|
||||
*/
|
||||
class Rest
|
||||
{
|
||||
/** Allowed types by default
|
||||
*/
|
||||
public $allowedtypes = array ("json", "xml", "txt", "csv");
|
||||
/** Allowed types by default
|
||||
*/
|
||||
public $allowedtypes = array("json", "xml", "txt", "csv");
|
||||
|
||||
/** Converted types
|
||||
*/
|
||||
private $convert = array (
|
||||
/** Converted types
|
||||
*/
|
||||
private $convert = array(
|
||||
"application/json" => "json",
|
||||
"application/xml" => "xml",
|
||||
"text/plain" => "txt",
|
||||
"text/csv" => "csv",
|
||||
);
|
||||
);
|
||||
|
||||
/** This method allow to choose the type of plugin to use on output
|
||||
* Based on the HTTP_ACCEPT parameter provided by the user
|
||||
* @return string like "json", "xml", "txt", "csv"
|
||||
*/
|
||||
public function chooseType ()
|
||||
{
|
||||
$type = reset ($this->allowedtypes);
|
||||
if (isset ($_SERVER["HTTP_ACCEPT"]))
|
||||
/** This method allow to choose the type of plugin to use on output
|
||||
* Based on the HTTP_ACCEPT parameter provided by the user
|
||||
* @return string like "json", "xml", "txt", "csv"
|
||||
*/
|
||||
public function chooseType()
|
||||
{
|
||||
// Must work with the complete definition to match, and by the name of
|
||||
// the output plugin at the end
|
||||
$convert = array_intersect ($this->convert, $this->allowedtypes);
|
||||
$type = array_search (reset ($this->allowedtypes), $this->convert);
|
||||
$http = new Http;
|
||||
$type = $http->bestChoice ($_SERVER["HTTP_ACCEPT"], array_keys ($convert),
|
||||
$type);
|
||||
$type = $this->convert[$type];
|
||||
$type = reset($this->allowedtypes);
|
||||
if (isset($_SERVER["HTTP_ACCEPT"])) {
|
||||
// Must work with the complete definition to match, and by the name of
|
||||
// the output plugin at the end
|
||||
$convert = array_intersect($this->convert, $this->allowedtypes);
|
||||
$type = array_search(reset($this->allowedtypes), $this->convert);
|
||||
$http = new Http();
|
||||
$type = $http->bestChoice(
|
||||
$_SERVER["HTTP_ACCEPT"],
|
||||
array_keys($convert),
|
||||
$type
|
||||
);
|
||||
$type = $this->convert[$type];
|
||||
}
|
||||
return $type;
|
||||
}
|
||||
return $type;
|
||||
}
|
||||
|
||||
/** Display the message (which can be a string, an array, an integer...)
|
||||
* into the type asked by the client if it is allowed.
|
||||
* By default, the JSON type is used
|
||||
* @param string $message Message to be displayed by the output type
|
||||
* @param integer|null $code HTTP code to use
|
||||
*/
|
||||
function display ($message, $code = 200)
|
||||
{
|
||||
$http = new Http;
|
||||
$text = $http->codetext ($code);
|
||||
header ($_SERVER["SERVER_PROTOCOL"]." $code $text");
|
||||
$type = $this->chooseType ();
|
||||
$constr = __NAMESPACE__."\\Output$type";
|
||||
$method = "out";
|
||||
$obj = new $constr ();
|
||||
$obj->$method ($message);
|
||||
}
|
||||
/** Display the message (which can be a string, an array, an integer...)
|
||||
* into the type asked by the client if it is allowed.
|
||||
* By default, the JSON type is used
|
||||
* @param string $message Message to be displayed by the output type
|
||||
* @param integer|null $code HTTP code to use
|
||||
*/
|
||||
public function display($message, $code = 200)
|
||||
{
|
||||
$http = new Http();
|
||||
$text = $http->codetext($code);
|
||||
header($_SERVER["SERVER_PROTOCOL"] . " $code $text");
|
||||
$type = $this->chooseType();
|
||||
$constr = __NAMESPACE__ . "\\Output$type";
|
||||
$method = "out";
|
||||
$obj = new $constr();
|
||||
$obj->$method($message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user