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

contributte / datagrid / 15346293061

30 May 2025 12:00PM UTC coverage: 35.433% (-0.06%) from 35.492%
15346293061

push

github

f3l1x
QA: fix phpstan

4 of 7 new or added lines in 2 files covered. (57.14%)

127 existing lines in 9 files now uncovered.

1198 of 3381 relevant lines covered (35.43%)

0.35 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\Fluent;
17
use Dibi\Helpers;
18
use ReflectionClass;
19

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

23
        protected array $data = [];
24

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

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

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

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

49
                return $this;
1✔
50
        }
51

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

56
                $this->data = $this->dataSource->fetchAll();
1✔
57

58
                return $this;
1✔
59
        }
60

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

70
                        return $this;
×
71
                }
72

73
                $sort = $sorting->getSort();
1✔
74

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

84
                        $reflection = new ReflectionClass(Fluent::class);
×
85

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

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

95
                return $this;
1✔
96
        }
97

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

103
        public function getDataSource(): Fluent
104
        {
105
                return $this->dataSource;
×
106
        }
107

108
        protected function applyFilterDate(FilterDate $filter): void
109
        {
110
                $conditions = $filter->getCondition();
×
111

112
                try {
UNCOV
113
                        $date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
×
114

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

121
        protected function applyFilterDateRange(FilterDateRange $filter): void
122
        {
123
                $conditions = $filter->getCondition();
×
124

125
                $valueFrom = $conditions[$filter->getColumn()]['from'];
×
126
                $valueTo = $conditions[$filter->getColumn()]['to'];
×
127

128
                if ($valueFrom) {
×
129
                        try {
UNCOV
130
                                $dateFrom = DateTimeHelper::tryConvertToDateTime($valueFrom, [$filter->getPhpFormat()]);
×
UNCOV
131
                                $dateFrom->setTime(0, 0, 0);
×
132

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

139
                if ($valueTo) {
×
140
                        try {
UNCOV
141
                                $dateTo = DateTimeHelper::tryConvertToDateTime($valueTo, [$filter->getPhpFormat()]);
×
UNCOV
142
                                $dateTo->setTime(23, 59, 59);
×
143

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

151
        protected function applyFilterRange(FilterRange $filter): void
1✔
152
        {
153
                $conditions = $filter->getCondition();
1✔
154

155
                $valueFrom = $conditions[$filter->getColumn()]['from'];
1✔
156
                $valueTo = $conditions[$filter->getColumn()]['to'];
1✔
157

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

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

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

173
                foreach ($condition as $column => $value) {
1✔
174
                        $column = Helpers::escape($driver, $column, Fluent::Identifier);
1✔
175

176
                        if ($filter->isExactSearch()) {
1✔
177
                                $this->dataSource->where(sprintf('%s = %%s', $column), $value);
1✔
178

179
                                continue;
1✔
180
                        }
181

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

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

189
                if (count($or) > 1) {
1✔
190
                        $this->dataSource->where($filter->hasConjunctionSearch() ? '(%and)' : '(%or)', $or);
1✔
191
                } else {
192
                        $this->dataSource->where($or);
1✔
193
                }
194
        }
1✔
195

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

201
                if ((is_countable($values) ? count($values) : 0) > 1) {
×
UNCOV
202
                        $value1 = array_shift($values);
×
203
                        $length = count($values);
×
204
                        $i = 1;
×
205

UNCOV
206
                        $this->dataSource->where('(%n = ?', $filter->getColumn(), $value1);
×
207

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

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

222
        protected function applyFilterSelect(FilterSelect $filter): void
1✔
223
        {
224
                $this->dataSource->where($filter->getCondition());
1✔
225
        }
1✔
226

227
}
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

© 2026 Coveralls, Inc