Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
90.00% covered (success)
90.00%
9 / 10
CRAP
96.97% covered (success)
96.97%
32 / 33
AbstractFilter
0.00% covered (danger)
0.00%
0 / 1
90.00% covered (success)
90.00%
9 / 10
22
96.97% covered (success)
96.97%
32 / 33
 check
n/a
0 / 0
0
n/a
0 / 0
 set
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 get
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 addError
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getErrors
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 isPositiveIntegerNotNull
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 isArray
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 eachEntryOfArrayIsString
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 eachKeyOfArrayIsString
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
5 / 5
 eachEntryOfArrayIsEnum
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 eachKeyOfArrayIsEnum
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
1<?php
2
3namespace Qmp\Laravel\ApiFilterRequest\Filters;
4
5use Qmp\Laravel\ApiFilterRequest\Filters;
6
7abstract class AbstractFilter
8{
9    protected $data;
10
11    protected $errors = [];
12
13    /**
14     * @param $data
15     * @return mixed
16     */
17    abstract public function check($data);
18
19    /**
20     * @param $data
21     */
22    public function set($data)
23    {
24        $this->data = $data;
25    }
26
27    /**
28     * @return array
29     */
30    public function get()
31    {
32        if (empty($this->data)) {
33            return [];
34        }
35
36        return $this->data;
37    }
38
39    /**
40     * @param $message
41     */
42    public function addError($message)
43    {
44        $this->errors[] = $message;
45    }
46
47    /**
48     * @return array
49     */
50    public function getErrors()
51    {
52        return $this->errors;
53    }
54
55    protected function isPositiveIntegerNotNull($data)
56    {
57        return is_int($data) && $data > 0;
58    }
59
60    /**
61     * @param $data
62     * @return bool
63     */
64    protected function isArray($data)
65    {
66        if (!is_array($data)) {
67            $this->addError('data is not an array : ' . var_export($data, true));
68            return false;
69        }
70
71        return true;
72    }
73
74    /**
75     * @param array $entries
76     * @return bool
77     */
78    protected function eachEntryOfArrayIsString(array $entries)
79    {
80        foreach ($entries as $entry) {
81            if (!is_string($entry)) {
82                $this->addError('Entry of array is not a string : ' . var_export($entry, true));
83                return false;
84            }
85        }
86
87        return true;
88    }
89
90    /**
91     * @param array $entries
92     * @return bool
93     */
94    protected function eachKeyOfArrayIsString(array $entries)
95    {
96        foreach ($entries as $key => $entry) {
97            if (!is_string($key) || is_numeric($key)) {
98                $this->addError('Key of array is not a string : ' . var_export([$key, $entry], true));
99                return false;
100            }
101        }
102
103        return true;
104    }
105
106    /**
107     * @param array $entries
108     * @param array $enum
109     * @return bool
110     */
111    protected function eachEntryOfArrayIsEnum(array $entries, array $enum)
112    {
113        foreach ($entries as $entry) {
114            if (!in_array(strtolower($entry), $enum)) {
115                $this->addError('Entry is not on array : ' . var_export($entry, true));
116                return false;
117            }
118        }
119
120        return true;
121    }
122
123    /**
124     * @param array $entries
125     * @param array $enum
126     * @return bool
127     */
128    protected function eachKeyOfArrayIsEnum(array $entries, array $enum)
129    {
130        foreach ($entries as $key => $entry) {
131            if (!in_array(strtolower($key), $enum)) {
132                $this->addError('Key is not on array : ' . var_export($entry, true));
133                return false;
134            }
135        }
136
137        return true;
138    }
139}