Completed
Push — master ( a098b4...5a38a9 )
by Renato
05:12
created

FilterArray::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace NwLaravel\Repositories\Criterias\Filters;
4
5
class FilterArray implements FilterInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $table;
11
12
    /**
13
     * @var array
14
     */
15
    protected $columns;
16
17
    /**
18
     * @var array
19
     */
20
    protected $dates;
21
22
    /**
23
     * Construct
24
     *
25
     * @param array  $columns
26
     * @param array  $dates
27
     */
28 18
    public function __construct($table, $columns, $dates)
29
    {
30 18
        $this->table = $table;
31 18
        $this->columns = $columns;
32 18
        $this->dates = $dates;
33 18
    }
34
35
    /**
36
     * Filter
37
     *
38
     * @param Query\Builder $query
39
     * @param int|string    $key
40
     * @param mixed         $value
41
     *
42
     * @return boolean
43
     */
44 17
    public function filter($query, $key, $value)
45
    {
46 17
        if (is_int($key)) {
47
            /**
48
             * Using String Format
49
             * eg: {field},{operator},{value}
50
             */
51 13
            if (is_string($value) && preg_match('/^([a-zA-Z0-9_]+),(.+),(.+)$/', $value, $matches)) {
52 2
                $value = array_splice($matches, 1, 3);
53 2
            }
54
55
            /**
56
             * Using Array com Operator
57
             * eg: ex: ('field', '=', 'value') or ('field', 'value')
58
             */
59 13
            if (is_array($value) && count($value)) {
60 4
                $value = array_pad($value, 3, null);
61 4
                list($field, $operator, $valor) = array_splice($value, 0, 3);
62 4
                $filterWhere = new FilterWhere($this->table, $this->columns, $this->dates, $operator);
63 4
                return $filterWhere->filter($query, $field, $valor);
64
            }
65 9
        }
66
67 14
        return false;
68
    }
69
}
70