43 private $optArgs = array();
49 private $emptyArgs = array();
55 private $needArgs = array();
61 private $hiddenArgs = array();
88 $this->
addOptionalParameter(
'userid',
"user system id or login name to execute function - default is (admin)", null, 1);
98 public static function isScalar($argVal, $argName, $apiUsage)
101 if (!is_scalar($argVal)) {
102 if (is_array($argVal)) {
103 if (!isset($_FILES[$argName])) {
104 $err = sprintf(
"Argument doesn't support multiple value (values got are [%s])", implode(
",", $argVal));
106 }
else $err = sprintf(
"Value type %s isn't authorized for argument, must be a scalar", gettype($argVal));
117 public static function isArray($argVal, $argName, $apiUsage)
120 if (!is_array($argVal)) {
121 $err = sprintf(
"Value type %s isn't authorized for argument, must be an array", gettype($argVal));
179 $this->hiddenArgs[] = array(
181 "def" => $argDefinition
198 public function addNeeded($argName, $argDefinition, array $restriction = null)
216 $this->needArgs[] = array(
218 "def" => $argDefinition,
220 "restriction" => $restriction
237 public function addOption($argName, $argDefinition, array $restriction = null, $default = null)
256 $this->optArgs[] = array(
258 "def" => $argDefinition,
259 "default" => $default,
260 "restriction" => $restriction
276 public function addEmpty($argName, $argDefinition =
"")
293 $this->emptyArgs[] = array(
295 "def" => $argDefinition,
297 "restriction" => null
309 return $this->action->getArgument($key, $defaultValue);
319 private function getArgumentText(array
$args, $empty =
false)
322 foreach ($args as
$arg) {
327 } elseif (!empty($arg[
"restriction"]) && is_array($arg[
"restriction"])) {
328 $res =
' [' . implode(
'|', $arg[
"restriction"]) .
']';
331 if ($arg[
"default"] !== null) {
332 $default = sprintf(
", default is '%s'", print_r($arg[
"default"],
true));
334 $string =
"\t--" . $arg[
"name"] . ($empty ?
" (%s) " :
"=<%s>");
336 $usage.= sprintf(
"$string%s%s\n", $arg[
"def"], $res, $default);
346 if (!is_callable($f,
true)) {
349 if (is_object($f) && ($f instanceof Closure)) {
352 if (is_array($f) && (is_scalar($f[0]))) {
355 if (is_callable($f,
false)) {
370 $usage.= $this->getArgumentText($this->needArgs);
372 $usage.= $this->getArgumentText($this->optArgs);
373 $usage.= $this->getArgumentText($this->emptyArgs,
true);
385 if ($error !=
'') $error.=
"\n";
388 if (!$this->useException) {
389 if (!empty(
$_SERVER[
'HTTP_HOST'])) {
391 $error.=
'<pre>' . htmlspecialchars(
$usage) .
'</pre>';
396 throw new \Dcp\ApiUsage\Exception(
"CORE0003", $error,
$usage);
398 if (!empty(
$_SERVER[
'HTTP_HOST'])) {
399 $this->action->exitError($error);
402 throw new \Dcp\ApiUsage\Exception(
"CORE0002", $error,
$usage);
406 throw new \Dcp\ApiUsage\Exception(
"CORE0002", $error,
$usage);
417 foreach ($this->hiddenArgs as $v) {
418 $keys[] = $v[
"name"];
467 foreach ($this->needArgs as $arg) {
470 $error = sprintf(
"argument '%s' expected\n", $arg[
"name"]);
475 $allArgs = array_merge($this->needArgs, $this->optArgs, $this->emptyArgs);
478 foreach ($allArgs as $arg) {
482 $error = call_user_func($arg[
"restriction"],
$value, $arg[
"name"], $this);
486 if ($error) $this->
exitError(sprintf(
"Error checking argument \"%s\" : %s", $arg[
"name"], $error));
488 if (is_array($arg[
"restriction"]) && !empty($arg[
"restriction"]) && !$this->
isCallable($arg[
"restriction"])) {
490 if ($error) $this->
exitError(sprintf(
"Error for argument '%s' : %s", $arg[
"name"], $error));
493 $argsKey[] = $arg[
"name"];
496 foreach (
$_GET as $k => $v) {
497 if (!in_array($k, $argsKey)) {
498 $error = sprintf(
"argument '%s' is not defined\n", $k);
513 $values = (!is_array(
$value)) ? array(
516 foreach ($values as $aValue) {
517 if (!in_array($aValue, $restrictions)) {
518 $error = sprintf(
"argument must be one of these values : %s\n", implode(
", ", $restrictions));
526 namespace Dcp\ApiUsage
532 public function __construct($code, $text, $usage =
'')
534 parent::__construct($code, $text);
535 $this->
usage = $usage;
540 if ($this->
usage)
return $this->usage;
if(substr($wsh, 0, 1)!= '/') $args
addEmptyParameter($argName, $argDefinition="")
verify($useException=false)
Exception class use exceptionCode to identifiy correctly exception.
addHidden($argName, $argDefinition)
getArgumentValue($key, $defaultValue= '')
setStrictMode($strict=true)
if false additionnal arguments are ignored, default is true
static isScalar($argVal, $argName, $apiUsage)
__construct($code, $text, $usage= '')
addOptionalParameter($argName, $argDefinition, $restriction=null, $default=null)
strict($strict=true)
if false additionnal arguments are ignored, default is true
addOption($argName, $argDefinition, array $restriction=null, $default=null)
deprecatedFunction($msg= '')
addHiddenParameter($argName, $argDefinition)
addEmpty($argName, $argDefinition="")
static matchValues($value, $restrictions)
if($file) if($subject==""&&$file) if($subject=="") $err
static isArray($argVal, $argName, $apiUsage)
addRequiredParameter($argName, $argDefinition, $restriction=null)
addNeeded($argName, $argDefinition, array $restriction=null)
Verify arguments for wsh programs.