Completed
Push — master ( 3962ba...3efda9 )
by Grzegorz
02:31
created

FilterValidation::isKeyAndValueAre()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 3
eloc 6
nc 3
nop 4
1
<?php
2
3
namespace ComicVine\Api\Filters;
4
5
/**
6
 * Trait FilterCheck
7
 *
8
 * @package grzgajda/comicvine-api
9
 * @author  Grzegorz Gajda <[email protected]>
10
 */
11
trait FilterValidation
12
{
13
    /**
14
     * Check if input is integer and stay
15
     * in range between $min and $max.
16
     *
17
     * @param mixed      $input Input field
18
     * @param int        $min   Min range of input
19
     * @param int|string $max   Max range of input
20
     *
21
     * @return bool
22
     */
23
    public function isIntAndBetween($input, $min, $max = "")
24
    {
25
        if (is_int($input) === false) {
26
            return false;
27
        }
28
29
        if ($input < $min) {
30
            return false;
31
        }
32
33
        if ($input > $max && is_int($max) === true) {
34
            return false;
35
        }
36
37
        return true;
38
    }
39
40
    /**
41
     * Check if key or value is not of any described types.
42
     *
43
     * @param string       $key         Value of key
44
     * @param string|array $keyParams   Not-Types of key
45
     * @param string       $value       Value of value
46
     * @param string|array $valueParams Not-Types of value
47
     *
48
     * @return bool
49
     */
50
    public function isKeyAndValueAre($key, $keyParams, $value, $valueParams)
51
    {
52
        if ($this->isParamOfType($key, $keyParams) === false) {
53
            return false;
54
        }
55
56
        if ($this->isParamOfType($value, $valueParams) === false) {
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * Check if values is any of these types.
65
     *
66
     * @param string       $param Value
67
     * @param string|array $types Type or array of types
68
     *
69
     * @return bool
70
     */
71
    public function isParamOfType($param, $types)
72
    {
73
        if (is_array($types) === true) {
74
            return $this->isParamOfTypeMultiple($param, $types);
75
        }
76
77
        return $this->isParamOfTypeSingle($param, $types);
78
    }
79
80
    /**
81
     * Check if value is any of these types.
82
     *
83
     * @param string $param Value to check
84
     * @param array  $types List of types
85
     *
86
     * @return bool
87
     */
88
    public function isParamOfTypeMultiple($param, $types)
89
    {
90
        array_walk($types, function(&$type) use ($param) {
91
            $type = $this->isParamOfTypeSingle($param, $type);
92
        });
93
94
        if (in_array(true, $types) === true) {
95
            return true;
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * Check if value is not that type.
103
     *
104
     * @param string $param Value
105
     * @param string $type  Type
106
     *
107
     * @return bool
108
     */
109
    public function isParamOfTypeSingle($param, $type)
110
    {
111
        return call_user_func("is_$type", $param);
112
    }
113
}