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

Validation::validNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 4
1
<?php
2
3
namespace ComicVine\Api;
4
5
use ComicVine\Api\Filters\FilterCheck;
6
use ComicVine\Api\Filters\FilterValidation;
7
8
/**
9
 * Check validation of inputs for ControllerQuery.
10
 *
11
 * Class Validation
12
 *
13
 * @package grzgajda/comicvine-api
14
 * @author  Grzegorz Gajda <[email protected]>
15
 */
16
class Validation
17
{
18
    use FilterCheck;
19
    use FilterValidation;
20
21
    /**
22
     * Mock for enabled filters.
23
     *
24
     * @var array
25
     */
26
    protected $enabledFilters = [];
27
28
    /**
29
     * Validation constructor.
30
     *
31
     * @param array $filters
32
     */
33
    public function __construct($filters = [])
34
    {
35
        if ($filters !== []) {
36
            $this->enabledFilters = $filters;
37
        }
38
    }
39
40
    /**
41
     * Check validation for $input
42
     *
43
     * @param string       $type
44
     * @param string|array $input
45
     *
46
     * @return bool
47
     */
48
    public function validation($type = "", $input)
49
    {
50
        switch ($type) {
51
            case 'field_list':
52
                return $this->validFieldList($input);
53
            case 'limit':
54
                return $this->validNumber('limit', $input, 0, 100);
0 ignored issues
show
Bug introduced by
It seems like $input defined by parameter $input on line 48 can also be of type array; however, ComicVine\Api\Validation::validNumber() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55
            case 'offset':
56
                return $this->validNumber('offset', $input, 0);
0 ignored issues
show
Bug introduced by
It seems like $input defined by parameter $input on line 48 can also be of type array; however, ComicVine\Api\Validation::validNumber() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
57
            case 'filter':
58
                return $this->validFilter($input);
59
            case 'sort':
60
                return $this->validSort($input);
61
            default:
62
                return false;
63
        }
64
    }
65
66
    /**
67
     * Validation for FIELD_LIST parameter.
68
     *
69
     * @param string|array $input
70
     *
71
     * @return bool
72
     */
73
    protected function validFieldList($input)
74
    {
75
        if (is_array($input) === false) {
76
            return false;
77
        }
78
79
        foreach ($input as $key => $value) {
80
            if ($this->isKeyAndValueAre($key, 'int', $value, 'string') === false) {
81
                return false;
82
            }
83
        }
84
85
        return true;
86
    }
87
88
    /**
89
     * Check if offset or limit is valid.
90
     *
91
     * @param string         $type  Type of valid (offset or limit)
92
     * @param string         $input Value
93
     * @param integer        $min   Min range what value can be
94
     * @param string|integer $max   Max range what value can be
95
     *
96
     * @return $this|bool
97
     */
98
    protected function validNumber($type, $input, $min, $max = "")
99
    {
100
        if ($this->isIntAndBetween($input, $min, $max) === false) {
101
            return false;
102
        }
103
104
        return $this->isEnabledFilter($type, $this->enabledFilters);
105
    }
106
107
    /**
108
     * Validation for FILTER parameter.
109
     *
110
     * @param string|array $input
111
     *
112
     * @return bool
113
     */
114
    protected function validFilter($input)
115
    {
116
        if (is_array($input) === false) {
117
            return false;
118
        }
119
120
        foreach ($input as $key => $value) {
121
            if ($this->isKeyAndValueAre($key, 'string', $value, ['string', 'int', 'float']) === false) {
122
                return false;
123
            }
124
        }
125
126
        return $this->isEnabledFilter('filter', $this->enabledFilters);
127
    }
128
129
    /**
130
     * Validation for SORT parameter.
131
     *
132
     * @param string|array $input
133
     *
134
     * @return bool
135
     */
136
    protected function validSort($input)
137
    {
138
        if (is_array($input) === false) {
139
            return false;
140
        }
141
142
        foreach ($input as $key => $value) {
143
            if (is_string($key) === false) {
144
                return false;
145
            }
146
            if ($value !== 'asc' && $value !== 'desc') {
147
                return false;
148
            }
149
        }
150
151
        return $this->isEnabledFilter('sort', $this->enabledFilters);
152
    }
153
}