• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / datagrid / 7048127238

30 Nov 2023 02:47PM UTC coverage: 34.081%. First build
7048127238

Pull #1060

github

paveljanda
Minor CSS changes for the next verison of datagrid
Pull Request #1060: [7.x] Next

118 of 431 new or added lines in 54 files covered. (27.38%)

1125 of 3301 relevant lines covered (34.08%)

0.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.11
/src/DataSource/DibiFluentDataSource.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Datagrid\DataSource;
4

5
use Contributte\Datagrid\AggregationFunction\IAggregatable;
6
use Contributte\Datagrid\AggregationFunction\IAggregationFunction;
7
use Contributte\Datagrid\Exception\DatagridDateTimeHelperException;
8
use Contributte\Datagrid\Filter\FilterDate;
9
use Contributte\Datagrid\Filter\FilterDateRange;
10
use Contributte\Datagrid\Filter\FilterMultiSelect;
11
use Contributte\Datagrid\Filter\FilterRange;
12
use Contributte\Datagrid\Filter\FilterSelect;
13
use Contributte\Datagrid\Filter\FilterText;
14
use Contributte\Datagrid\Utils\DateTimeHelper;
15
use Contributte\Datagrid\Utils\Sorting;
16
use dibi;
17
use Dibi\Fluent;
18
use Dibi\Helpers;
19
use ReflectionClass;
20

21
class DibiFluentDataSource extends FilterableDataSource implements IDataSource, IAggregatable
22
{
23

24
        /** @var array */
25
        protected array $data = [];
26

27
        public function __construct(protected Fluent $dataSource, protected string $primaryKey)
1✔
28
        {
29
        }
1✔
30

31
        public function getCount(): int
32
        {
33
                return $this->dataSource->count();
1✔
34
        }
35

36
        /**
37
         * {@inheritDoc}
38
         */
39
        public function getData(): array
40
        {
41
                return $this->data !== [] ? $this->data : $this->dataSource->fetchAll();
1✔
42
        }
43

44
        /**
45
         * {@inheritDoc}
46
         */
47
        public function filterOne(array $condition): IDataSource
1✔
48
        {
49
                $this->dataSource->where($condition)->limit(1);
1✔
50

51
                return $this;
1✔
52
        }
53

54
        public function limit(int $offset, int $limit): IDataSource
1✔
55
        {
56
                $this->dataSource->limit($limit)->offset($offset);
1✔
57

58
                $this->data = $this->dataSource->fetchAll();
1✔
59

60
                return $this;
1✔
61
        }
62

63
        public function sort(Sorting $sorting): IDataSource
1✔
64
        {
65
                if (is_callable($sorting->getSortCallback())) {
1✔
66
                        call_user_func(
×
67
                                $sorting->getSortCallback(),
×
68
                                $this->dataSource,
×
69
                                $sorting->getSort()
×
70
                        );
71

72
                        return $this;
×
73
                }
74

75
                $sort = $sorting->getSort();
1✔
76

77
                if ($sort !== []) {
1✔
78
                        $this->dataSource->removeClause('ORDER BY');
1✔
79
                        $this->dataSource->orderBy($sort);
1✔
80
                } else {
81
                        /**
82
                         * Has the statement already a order by clause?
83
                         */
84
                        $this->dataSource->clause('ORDER BY');
×
85

86
                        $reflection = new ReflectionClass(Fluent::class);
×
87

88
                        $cursorProperty = $reflection->getProperty('cursor');
×
89
                        $cursorProperty->setAccessible(true);
×
90
                        $cursor = $cursorProperty->getValue($this->dataSource);
×
91

92
                        if (!(bool) $cursor) {
×
93
                                $this->dataSource->orderBy($this->primaryKey);
×
94
                        }
95
                }
96

97
                return $this;
1✔
98
        }
99

100
        public function processAggregation(IAggregationFunction $function): void
101
        {
102
                $function->processDataSource(clone $this->dataSource);
×
103
        }
104

105
        protected function applyFilterDate(FilterDate $filter): void
106
        {
107
                $conditions = $filter->getCondition();
×
108

109
                try {
110
                        $date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
×
111

112
                        $this->dataSource->where('DATE(%n) = ?', $filter->getColumn(), $date->format('Y-m-d'));
×
NEW
113
                } catch (DatagridDateTimeHelperException) {
×
114
                        // ignore the invalid filter value
115
                }
116
        }
117

118
        protected function applyFilterDateRange(FilterDateRange $filter): void
119
        {
120
                $conditions = $filter->getCondition();
×
121

122
                $valueFrom = $conditions[$filter->getColumn()]['from'];
×
123
                $valueTo = $conditions[$filter->getColumn()]['to'];
×
124

125
                if ($valueFrom) {
×
126
                        try {
127
                                $dateFrom = DateTimeHelper::tryConvertToDateTime($valueFrom, [$filter->getPhpFormat()]);
×
128
                                $dateFrom->setTime(0, 0, 0);
×
129

130
                                $this->dataSource->where('DATE(%n) >= ?', $filter->getColumn(), $dateFrom);
×
NEW
131
                        } catch (DatagridDateTimeHelperException) {
×
132
                                // ignore the invalid filter value
133
                        }
134
                }
135

136
                if ($valueTo) {
×
137
                        try {
138
                                $dateTo = DateTimeHelper::tryConvertToDateTime($valueTo, [$filter->getPhpFormat()]);
×
139
                                $dateTo->setTime(23, 59, 59);
×
140

141
                                $this->dataSource->where('DATE(%n) <= ?', $filter->getColumn(), $dateTo);
×
NEW
142
                        } catch (DatagridDateTimeHelperException) {
×
143
                                // ignore the invalid filter value
144
                        }
145
                }
146
        }
147

148
        protected function applyFilterRange(FilterRange $filter): void
1✔
149
        {
150
                $conditions = $filter->getCondition();
1✔
151

152
                $valueFrom = $conditions[$filter->getColumn()]['from'];
1✔
153
                $valueTo = $conditions[$filter->getColumn()]['to'];
1✔
154

155
                if ($valueFrom || $valueFrom !== '') {
1✔
156
                        $this->dataSource->where('%n >= ?', $filter->getColumn(), $valueFrom);
1✔
157
                }
158

159
                if ($valueTo || $valueTo !== '') {
1✔
160
                        $this->dataSource->where('%n <= ?', $filter->getColumn(), $valueTo);
1✔
161
                }
162
        }
1✔
163

164
        protected function applyFilterText(FilterText $filter): void
1✔
165
        {
166
                $condition = $filter->getCondition();
1✔
167
                $driver = $this->dataSource->getConnection()->getDriver();
1✔
168
                $or = [];
1✔
169

170
                foreach ($condition as $column => $value) {
1✔
171
                        $column = Helpers::escape($driver, $column, dibi::IDENTIFIER);
1✔
172

173
                        if ($filter->isExactSearch()) {
1✔
174
                                $this->dataSource->where(sprintf('%s = %%s', $column), $value);
1✔
175

176
                                continue;
1✔
177
                        }
178

179
                        $words = $filter->hasSplitWordsSearch() === false ? [$value] : explode(' ', $value);
1✔
180

181
                        foreach ($words as $word) {
1✔
182
                                $or[] = [sprintf('%s LIKE %%~like~', $column), $word];
1✔
183
                        }
184
                }
185

186
                if (count($or) > 1) {
1✔
187
                        $this->dataSource->where('(%or)', $or);
1✔
188
                } else {
189
                        $this->dataSource->where($or);
1✔
190
                }
191
        }
1✔
192

193
        protected function applyFilterMultiSelect(FilterMultiSelect $filter): void
194
        {
195
                $condition = $filter->getCondition();
×
196
                $values = $condition[$filter->getColumn()];
×
197

NEW
198
                if ((is_countable($values) ? count($values) : 0) > 1) {
×
199
                        $value1 = array_shift($values);
×
NEW
200
                        $length = count($values);
×
201
                        $i = 1;
×
202

203
                        $this->dataSource->where('(%n = ?', $filter->getColumn(), $value1);
×
204

205
                        foreach ($values as $value) {
×
206
                                if ($i === $length) {
×
207
                                        $this->dataSource->__call('or', ['%n = ?)', $filter->getColumn(), $value]);
×
208
                                } else {
209
                                        $this->dataSource->__call('or', ['%n = ?', $filter->getColumn(), $value]);
×
210
                                }
211

212
                                $i++;
×
213
                        }
214
                } else {
215
                        $this->dataSource->where('%n = ?', $filter->getColumn(), reset($values));
×
216
                }
217
        }
218

219
        protected function applyFilterSelect(FilterSelect $filter): void
1✔
220
        {
221
                $this->dataSource->where($filter->getCondition());
1✔
222
        }
1✔
223

224
        protected function getDataSource(): Fluent
225
        {
226
                return $this->dataSource;
×
227
        }
228

229
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc