Completed
Branch master (3b97d6)
by Pavel
09:49
created

ArrayDataProvider::lteDate()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 1
nop 2
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
4
namespace Pfilsx\DataGrid\Grid\Providers;
5
6
7
use DateTime;
8
use Pfilsx\DataGrid\Grid\DataGridItem;
9
10
class ArrayDataProvider extends DataProvider
11
{
12
    protected $data;
13
14
    public function __construct(array $data)
15
    {
16
        $this->data = $data;
17
    }
18
19
    public function getItems(): array
20
    {
21
        if ($this->getPager()->isEnabled()){
22
            $this->data = array_slice($this->data, $this->getPager()->getFirst(), $this->getPager()->getLimit());
23
        }
24
        return array_map(function ($row) {
25
            $item = new DataGridItem();
26
            $item->setRow($row);
27
            return $item;
28
        }, $this->data);
29
    }
30
31
    public function getTotalCount(): int
32
    {
33
        return count($this->data);
34
    }
35
36
    public function setSort(array $sort): DataProviderInterface
37
    {
38
        if (!empty($this->data)) {
39
            foreach ($sort as $attribute => $order) {
40
                usort($this->data, $this->buildCompare($attribute, $order));
41
            }
42
43
        }
44
        return $this;
45
    }
46
47
    public function addEqualFilter(string $attribute, $value): DataProviderInterface
48
    {
49
        $this->data = array_filter($this->data, function ($row) use ($attribute, $value) {
50
            if (!array_key_exists($attribute, $row)) {
51
                return false;
52
            }
53
            return $row[$attribute] == $value;
54
        });
55
        return $this;
56
    }
57
58
    public function addLikeFilter(string $attribute, $value): DataProviderInterface
59
    {
60
        $this->data = array_filter($this->data, function ($row) use ($attribute, $value) {
61
            if (!array_key_exists($attribute, $row)) {
62
                return false;
63
            }
64
            return mb_strpos(mb_strtolower($row[$attribute]), mb_strtolower($value)) !== false;
65
        });
66
        return $this;
67
    }
68
69
    public function addCustomFilter(string $attribute, $value, callable $callback): DataProviderInterface
70
    {
71
        $this->data = array_filter($this->data, function ($row) use ($attribute, $value, $callback) {
72
            return call_user_func_array($callback, [$row, $attribute, $value]);
73
        });
74
        return $this;
75
    }
76
77
    protected function buildCompare($attribute, $order)
78
    {
79
        return function ($a, $b) use ($attribute, $order) {
80
            if (!array_key_exists($attribute, $a) || !array_key_exists($attribute, $b)) {
81
                return 0;
82
            }
83
            $attrValueA = $a[$attribute];
84
            $attrValueB = $b[$attribute];
85
            if ($attrValueA == $attrValueB) {
86
                return 0;
87
            }
88
            if (($type1 = gettype($attrValueA)) != gettype($attrValueB)) {
89
                return 0;
90
            }
91
            if ($type1 == 'string') {
92
                return $order == 'ASC' ? strcmp($attrValueA, $attrValueB) : -strcmp($attrValueA, $attrValueB);
93
            }
94
            return $order == 'ASC' ? $attrValueA <=> $attrValueB : $attrValueB <=> $attrValueA;
95
        };
96
    }
97
98
    protected function equalDate($attribute, $value): void
99
    {
100
        $date = new DateTime($value);
101
        $this->data = array_filter($this->data, function ($row) use ($attribute, $date) {
102
            if (!array_key_exists($attribute, $row)) {
103
                return false;
104
            }
105
            $attrValue = $row[$attribute];
106
            if ($attrValue instanceof DateTime) {
107
                return date('d.m.Y', $attrValue->getTimestamp()) == date('d.m.Y', $date->getTimestamp());
108
            }
109
            if (is_string($attrValue)) {
110
                $attrDate = new DateTime($attrValue);
111
                return date('d.m.Y', $attrDate->getTimestamp()) == date('d.m.Y', $date->getTimestamp());
112
            }
113
            return false;
114
        });
115
    }
116
117
    protected function ltDate($attribute, $value): void
118
    {
119
        $date = new DateTime($value);
120
        $this->data = array_filter($this->data, function ($row) use ($attribute, $date) {
121
            if (!array_key_exists($attribute, $row)) {
122
                return false;
123
            }
124
            $attrValue = $row[$attribute];
125
            if ($attrValue instanceof DateTime) {
126
                return $attrValue < $date;
127
            }
128
            if (is_string($attrValue)) {
129
                $attrDate = new DateTime($attrValue);
130
                return $attrDate < $date;
131
            }
132
            return false;
133
        });
134
    }
135
136
    protected function lteDate($attribute, $value): void
137
    {
138
        $date = (new DateTime($value))->modify('+1 day');
139
        $this->data = array_filter($this->data, function ($row) use ($attribute, $date) {
140
            if (!array_key_exists($attribute, $row)) {
141
                return false;
142
            }
143
            $attrValue = $row[$attribute];
144
            if ($attrValue instanceof DateTime) {
145
                return $attrValue < $date;
146
            }
147
            if (is_string($attrValue)) {
148
                $attrDate = new DateTime($attrValue);
149
                return $attrDate < $date;
150
            }
151
            return false;
152
        });
153
    }
154
155
    protected function gtDate($attribute, $value): void
156
    {
157
        $date = (new DateTime($value))->modify('+1 day');
158
        $this->data = array_filter($this->data, function ($row) use ($attribute, $date) {
159
            if (!array_key_exists($attribute, $row)) {
160
                return false;
161
            }
162
            $attrValue = $row[$attribute];
163
            if ($attrValue instanceof DateTime) {
164
                return $attrValue >= $date;
165
            }
166
            if (is_string($attrValue)) {
167
                $attrDate = new DateTime($attrValue);
168
                return $attrDate >= $date;
169
            }
170
            return false;
171
        });
172
    }
173
174
    protected function gteDate($attribute, $value): void
175
    {
176
        $date = new DateTime($value);
177
        $this->data = array_filter($this->data, function ($row) use ($attribute, $date) {
178
            if (!array_key_exists($attribute, $row)) {
179
                return false;
180
            }
181
            $attrValue = $row[$attribute];
182
            if ($attrValue instanceof DateTime) {
183
                return $attrValue >= $date;
184
            }
185
            if (is_string($attrValue)) {
186
                $attrDate = new DateTime($attrValue);
187
                return $attrDate >= $date;
188
            }
189
            return false;
190
        });
191
    }
192
}
193