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

contributte / datagrid / 8264560336

13 Mar 2024 12:24PM UTC coverage: 34.102% (-10.4%) from 44.533%
8264560336

push

github

web-flow
[7.x] Next (#1060)

* PHP 8.0

* Translator: switch from ITranslator to Translator (#973)

* [7.x] Bootstrap 5 + PHP 8 + vanilla javascript (#1021)

* Bootstrap 5

* Bootstrap 5 (docs)

* Bootstrap 5

* form-control -> form-select

* Bump bootstrap-select for Bootstrap 5 support

* Removed `input-sm` from Bootstrap 3

See https://getbootstrap.com/docs/4.0/migration/#forms-1

* Bootstrap 5: When selectpicker, replace form-select classes with form-control and refresh it

* Hide `underline` also for `dropdown-item`. And merged into one CSS rule.

* Update the filterMultiSelect initialization

* Text-align: left -> start

Co-authored-by: Radim Vaculík <radim.vaculik@gmail.com>
Co-authored-by: Jaroslav Líbal <jaroslav.libal@neatous.cz>

* [7.x] phpstan-deprecation-rules (#1061)

* Fix sort

* Add method for setting custom Action href, v6.x (#853)

* [7.x] Nextras ORM 4 support, closes #984

* Fix ElasticsearchDataSource.php data source (#1041)

* Error: Typed property Ublaboo\DataGrid\InlineEdit\InlineEdit::$itemID must not be accessed before initialization

* composer: allow-plugins: php-http/discovery

* ItemDetailForm: $httpPost: Typed property must not be accessed...

* phpstan: revert  --memory-limit=4G

* NetteDatabaseSelectionHelper: Context -> Explorer, getSupplementalDriver -> getDriver

* Update README.md

* Templates: fix variables

* data-bs-toggle attribute for multiaction button (#1072)

* dependabot.yml (#1078)

* Allow nette/utils:4.0 (#1077)

* Add onColumnShow and onColumnHide event methods (#1076)

* Add onColumnShow and onColumnHide event methods

* Add native type array

* Removed duplicity

* Return value of BackedEnum when reading Doctrine entity property. (#1081)

* Return value of BackedEnum when reading array value (#1083)

* Added method to check if filter is on default values; Closes #1082 (#1084)

* ublaboo -> contributte (#1067) (#1075)

* Delete depe... (continued)

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

613 existing lines in 31 files now uncovered.

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

0.0
/src/DataSource/ElasticsearchDataSource.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Datagrid\DataSource;
4

5
use Contributte\Datagrid\Filter\FilterDate;
6
use Contributte\Datagrid\Filter\FilterDateRange;
7
use Contributte\Datagrid\Filter\FilterMultiSelect;
8
use Contributte\Datagrid\Filter\FilterRange;
9
use Contributte\Datagrid\Filter\FilterSelect;
10
use Contributte\Datagrid\Filter\FilterText;
11
use Contributte\Datagrid\Utils\DateTimeHelper;
12
use Contributte\Datagrid\Utils\Sorting;
13
use Elastic\Elasticsearch\Client;
14
use RuntimeException;
15
use UnexpectedValueException;
16

17
class ElasticsearchDataSource extends FilterableDataSource implements IDataSource
18
{
19

20
        protected SearchParamsBuilder $searchParamsBuilder;
21

22
        /** @var callable */
23
        private $rowFactory;
24

NEW
25
        public function __construct(private Client $client, string $indexName, ?callable $rowFactory = null)
×
26
        {
UNCOV
27
                $this->searchParamsBuilder = new SearchParamsBuilder($indexName);
×
28

UNCOV
29
                if ($rowFactory === null) {
×
NEW
30
                        $rowFactory = static fn (array $hit): array => $hit['_source'];
×
31
                }
32

UNCOV
33
                $this->rowFactory = $rowFactory;
×
34
        }
35

36
        public function getCount(): int
37
        {
UNCOV
38
                $searchResult = $this->client->search($this->searchParamsBuilder->buildParams());
×
39

UNCOV
40
                if (!isset($searchResult['hits'])) {
×
NEW
41
                        throw new UnexpectedValueException();
×
42
                }
43

UNCOV
44
                return is_array($searchResult['hits']['total'])
×
UNCOV
45
                        ? $searchResult['hits']['total']['value']
×
UNCOV
46
                        : $searchResult['hits']['total'];
×
47
        }
48

49
        /**
50
         * {@inheritDoc}
51
         */
52
        public function getData(): array
53
        {
UNCOV
54
                $searchResult = $this->client->search($this->searchParamsBuilder->buildParams());
×
55

UNCOV
56
                if (!isset($searchResult['hits'])) {
×
NEW
57
                        throw new UnexpectedValueException();
×
58
                }
59

UNCOV
60
                return array_map($this->rowFactory, $searchResult['hits']['hits']);
×
61
        }
62

63
        /**
64
         * {@inheritDoc}
65
         */
66
        public function filterOne(array $condition): IDataSource
67
        {
UNCOV
68
                foreach ($condition as $value) {
×
UNCOV
69
                        $this->searchParamsBuilder->addIdsQuery($value);
×
70
                }
71

UNCOV
72
                return $this;
×
73
        }
74

75
        public function limit(int $offset, int $limit): IDataSource
76
        {
UNCOV
77
                $this->searchParamsBuilder->setFrom($offset);
×
UNCOV
78
                $this->searchParamsBuilder->setSize($limit);
×
79

UNCOV
80
                return $this;
×
81
        }
82

83
        public function applyFilterDate(FilterDate $filter): void
84
        {
UNCOV
85
                foreach ($filter->getCondition() as $column => $value) {
×
UNCOV
86
                        $timestampFrom = null;
×
UNCOV
87
                        $timestampTo = null;
×
88

UNCOV
89
                        if ($value) {
×
UNCOV
90
                                $dateFrom = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]);
×
UNCOV
91
                                $dateFrom->setTime(0, 0, 0);
×
92

UNCOV
93
                                $timestampFrom = $dateFrom->getTimestamp();
×
94

UNCOV
95
                                $dateTo = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]);
×
UNCOV
96
                                $dateTo->setTime(23, 59, 59);
×
97

UNCOV
98
                                $timestampTo = $dateTo->getTimestamp();
×
99

UNCOV
100
                                $this->searchParamsBuilder->addRangeQuery($column, $timestampFrom, $timestampTo);
×
101
                        }
102
                }
103
        }
104

105
        public function applyFilterDateRange(FilterDateRange $filter): void
106
        {
UNCOV
107
                foreach ($filter->getCondition() as $column => $values) {
×
UNCOV
108
                        $timestampFrom = null;
×
UNCOV
109
                        $timestampTo = null;
×
110

UNCOV
111
                        if ($values['from']) {
×
UNCOV
112
                                $dateFrom = DateTimeHelper::tryConvertToDateTime($values['from'], [$filter->getPhpFormat()]);
×
UNCOV
113
                                $dateFrom->setTime(0, 0, 0);
×
114

UNCOV
115
                                $timestampFrom = $dateFrom->getTimestamp();
×
116
                        }
117

UNCOV
118
                        if ($values['to']) {
×
UNCOV
119
                                $dateTo = DateTimeHelper::tryConvertToDateTime($values['to'], [$filter->getPhpFormat()]);
×
UNCOV
120
                                $dateTo->setTime(23, 59, 59);
×
121

UNCOV
122
                                $timestampTo = $dateTo->getTimestamp();
×
123
                        }
124

UNCOV
125
                        if (is_int($timestampFrom) || is_int($timestampTo)) {
×
UNCOV
126
                                $this->searchParamsBuilder->addRangeQuery($column, $timestampFrom, $timestampTo);
×
127
                        }
128
                }
129
        }
130

131
        public function applyFilterRange(FilterRange $filter): void
132
        {
UNCOV
133
                foreach ($filter->getCondition() as $column => $value) {
×
UNCOV
134
                        $this->searchParamsBuilder->addRangeQuery($column, $value['from'] ?? null, $value['to'] ?? null);
×
135
                }
136
        }
137

138
        public function applyFilterText(FilterText $filter): void
139
        {
UNCOV
140
                foreach ($filter->getCondition() as $column => $value) {
×
UNCOV
141
                        if ($filter->isExactSearch()) {
×
UNCOV
142
                                $this->searchParamsBuilder->addMatchQuery($column, $value);
×
143
                        } else {
UNCOV
144
                                $this->searchParamsBuilder->addPhrasePrefixQuery($column, $value);
×
145
                        }
146
                }
147
        }
148

149
        public function applyFilterMultiSelect(FilterMultiSelect $filter): void
150
        {
UNCOV
151
                foreach ($filter->getCondition() as $column => $values) {
×
UNCOV
152
                        $this->searchParamsBuilder->addBooleanMatchQuery($column, $values);
×
153
                }
154
        }
155

156
        public function applyFilterSelect(FilterSelect $filter): void
157
        {
UNCOV
158
                foreach ($filter->getCondition() as $column => $value) {
×
UNCOV
159
                        $this->searchParamsBuilder->addMatchQuery($column, $value);
×
160
                }
161
        }
162

163
        /**
164
         * {@inheritDoc}
165
         *
166
         * @throws RuntimeException
167
         */
168
        public function sort(Sorting $sorting): IDataSource
169
        {
UNCOV
170
                if (is_callable($sorting->getSortCallback())) {
×
NEW
171
                        throw new RuntimeException('No can do - not implemented yet');
×
172
                }
173

UNCOV
174
                foreach ($sorting->getSort() as $column => $order) {
×
UNCOV
175
                        $this->searchParamsBuilder->setSort(
×
UNCOV
176
                                [$column => ['order' => strtolower($order)]]
×
177
                        );
178
                }
179

UNCOV
180
                return $this;
×
181
        }
182

183
        protected function getDataSource(): SearchParamsBuilder
184
        {
NEW
185
                return $this->searchParamsBuilder;
×
186
        }
187

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