Completed
Push — master ( 3962ba...3efda9 )
by Grzegorz
02:31
created

FilterCheck::isEnabledFilter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 3
eloc 6
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 FilterCheck
12
{
13
    /**
14
     * Check if field $field is enabled for
15
     * making queries.
16
     *
17
     * @param string $field   Name of filters' field.
18
     * @param array  $filters Array of enabled filters for query.
19
     * @param mixed  $object  Return false or $this.
20
     *
21
     * @return $this
22
     */
23
    protected function isEnabledFilter($field, array $filters, $object = null)
24
    {
25
        if (array_key_exists($field, $filters) === false) {
26
            return $this->returnObjectOrBool($object);
27
        }
28
29
        if ($filters[$field] === false) {
30
            return $this->returnObjectOrBool($object);
31
        }
32
33
        return true;
34
    }
35
36
    /**
37
     * Check if object is null, if yes: return bool, if not:
38
     * return object.
39
     *
40
     * @param mixed $object Instance of called class.
41
     *
42
     * @return bool|null
43
     */
44
    protected function returnObjectOrBool($object)
45
    {
46
        if ($object === null) {
47
            return false;
48
        }
49
50
        return $object;
51
    }
52
}