upd: api.core.Request (added 'default' parameter spec + fixed root (/) uri parameters)

This commit is contained in:
xdrm-brackets 2017-12-11 18:30:29 +01:00
parent e39c9cebe6
commit 15e28c10ec
3 changed files with 29 additions and 16 deletions

View File

@ -119,9 +119,8 @@
$this->buildOptions(); $this->buildOptions();
/* (8) Construction de l'objet (add http method to params) /* (8) Construction de l'objet
---------------------------------------------------------*/ ---------------------------------------------------------*/
$this->params['HTTP_METHOD'] = $this->http_method;
$this->error->set(Err::Success); $this->error->set(Err::Success);
return true; // On retourne que tout s'est bien passe return true; // On retourne que tout s'est bien passe
@ -161,9 +160,14 @@
/* (1) Verification format general /* (1) Verification format general
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) If wrong format -> exit */ /* (1) If wrong format -> exit */
if( !preg_match('@^\/[\w-]+(\/[\w-]+)*\/?$@', $uri, $matches) && $uri != '/' ) if( !preg_match('@^\/[^\/]*(\/[^\/]+)*\/?$@', $uri) )
return $this->error->set(Err::WrongPathModule); return $this->error->set(Err::WrongPathModule);
/* (2) Add ending '/' if not there */
if( $uri[strlen($uri)-1] != '/' )
$uri = "$uri/";
/* (2) Verification de l'existence du chemin (conf) /* (2) Verification de l'existence du chemin (conf)
---------------------------------------------------------*/ ---------------------------------------------------------*/
@ -174,9 +178,9 @@
foreach(Config::get()->index as $key=>$void){ foreach(Config::get()->index as $key=>$void){
$match_size = strlen($key); $match_size = strlen($key);
/* (1.1) Look for the longer match */ /* (1.1) Look for the longer ( match | '/' ) */
if( $match_size > $exists_size && substr($uri, 0, $match_size) == $key ){ if( $match_size > $exists_size && substr($uri, 0, $match_size+1) == "$key/" || $key == '/' ){
$exists_size = $match_size; $exists_size = $key == '/' ? 0 : $match_size;
$path = $key; $path = $key;
} }
@ -191,12 +195,15 @@
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Extract URI string after @path */ /* (1) Extract URI string after @path */
$uri_end = substr($uri, $exists_size); $uri_end = substr($uri, $exists_size);
/* (2) Special case: add / if root uri arguments */
if( strlen($uri_end) > 0 && $uri_end[0] != '/' )
$uri_end = "/$uri_end";
/* (2) If invalid format, return error */ /* (3) If invalid format, return error */
if( !preg_match('@^((?:\/[^\/]+)*)\/?$@', $uri_end, $uri_match) ) if( !preg_match('@^((?:\/[^\/]+)*)\/?$@', $uri_end, $uri_match) )
return $this->error->set(Err::InvalidURI); return $this->error->set(Err::InvalidURI);
/* (3) Add each URI parameter to the parameter store */ /* (4) Add each URI parameter to the parameter store */
$uri_args = array_slice( explode('/', $uri_match[1]), 1); $uri_args = array_slice( explode('/', $uri_match[1]), 1);
foreach($uri_args as $index=>$value) foreach($uri_args as $index=>$value)
@ -320,10 +327,15 @@
if( isset($config['rename']) && is_string($config['rename']) && preg_match('@^\w+$@', $config['rename']) ) if( isset($config['rename']) && is_string($config['rename']) && preg_match('@^\w+$@', $config['rename']) )
$rename = $config['rename']; $rename = $config['rename'];
/* (2) On récupère si le paramètre est optionnel ou pas */ /* (2) Gestion du paramètre DEFAULT */
$default = null;
if( isset($config['default']) && is_string($config['default']) )
$default = $config['default'];
/* (3) On récupère si le paramètre est optionnel ou pas */
$optional = isset($config['optional']) && $config['optional'] === true; $optional = isset($config['optional']) && $config['optional'] === true;
/* (3) Si de type 'FILE' + fichier existe => on enregistre la ref. */ /* (4) Si de type 'FILE' + fichier existe => on enregistre la ref. */
if( $config['type'] == 'FILE' && isset($_FILES[$name]) ) if( $config['type'] == 'FILE' && isset($_FILES[$name]) )
$this->params[$rename] = &$_FILES[$name]; $this->params[$rename] = &$_FILES[$name];
@ -337,11 +349,11 @@
/* (1) Si le paramètre est optionnel et manquant */ /* (1) Si le paramètre est optionnel et manquant */
if( $optional && !isset($this->raw_params[$name]) ){ if( $optional && !isset($this->raw_params[$name]) ){
// On le crée le param optionnel avec la valeur NULL // On le crée le param optionnel avec la valeur @default
$this->params[$rename] = null; $this->params[$rename] = $default;
/* (2) Si le paramètre est renseigné (sauf FILE) */ /* (2) Si le paramètre est renseigné (sauf FILE) */
}elseif( $config['type'] != 'FILE'){ }elseif( $config['type'] != 'FILE' ){
// Si la verification est fausse, on retourne faux // Si la verification est fausse, on retourne faux
if( !Checker::run($config['type'], $this->raw_params[$name]) ) if( !Checker::run($config['type'], $this->raw_params[$name]) )
@ -439,7 +451,6 @@
return new Response($this->error); return new Response($this->error);
} }
/* (4) On amorce la methode /* (4) On amorce la methode
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) On lance la fonction */ /* (1) On lance la fonction */

View File

@ -13,7 +13,7 @@
public function get($args){ public function get($args){
extract($args); extract($args);
return [ 'bla' => uniqid() ]; return [ 'args' => $args ];
} }

View File

@ -2,7 +2,9 @@
"GET": { "GET": {
"description": "Returns the API documentation", "description": "Returns the API documentation",
"permissions": [], "permissions": [],
"parameters": {} "parameters": {
"URL0": { "description": "Name of the project to release", "type": "alphanumeric", "rename": "var1", "optional": true, "default": "default_value" }
}
}, },
"admin": { "admin": {