Completed
Push — master ( 3efda9...d6329d )
by Grzegorz
02:38
created

FilterValidation::isIntAndBetween()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 7
nc 3
nop 3
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 View Code Duplication
        if (is_array($valueParams) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            if ($this->isParamOfTypeMultiple($value, $valueParams) === false
52
                || $this->isParamOfTypeSingle($key, $keyParams) === false
53
            ) {
54
                return false;
55
            }
56
        }
57
58 View Code Duplication
        if (is_array($valueParams) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            if ($this->isParamOfTypeSingle($key, $keyParams) === false
60
                || $this->isParamOfTypeSingle($value, $valueParams) === false
61
            ) {
62
                return false;
63
            }
64
        }
65
66
        return true;
67
    }
68
69
    /**
70
     * Check if value is any of these types.
71
     *
72
     * @param string $param Value to check
73
     * @param array  $types List of types
74
     *
75
     * @return bool
76
     */
77
    public function isParamOfTypeMultiple($param, $types)
78
    {
79
        array_walk($types, function (&$type) use ($param) {
80
            $type = $this->isParamOfTypeSingle($param, $type);
81
        });
82
83
        echo in_array(true, $types);
84
85
        return in_array(true, $types);
86
    }
87
88
    /**
89
     * Check if value is not that type.
90
     *
91
     * @param string $param Value
92
     * @param string $type  Type
93
     *
94
     * @return bool
95
     */
96
    public function isParamOfTypeSingle($param, $type)
97
    {
98
        return call_user_func("is_$type", $param);
99
    }
100
}