|
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
|
|
|
if (!isset($params[$key])) { |
|
29
|
|
|
throw new Exception("`$key` param required"); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($class !== false && is_string($class)) { |
|
33
|
|
|
if (!is_a($params[$key], $class)) { |
|
34
|
|
|
throw new Exception( |
|
35
|
|
|
"`$key` must be an instance of `$class`, instance of " . |
|
36
|
|
|
get_class($params[$key]) . ' passed instead' |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Force a boolean result from a particular parameter key |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed[string] $params |
|
|
|
|
|
|
47
|
|
|
* @param string $key [description] |
|
48
|
|
|
* @return boolean `TRUE` iff `$params[$key]` exists and has a true value |
|
49
|
|
|
* (`1`, `'yes'`, `'true'`, `true`, etc.), `FALSE` |
|
50
|
|
|
* otherwise. |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function forceBooleanParameter($params, $key) |
|
53
|
|
|
{ |
|
54
|
|
|
return isset($params[$key]) && filter_var($params[$key], FILTER_VALIDATE_BOOLEAN); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set a parameter to a default value if it is not set |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed[string] $params |
|
|
|
|
|
|
61
|
|
|
* @param string $key |
|
62
|
|
|
* @param mixed $value |
|
63
|
|
|
* @param string|false $class |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function defaultParameter(&$params, $key, $value, $class = false) |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
$this->requireParameter($params, $key, $class); |
|
70
|
|
|
} catch (Exception $e) { |
|
71
|
|
|
$params[$key] = $value; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
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.