Completed
Push — master ( a694da...3962ba )
by Grzegorz
02:30
created

Validation::validLimit()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 12
loc 12
rs 9.2
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace ComicVine\Api;
4
5
use ComicVine\Api\Filters\FilterCheck;
6
7
/**
8
 * Check validation of inputs for ControllerQuery.
9
 *
10
 * Class Validation
11
 *
12
 * @package grzgajda/comicvine-api
13
 * @author  Grzegorz Gajda <[email protected]>
14
 */
15
class Validation
16
{
17
    use FilterCheck;
18
19
    /**
20
     * Mock for enabled filters.
21
     *
22
     * @var array
23
     */
24
    protected $enabledFilters = [];
25
26
    /**
27
     * Validation constructor.
28
     *
29
     * @param array $filters
30
     */
31
    public function __construct($filters = [])
32
    {
33
        if ($filters !== []) {
34
            $this->enabledFilters = $filters;
35
        }
36
    }
37
38
    /**
39
     * Check validation for $input
40
     *
41
     * @param string       $type
42
     * @param string|array $input
43
     *
44
     * @return bool
45
     */
46
    public function validation($type = "", $input)
47
    {
48
        switch ($type) {
49
            case 'field_list':
50
                return $this->validFieldList($input);
51
            case 'limit':
52
                return $this->validLimit($input);
53
            case 'offset':
54
                return $this->validOffset($input);
55
            case 'filter':
56
                return $this->validFilter($input);
57
            case 'sort':
58
                return $this->validSort($input);
59
            default:
60
                return false;
61
        }
62
    }
63
64
    /**
65
     * Validation for FIELD_LIST parameter.
66
     *
67
     * @param string|array $input
68
     *
69
     * @return bool
70
     */
71
    protected function validFieldList($input)
72
    {
73
        if (is_array($input) === false) {
74
            return false;
75
        }
76
77
        foreach ($input as $key => $value) {
78
            if (is_int($key) === false) {
79
                return false;
80
            }
81
            if (is_string($value) === false) {
82
                return false;
83
            }
84
        }
85
86
        return true;
87
    }
88
89
    /**
90
     * Validation for LIMIT parameter.
91
     *
92
     * @param string|array $input
93
     *
94
     * @return bool
95
     */
96 View Code Duplication
    protected function validLimit($input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
97
    {
98
        if (is_int($input) === false) {
99
            return false;
100
        }
101
102
        if ($input < 0 || $input > 100) {
103
            return false;
104
        }
105
106
        return $this->isEnabledFilter('limit', $this->enabledFilters);
107
    }
108
109
    /**
110
     * Validation for OFFSET parameter.
111
     *
112
     * @param string|array $input
113
     *
114
     * @return bool
115
     */
116 View Code Duplication
    protected function validOffset($input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
117
    {
118
        if (is_int($input) === false) {
119
            return false;
120
        }
121
122
        if ($input < 0) {
123
            return false;
124
        }
125
126
        return $this->isEnabledFilter('offset', $this->enabledFilters);
127
    }
128
129
    /**
130
     * Validation for FILTER parameter.
131
     *
132
     * @param string|array $input
133
     *
134
     * @return bool
135
     */
136
    protected function validFilter($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 (is_array($value) === true) {
147
                return false;
148
            }
149
            if (is_object($value) === true) {
150
                return false;
151
            }
152
        }
153
154
        return $this->isEnabledFilter('filter', $this->enabledFilters);
155
    }
156
157
    /**
158
     * Validation for SORT parameter.
159
     *
160
     * @param string|array $input
161
     *
162
     * @return bool
163
     */
164
    protected function validSort($input)
165
    {
166
        if (is_array($input) === false) {
167
            return false;
168
        }
169
170
        foreach ($input as $key => $value) {
171
            if (is_string($key) === false) {
172
                return false;
173
            }
174
            if ($value !== 'asc' && $value !== 'desc') {
175
                return false;
176
            }
177
        }
178
179
        return $this->isEnabledFilter('sort', $this->enabledFilters);
180
    }
181
}