Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.ApiUsage.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Verify arguments for wsh programs
9  *
10  * @class apiUsage
11  * @brief Verify arguments for wsh programs
12  * @code
13  $usage = new ApiUsage();
14  $usage->setText("Refresh documents ");
15  $usage->addNeeded("famid", "the family filter");
16  $usage->addOption("revision", "use all revision - default is no", array(
17  "yes",
18  "no"
19  ));
20  $usage->addOption("save", "use modify default is light", array(
21  "complete",
22  "light",
23  "none"
24  ));
25  $usage->verify();
26  * @endcode
27  */
28 class ApiUsage
29 {
30  /**
31  * usage text
32  *
33  * @var string
34  */
35  private $text = '';
36  /**
37  * optionnals arguments
38  *
39  * @var array
40  */
41  private $optArgs = array();
42  /**
43  * needed arguments
44  *
45  * @var array
46  */
47  private $needArgs = array();
48  /**
49  * hidden arguments
50  *
51  * @var array
52  */
53  private $hiddenArgs = array();
54  /**
55  * current action
56  *
57  * @var Action
58  */
59  protected $action;
60  /**
61  * strict mode
62  *
63  * @var boolean
64  */
65  protected $strict = true;
66  /**
67  * init action
68  */
69  public function __construct()
70  {
71  global $action;
72  $this->action = & $action;
73  $this->addHidden("api", "api file to use");
74  $this->addOption('userid', "user system id to execute function - default is (admin)", array() , 1);
75  }
76  /**
77  * add textual definition of program
78  *
79  * @param string $text usage text
80  *
81  * @return void
82  */
83  public function setText($text)
84  {
85  $this->text = $text;
86  }
87  /**
88  * add hidden argument (private arg not see them in usage)
89  *
90  * @param string $argName argument name
91  * @param string $argDefinition argument définition
92  *
93  * @return argument value
94  */
95  public function addHidden($argName, $argDefinition)
96  {
97  $this->hiddenArgs[] = array(
98  "name" => $argName,
99  "def" => $argDefinition
100  );
101  return $this->action->getArgument($argName);
102  }
103  /**
104  * add needed argument
105  *
106  * @param string $argName argument name
107  * @param string $argDefinition argument définition
108  * @param array $restriction optionnal enumeration for argument
109  *
110  * @return argument value
111  */
112  public function addNeeded($argName, $argDefinition, array $restriction = null)
113  {
114  $this->needArgs[] = array(
115  "name" => $argName,
116  "def" => $argDefinition,
117  "restriction" => $restriction
118  );
119  return $this->action->getArgument($argName);
120  }
121  /**
122  * add optionnal argument
123  *
124  * @param string $argName argument name
125  * @param string $argDefinition argument définition
126  * @param array $restriction optionnal enumeration for argument
127  * @param string $default default value if no value set
128  *
129  * @return argument value
130  */
131  public function addOption($argName, $argDefinition, array $restriction = null, $default = null)
132  {
133  $this->optArgs[] = array(
134  "name" => $argName,
135  "def" => $argDefinition,
136  "default" => $default,
137  "restriction" => $restriction
138  );
139  return $this->action->getArgument($argName, $default);
140  }
141  /**
142  * get usage for a specific argument
143  *
144  * @param array $args argument
145  *
146  * @return string
147  */
148  private function getArgumentText(array $args)
149  {
150  $usage = '';
151  foreach ($args as $arg) {
152  $res = '';
153  if ($arg["restriction"]) {
154  $res = ' [' . implode('|', $arg["restriction"]) . ']';
155  }
156  $default = "";
157  if ($arg["default"] !== null) {
158  $default = sprintf(", default is '%s'", $arg["default"]);
159  }
160  $usage.= sprintf("\t--%s=<%s>%s%s\n", $arg["name"], $arg["def"], $res, $default);
161  }
162  return $usage;
163  }
164  /**
165  * return usage text for the action
166  *
167  * @return string
168  */
169  public function getUsage()
170  {
171  $usage = $this->text;
172  $usage.= "\nUsage :\n";
173  $usage.= $this->getArgumentText($this->needArgs);
174  $usage.= " Options:\n";
175  $usage.= $this->getArgumentText($this->optArgs);
176 
177  return $usage;
178  }
179  /**
180  * exit when error
181  *
182  * @param string $error message error
183  *
184  * @return void
185  */
186  public function exitError($error = '')
187  {
188  if ($error != '') $error.= "\n";
189  $usage = $this->getUsage();
190  if ($_SERVER['HTTP_HOST'] != "") {
191  $usage = str_replace('--', '&', $usage);
192  $error.= '<pre>' . htmlspecialchars($usage) . '</pre>';
193  } else {
194  $error.= $usage;
195  }
196  $this->action->exitError($error);
197  }
198  /**
199  * list hidden keys
200  *
201  * @return array
202  */
203  protected function getHiddenKeys()
204  {
205  $keys = array();
206  foreach ($this->hiddenArgs as $v) {
207  $keys[] = $v["name"];
208  }
209  return $keys;
210  }
211  /**
212  * set strict mode
213  *
214  * @param boolean $strict strict mode
215  * @brief if false additionnal arguments are ignored, default is true
216  *
217  * @return void
218  */
219  public function strict($strict = true)
220  {
221  $this->strict = $strict;
222  }
223  /**
224  * verify if wsh program argument are valids. If not wsh exit
225  *
226  * @param boolean $useExcepion if true throw ApiUsageException when verify is not successful
227  *
228  * @return void
229  */
230  public function verify($useException = false)
231  {
232  foreach ($this->needArgs as $arg) {
233  $value = $this->action->getArgument($arg["name"]);
234  if ($value == '') {
235  $error = sprintf("argument '%s' expected\n", $arg["name"]);
236  if ($useException) {
237  throw ApiUsageException($error);
238  } else {
239  $this->exitError($error);
240  }
241  }
242  }
243  $allArgs = array_merge($this->needArgs, $this->optArgs);
244  $argsKey = $this->getHiddenKeys();
245 
246  foreach ($allArgs as $arg) {
247  $value = $this->action->getArgument($arg["name"], null);
248  if ($value !== null && $arg["restriction"]) {
249  if (!in_array($value, $arg["restriction"])) {
250  $error = sprintf("argument '%s' must be one of these values : %s\n", $arg["name"], implode(", ", $arg["restriction"]));
251  if ($useException) {
252  throw ApiUsageException($error);
253  } else {
254  $this->exitError($error);
255  }
256  }
257  }
258  $argsKey[] = $arg["name"];
259  }
260  if ($this->strict) {
261  foreach ($_GET as $k => $v) {
262  if (!in_array($k, $argsKey)) {
263  $error = sprintf("argument '%s' is not defined\n", $k);
264  if ($useException) {
265  throw ApiUsageException($error);
266  } else {
267  $this->exitError($error);
268  }
269  }
270  }
271  }
272  }
273 }
274 
275 class ApiUsageException extends Exception
276 {
277 }
278 ?>
← centre documentaire © anakeen - published under CC License - Dynacase