FilterValidation::isKeyAndValueAre()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 3
eloc 5
nc 2
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
            || ($input > $max && is_int($max) === true)
31
        ) {
32
            return false;
33
        }
34
35
        return true;
36
    }
37
38
    /**
39
     * Check if key or value is not of any described types.
40
     *
41
     * @param string       $key         Value of key
42
     * @param string       $keyParams   Not-Types of key
43
     * @param string       $value       Value of value
44
     * @param string|array $valueParams Not-Types of value
45
     *
46
     * @return bool
47
     */
48
    public function isKeyAndValueAre($key, $keyParams, $value, $valueParams)
49
    {
50
        if ($this->isParamOfTypeSingle($key, $keyParams) === false
51
            || $this->isParamOfTypeSingle($value, $valueParams) === false
52
        ) {
53
            return false;
54
        }
55
56
        return true;
57
    }
58
59
    /**
60
     * Check if value is any of these types.
61
     *
62
     * @param string $param Value to check
63
     * @param array  $types List of types
64
     *
65
     * @return bool
66
     */
67
    public function isParamOfTypeMultiple($param, $types)
68
    {
69
        array_walk($types, function (&$type) use ($param) {
70
            $type = $this->isParamOfTypeSingle($param, $type);
71
        });
72
73
        return in_array(true, $types);
74
    }
75
76
    /**
77
     * Check if value is not that type.
78
     *
79
     * @param string       $param Value
80
     * @param string|array $type  Type
81
     *
82
     * @return bool
83
     */
84
    public function isParamOfTypeSingle($param, $type)
85
    {
86
        if (is_array($type) === true) {
87
            return $this->isParamOfTypeMultiple($param, $type);
88
        }
89
90
        return call_user_func("is_$type", $param);
91
    }
92
93
    /**
94
     * Check if value has a proper value and key a proper type.
95
     *
96
     * @param string $key      Key of value
97
     * @param string $keyParam Key possible types
98
     * @param string $param    Param value
99
     * @param array  $string   Param possible values.
100
     *
101
     * @return boolean
102
     */
103
    public function isParamAValue($key, $keyParam, $param, array $string)
104
    {
105
        if ($this->isParamOfTypeSingle($key, $keyParam) === true
106
            && in_array($param, $string) === true
107
        ) {
108
            return true;
109
        }
110
111
        return false;
112
    }
113
}