From a2828fbd63b54f2848b7eece0585b317124119c7 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 13 Dec 2017 13:00:20 +0100 Subject: [PATCH] Added api.core.Documentation (human-readable parameter list) --- build/api/core/Documentation.php | 95 +++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 8 deletions(-) diff --git a/build/api/core/Documentation.php b/build/api/core/Documentation.php index 9d1f0e9..abf7dce 100644 --- a/build/api/core/Documentation.php +++ b/build/api/core/Documentation.php @@ -40,7 +40,13 @@ $cfg[$method]['uri_scheme'] = self::uri_scheme($rq->get('id')['path'], $spec); /* (2) Build human-readable permission list */ - $cfg[$method]['perm'] = "accessible with:".self::permissions($spec); + $cfg[$method]['permissions'] = "accessible with:".self::permissions($spec); + unset($cfg[$method]['per']); + + /* (3) Build ease parameter list */ + $cfg[$method]['parameters'] = self::parameters($spec); + unset($cfg[$method]['par']); + } @@ -96,19 +102,16 @@ $pspec = $spec['par']["URL$i"]; - /* (2.2) Define the 'optional' property */ - $optional = ( isset($pspec['opt']) && $pspec['opt'] === true ) ? '?' : ''; - - /* (2.3) If 'rename' set the rename content */ + /* (2.2) If 'rename' set the rename content */ if( isset($pspec['ren']) && is_string($pspec['ren']) ){ - $uri .= '/@'.$pspec['ren']."$optional"; + $uri .= '/@'.$pspec['ren']; continue; } - /* (2.4) If no rename set the default name */ - $uri .= "/@url$i$optional"; + /* (2.3) If no rename set the default name */ + $uri .= "/@url$i"; } return $uri; @@ -161,6 +164,82 @@ } + /* (5) Builds a readable parameter list + * + * @spec Specification + * + ---------------------------------------------------------*/ + private static function parameters($spec=null){ + + /* (1) If no param return nothing */ + if( !isset($spec['par']) || !is_array($spec['par']) || count($spec['par']) <= 0 ) + return; + + /* (2) Initialize parameter output */ + $param = [ + 'GET' => [], + 'POST' => [] + ]; + $post_index = -1; + + + + /* (1) Set 'URL' parameter available in the spec */ + foreach($spec['par'] as $pname=>$pspec){ + + + /* (1) Manage POST parameters + ---------------------------------------------------------*/ + if( strlen($pname) >= 3 && substr($pname, 0, 3) == 'URL' && is_numeric(substr($pname, 3)) ){ + + /* (1) Get the URL index (position) */ + $index = intval(substr($pname,3)); + + /* (2) Manage 'rename' property */ + $name = ( isset($pspec['ren']) && is_string($pspec['ren']) ) ? $pspec['ren'] : $pname; + + /* (3) Set default values */ + $param['GET'][$index] = [ + 'name' => $name, + 'required' => !isset($pspec['opt']) || $pspec['opt'] !== true + ]; + + /* (4) Manage 'default' property */ + if( !$param['GET'][$index]['required'] ) + $param['GET'][$index]['default'] = isset($pspec['def']) ? $pspec['def'] : null; + + continue; + } + + /* (2) Manage GET+POST parameters + ---------------------------------------------------------*/ + /* (1) Get the POST index */ + $post_index = $post_index + 1; + + /* (2) Manage 'rename' property */ + $name = ( isset($pspec['ren']) && is_string($pspec['ren']) ) ? $pspec['ren'] : $pname; + + /* (3) Set default values */ + $param['POST'][$post_index] = [ + 'name' => $name, + 'required' => !isset($pspec['opt']) || $pspec['opt'] !== true + ]; + + /* (4) Manage 'default' property */ + if( !$param['POST'][$post_index]['required'] ) + $param['POST'][$post_index]['default'] = isset($pspec['def']) ? $pspec['def'] : null; + + + } + + + + + + return $param; + } + +