• 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

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

3
namespace Contributte\Datagrid\DataSource;
4

5
use Contributte\Datagrid\Utils\Sorting;
6
use UnexpectedValueException;
7

8
class ApiDataSource implements IDataSource
9
{
10

11
        /** @var array */
12
        protected array $data = [];
13

14
        protected ?string $sortColumn = null;
15

16
        protected ?string $orderColumn = null;
17

18
        protected ?int $limit = null;
19

20
        protected ?int $offset = null;
21

22
        protected int $filterOne = 0;
23

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

NEW
27
        public function __construct(protected string $url, protected array $queryParams = [])
×
28
        {
29
        }
30

31
        public function getCount(): int
32
        {
33
                return $this->getResponse(['count' => '']);
×
34
        }
35

36
        /**
37
         * {@inheritDoc}
38
         */
39
        public function getData(): array
40
        {
41
                return $this->data !== [] ? $this->data : $this->getResponse([
×
42
                        'sort' => $this->sortColumn,
×
43
                        'order' => $this->orderColumn,
×
44
                        'limit' => $this->limit,
×
45
                        'offset' => $this->offset,
×
46
                        'filter' => $this->filter,
×
47
                        'one' => $this->filterOne,
×
48
                ]);
49
        }
50

51
        /**
52
         * {@inheritDoc}
53
         */
54
        public function filter(array $filters): void
55
        {
56
                /**
57
                 * First, save all filter values to array
58
                 */
59
                foreach ($filters as $filter) {
×
60
                        if ($filter->isValueSet() && $filter->getConditionCallback() === null) {
×
61
                                $this->filter[$filter->getKey()] = $filter->getCondition();
×
62
                        }
63
                }
64

65
                /**
66
                 * Download filtered data
67
                 */
68
                $this->data = $this->getData();
×
69

70
                /**
71
                 * Apply possible user filter callbacks
72
                 */
73
                foreach ($filters as $filter) {
×
74
                        if ($filter->isValueSet() && $filter->getConditionCallback() !== null) {
×
75
                                $this->data = (array) call_user_func_array(
×
76
                                        $filter->getConditionCallback(),
×
77
                                        [$this->data, $filter->getValue()]
×
78
                                );
79
                        }
80
                }
81
        }
82

83
        /**
84
         * {@inheritDoc}
85
         */
86
        public function filterOne(array $condition): IDataSource
87
        {
88
                $this->filter = $condition;
×
89
                $this->filterOne = 1;
×
90

91
                return $this;
×
92
        }
93

94
        public function limit(int $offset, int $limit): IDataSource
95
        {
96
                $this->offset = $offset;
×
97
                $this->limit = $limit;
×
98

99
                return $this;
×
100
        }
101

102
        public function sort(Sorting $sorting): IDataSource
103
        {
104
                /**
105
                 * there is only one iteration
106
                 */
107
                foreach ($sorting->getSort() as $column => $order) {
×
108
                        $this->sortColumn = $column;
×
109
                        $this->orderColumn = $order;
×
110
                }
111

112
                return $this;
×
113
        }
114

115
        /**
116
         * Get data of remote source
117
         */
118
        protected function getResponse(array $params = []): mixed
119
        {
120
                $queryString = http_build_query($params + $this->queryParams);
×
121
                $url = sprintf('%s?%s', $this->url, $queryString);
×
122

123
                $content = file_get_contents($url);
×
124

125
                if ($content === false) {
×
126
                        throw new UnexpectedValueException(sprintf('Could not open URL %s', $url));
×
127
                }
128

NEW
129
                return json_decode($content, null, 512, JSON_THROW_ON_ERROR);
×
130
        }
131

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