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

contributte / datagrid / 8230157862

11 Mar 2024 09:02AM UTC coverage: 34.102%. First build
8230157862

Pull #1060

github

radimvaculik
Fix failing tests
Pull Request #1060: [7.x] Next

117 of 435 new or added lines in 54 files covered. (26.9%)

1124 of 3296 relevant lines covered (34.1%)

0.34 hits per line

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

58.82
/src/DataSource/DoctrineCollectionDataSource.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 Doctrine\Common\Collections\Collection;
17
use Doctrine\Common\Collections\Criteria;
18
use Doctrine\Common\Collections\Selectable;
19
use LogicException;
20

21
final class DoctrineCollectionDataSource extends FilterableDataSource implements
22
        IDataSource,
23
        IAggregatable
24
{
25

26
        /** @var Collection&Selectable */
27
        protected $dataSource;
28

29
        protected Criteria $criteria;
30

31
        public function __construct(Collection $collection, protected string $primaryKey)
1✔
32
        {
33
                if (!($collection instanceof Selectable)) {
1✔
NEW
34
                        throw new LogicException(sprintf('Given collection must implement Selectable'));
×
35
                }
36

37
                $this->criteria = Criteria::create();
1✔
38
                $this->dataSource = $collection;
1✔
39
        }
1✔
40

41
        public function getCount(): int
42
        {
43
                return $this->dataSource->matching($this->criteria)->count();
1✔
44
        }
45

46
        /**
47
         * {@inheritDoc}
48
         */
49
        public function getData(): array
50
        {
51
                return $this->dataSource->matching($this->criteria)->toArray();
1✔
52
        }
53

54
        /**
55
         * {@inheritDoc}
56
         */
57
        public function filterOne(array $condition): IDataSource
1✔
58
        {
59
                foreach ($condition as $column => $value) {
1✔
60
                        if ($column === $this->primaryKey && is_numeric($value)) {
1✔
61
                                $value = (int) $value;
1✔
62
                        }
63

64
                        $expr = Criteria::expr()->eq($column, $value);
1✔
65
                        $this->criteria->andWhere($expr);
1✔
66
                }
67

68
                return $this;
1✔
69
        }
70

71
        public function limit(int $offset, int $limit): IDataSource
1✔
72
        {
73
                $this->criteria->setFirstResult($offset)->setMaxResults($limit);
1✔
74

75
                return $this;
1✔
76
        }
77

78
        public function sort(Sorting $sorting): IDataSource
1✔
79
        {
80
                if (is_callable($sorting->getSortCallback())) {
1✔
81
                        call_user_func(
×
82
                                $sorting->getSortCallback(),
×
83
                                $this->criteria,
×
84
                                $sorting->getSort()
×
85
                        );
86

87
                        return $this;
×
88
                }
89

90
                if ($sorting->getSort() !== []) {
1✔
91
                        $this->criteria->orderBy($sorting->getSort());
1✔
92

93
                        return $this;
1✔
94
                }
95

96
                $this->criteria->orderBy([$this->primaryKey => 'ASC']);
×
97

98
                return $this;
×
99
        }
100

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

106
        /**
107
         * @return Collection&Selectable
108
         */
109
        public function getDataSource(): mixed
110
        {
111
                return $this->dataSource;
×
112
        }
113

114
        protected function applyFilterDate(FilterDate $filter): void
115
        {
116
                foreach ($filter->getCondition() as $value) {
×
117
                        try {
118
                                $date = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]);
×
119

120
                                $from = Criteria::expr()->gte($filter->getColumn(), $date->format('Y-m-d 00:00:00'));
×
121
                                $to = Criteria::expr()->lte($filter->getColumn(), $date->format('Y-m-d 23:59:59'));
×
122

123
                                $this->criteria->andWhere($from)->andWhere($to);
×
NEW
124
                        } catch (DatagridDateTimeHelperException) {
×
125
                                // ignore the invalid filter value
126
                        }
127
                }
128
        }
129

130
        protected function applyFilterDateRange(FilterDateRange $filter): void
131
        {
132
                $conditions = $filter->getCondition();
×
133
                $values = $conditions[$filter->getColumn()];
×
134

135
                $valueFrom = $values['from'];
×
136

NEW
137
                if ($valueFrom) {
×
138
                        try {
139
                                $dateFrom = DateTimeHelper::tryConvertToDateTime($valueFrom, [$filter->getPhpFormat()]);
×
140
                                $dateFrom->setTime(0, 0, 0);
×
141

142
                                $expr = Criteria::expr()->gte($filter->getColumn(), $dateFrom->format('Y-m-d H:i:s'));
×
143
                                $this->criteria->andWhere($expr);
×
NEW
144
                        } catch (DatagridDateTimeHelperException) {
×
145
                                // ignore the invalid filter value
146
                        }
147
                }
148

149
                $valueTo = $values['to'];
×
150

NEW
151
                if ($valueTo) {
×
152
                        try {
153
                                $dateTo = DateTimeHelper::tryConvertToDateTime($valueTo, [$filter->getPhpFormat()]);
×
154
                                $dateTo->setTime(23, 59, 59);
×
155

156
                                $expr = Criteria::expr()->lte($filter->getColumn(), $dateTo->format('Y-m-d H:i:s'));
×
157
                                $this->criteria->andWhere($expr);
×
NEW
158
                        } catch (DatagridDateTimeHelperException) {
×
159
                                // ignore the invalid filter value
160
                        }
161
                }
162
        }
163

164
        protected function applyFilterRange(FilterRange $filter): void
1✔
165
        {
166
                $conditions = $filter->getCondition();
1✔
167

168
                $valueFrom = $conditions[$filter->getColumn()]['from'];
1✔
169
                $valueTo = $conditions[$filter->getColumn()]['to'];
1✔
170

171
                if (is_numeric($valueFrom)) {
1✔
172
                        $expr = Criteria::expr()->gte($filter->getColumn(), $valueFrom);
1✔
173
                        $this->criteria->andWhere($expr);
1✔
174
                }
175

176
                if (is_numeric($valueTo)) {
1✔
177
                        $expr = Criteria::expr()->lte($filter->getColumn(), $valueTo);
1✔
178
                        $this->criteria->andWhere($expr);
1✔
179
                }
180
        }
1✔
181

182
        protected function applyFilterText(FilterText $filter): void
1✔
183
        {
184
                $exprs = [];
1✔
185

186
                foreach ($filter->getCondition() as $column => $value) {
1✔
187
                        if ($filter->isExactSearch()) {
1✔
188
                                $exprs[] = Criteria::expr()->eq($column, $value);
1✔
189

190
                                continue;
1✔
191
                        }
192

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

195
                        foreach ($words as $word) {
1✔
196
                                $exprs[] = Criteria::expr()->contains($column, $word);
1✔
197
                        }
198
                }
199

200
                $expr = call_user_func_array([Criteria::expr(), 'orX'], $exprs);
1✔
201
                $this->criteria->andWhere($expr);
1✔
202
        }
1✔
203

204
        protected function applyFilterMultiSelect(FilterMultiSelect $filter): void
205
        {
206
                $values = $filter->getCondition()[$filter->getColumn()];
×
207

208
                $expr = Criteria::expr()->in($filter->getColumn(), $values);
×
209
                $this->criteria->andWhere($expr);
×
210
        }
211

212
        protected function applyFilterSelect(FilterSelect $filter): void
1✔
213
        {
214
                foreach ($filter->getCondition() as $column => $value) {
1✔
215
                        $expr = Criteria::expr()->eq($column, $value);
1✔
216
                        $this->criteria->andWhere($expr);
1✔
217
                }
218
        }
1✔
219

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