Completed
Push — master ( 1d790a...bb9b7d )
by Seth
02:11
created

RequireParameter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A requireParameter() 0 14 3
A forceBooleanParameter() 0 4 2
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
1 ignored issue
show
Documentation introduced by
The doc-type mixed[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

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.

Loading history...
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
1 ignored issue
show
Documentation introduced by
The doc-type mixed[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

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.

Loading history...
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