FilterHandlers::isFilter()   A
last analyzed

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 2
1
<?php
2
3
namespace ComicVine\Api\Filters;
4
5
/**
6
 * Trait FilterHandlers
7
 *
8
 * @package grzgajda/comicvine-api
9
 * @author  Grzegorz Gajda <[email protected]>
10
 */
11
trait FilterHandlers
12
{
13
    /**
14
     * Validation for setted filters to stay
15
     * clean URL.
16
     *
17
     * @param array $filters Setted filters.
18
     * @param array $enabled Enabled filters.
19
     *
20
     * @return bool
21
     */
22
    protected function flushDisabledFilters(array $filters, array $enabled)
23
    {
24
        array_walk($filters, function (&$param, $field) use ($enabled) {
25
            if ($this->isApiKey($field) === true) {
26
                return $param;
27
            }
28
            if ($this->isFieldList($field) === true) {
29
                return $param = $this->validFieldList($field, $param);
30
            }
31
            if ($this->isFilter($field, $enabled) === true) {
32
                return $param = $this->validFilter($param);
33
            }
34
35
            return $param = [];
36
        });
37
38
        return $filters;
39
    }
40
41
    /**
42
     * Checking if current position of array_walk is
43
     * 'api_key'.
44
     *
45
     * @param string $field Key of field.
46
     *
47
     * @return bool
48
     */
49
    protected function isApiKey($field)
50
    {
51
        if ($field === "api_key") {
52
            return true;
53
        }
54
55
        return false;
56
    }
57
58
    /**
59
     * Checking if current position of array_walk
60
     * is 'api_key'.
61
     *
62
     * @param string $field Key of field.
63
     *
64
     * @return bool
65
     */
66
    protected function isFieldList($field)
67
    {
68
        if ($field === "field_list") {
69
            return true;
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * Valid for 'field_list' key.
77
     *
78
     * @param string       $field Key of field.
79
     * @param string|array $param Param of field.
80
     *
81
     * @return array
82
     */
83
    protected function validFieldList($field, $param)
84
    {
85
        if ($field === 'field_list' && $param !== "") {
86
            return $param;
87
        }
88
89
        return [];
90
    }
91
92
    /**
93
     * Check if current position is enabled.
94
     *
95
     * @param string $field   Key of field
96
     * @param array  $enabled Array of enabled filters.
97
     *
98
     * @return bool
99
     */
100
    protected function isFilter($field, $enabled)
101
    {
102
        if (array_key_exists($field, $enabled) === false) {
103
            return false;
104
        }
105
106
        if ($enabled[$field] === true) {
107
            return true;
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * Check if params are valid.
115
     *
116
     * @param string|array $param Params of filter.
117
     *
118
     * @return string|array
119
     */
120
    protected function validFilter($param)
121
    {
122
        if ($param !== "") {
123
            return $param;
124
        }
125
126
        return [];
127
    }
128
129
}