1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace smtech\StMarksSearch; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Require parameters |
9
|
|
|
* |
10
|
|
|
* @author Seth Battis <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
trait RequireParameter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Require that a specific parameter key exists (and optionally has a |
16
|
|
|
* particular class as one of its parents) |
17
|
|
|
* |
18
|
|
|
* @param mixed[string] $params |
|
|
|
|
19
|
|
|
* @param string $key |
20
|
|
|
* @param string|false $class (Optional, defaults to `FALSE`) name of class |
21
|
|
|
* to require as a parent |
22
|
|
|
* @return void |
23
|
|
|
* @throws Exception If the required parameter is not set (or does not |
24
|
|
|
* have the correct parent class, if `$class` is specified) |
25
|
|
|
*/ |
26
|
|
|
protected function requireParameter($params, $key, $class = false) |
27
|
|
|
{ |
28
|
|
|
assert(isset($params[$key]), new Exception("`$key` param required")); |
29
|
|
|
|
30
|
|
|
if ($class !== false && is_string($class)) { |
31
|
|
|
assert( |
32
|
|
|
is_a($params[$key], $class), |
33
|
|
|
new Exception( |
34
|
|
|
"`$key` must be an instance of `$class`, instance of " . |
35
|
|
|
get_class($params[$key]) . ' passed instead' |
36
|
|
|
) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Force a boolean result from a particular parameter key |
44
|
|
|
* |
45
|
|
|
* @param mixed[string] $params |
|
|
|
|
46
|
|
|
* @param string $key [description] |
47
|
|
|
* @return boolean `TRUE` iff `$params[$key]` exists and has a true value |
48
|
|
|
* (`1`, `'yes'`, `'true'`, `true`, etc.), `FALSE` |
49
|
|
|
* otherwise. |
50
|
|
|
*/ |
51
|
|
|
protected function forceBooleanParameter($params, $key) |
52
|
|
|
{ |
53
|
|
|
return isset($params[$key]) && filter_var($params[$key], FILTER_VALIDATE_BOOLEAN); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.