1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace smtech\StMarksSearch; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class ParameterArrayConstructor |
8
|
|
|
{ |
9
|
|
|
public function __construct($params) |
10
|
|
|
{ |
11
|
|
|
foreach ($params as $field => $value) { |
12
|
|
|
/* |
13
|
|
|
* looking for setters of the form `setField` for a field named |
14
|
|
|
* `field` |
15
|
|
|
*/ |
16
|
|
|
$setter = 'set' . ucfirst($field); |
17
|
|
|
$this->$setter($value); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Require that a specific parameter key exists (and optionally has a |
23
|
|
|
* particular class as one of its parents) |
24
|
|
|
* |
25
|
|
|
* @param mixed[string] $params |
|
|
|
|
26
|
|
|
* @param string $key |
27
|
|
|
* @param string|false $class (Optional, defaults to `FALSE`) name of class |
28
|
|
|
* to require as a parent |
29
|
|
|
* @return void |
30
|
|
|
* @throws Exception If the required parameter is not set (or does not |
31
|
|
|
* have the correct parent class, if `$class` is specified) |
32
|
|
|
*/ |
33
|
|
|
protected static function requireParameter($params, $key, $class = false) |
34
|
|
|
{ |
35
|
|
|
if (!isset($params[$key])) { |
36
|
|
|
throw new Exception("`$key` param required"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($class !== false && is_string($class)) { |
40
|
|
|
if (!is_a($params[$key], $class)) { |
41
|
|
|
throw new Exception( |
42
|
|
|
"`$key` must be an instance of `$class`, instance of " . |
43
|
|
|
get_class($params[$key]) . ' passed instead' |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set a parameter to a default value if it is not set |
51
|
|
|
* |
52
|
|
|
* @param mixed[string] $params |
|
|
|
|
53
|
|
|
* @param string $key |
54
|
|
|
* @param mixed $value |
55
|
|
|
* @param string|false $class |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
protected static function defaultParameter(&$params, $key, $value, $class = false) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
|
|
static::requireParameter($params, $key, $class); |
62
|
|
|
} catch (Exception $e) { |
63
|
|
|
$params[$key] = $value; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Force a boolean result from a particular parameter key |
69
|
|
|
* |
70
|
|
|
* @param mixed[string] $params |
|
|
|
|
71
|
|
|
* @param string $key [description] |
72
|
|
|
* @return boolean `TRUE` iff `$params[$key]` exists and has a true value |
73
|
|
|
* (`1`, `'yes'`, `'true'`, `true`, etc.), `FALSE` |
74
|
|
|
* otherwise. |
75
|
|
|
*/ |
76
|
|
|
protected static function forceBooleanParameter($params, $key) |
77
|
|
|
{ |
78
|
|
|
return isset($params[$key]) && filter_var($params[$key], FILTER_VALIDATE_BOOLEAN); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected static function consumeParameters($params, $consumedParams) |
82
|
|
|
{ |
83
|
|
|
return array_diff_key($params, array_flip($consumedParams)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function __call($method, $arguments) |
87
|
|
|
{ |
88
|
|
|
if (method_exists($this, $method)) { |
89
|
|
|
return $this->$method($arguments); |
90
|
|
|
} elseif (preg_match('/^([gs]et)([A-Z].*)$/', $method, $match)) { |
91
|
|
|
$verb = $match[1]; |
92
|
|
|
$property = lcfirst($match[2]); |
93
|
|
|
if ($verb == 'set') { |
94
|
|
|
$this->$property = $arguments[0]; |
95
|
|
|
return; |
96
|
|
|
} elseif (isset($this->$property)) { |
97
|
|
|
return $this->$property; |
98
|
|
|
} else { |
99
|
|
|
trigger_error('Fatal error: Call to undefined property ' . static::class . "::$property"); |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
|
|
trigger_error('Fatal error: Call to undefined method ' . static::class . "::$method"); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.