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

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

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

97.37
/src/State/Pagination/Pagination.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\State\Pagination;
15

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use ApiPlatform\Metadata\Operation;
18

19
/**
20
 * Pagination configuration.
21
 *
22
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
23
 */
24
final class Pagination
25
{
26
    private readonly array $options;
27
    private readonly array $graphQlOptions;
28

29
    public function __construct(array $options = [], array $graphQlOptions = [])
30
    {
31
        $this->options = array_merge([
1,409✔
32
            'enabled' => true,
1,409✔
33
            'client_enabled' => false,
1,409✔
34
            'client_items_per_page' => false,
1,409✔
35
            'items_per_page' => 30,
1,409✔
36
            'page_default' => 1,
1,409✔
37
            'page_parameter_name' => 'page',
1,409✔
38
            'enabled_parameter_name' => 'pagination',
1,409✔
39
            'items_per_page_parameter_name' => 'itemsPerPage',
1,409✔
40
            'maximum_items_per_page' => null,
1,409✔
41
            'partial' => false,
1,409✔
42
            'client_partial' => false,
1,409✔
43
            'partial_parameter_name' => 'partial',
1,409✔
44
        ], $options);
1,409✔
45
        $this->graphQlOptions = array_merge([
1,409✔
46
            'enabled' => true,
1,409✔
47
        ], $graphQlOptions);
1,409✔
48
    }
49

50
    /**
51
     * Gets the current page.
52
     *
53
     * @throws InvalidArgumentException
54
     */
55
    public function getPage(array $context = []): int
56
    {
57
        $page = (int) $this->getParameterFromContext(
771✔
58
            $context,
771✔
59
            $this->options['page_parameter_name'],
771✔
60
            $this->options['page_default']
771✔
61
        );
771✔
62

63
        if (1 > $page) {
771✔
64
            throw new InvalidArgumentException('Page should not be less than 1');
2✔
65
        }
66

67
        return $page;
769✔
68
    }
69

70
    /**
71
     * Gets the current offset.
72
     */
73
    public function getOffset(?Operation $operation = null, array $context = []): int
74
    {
75
        $graphql = (bool) ($context['graphql_operation_name'] ?? false);
769✔
76

77
        $limit = $this->getLimit($operation, $context);
769✔
78

79
        if ($graphql && null !== ($after = $this->getParameterFromContext($context, 'after'))) {
769✔
80
            return false === ($after = base64_decode($after, true)) ? 0 : (int) $after + 1;
3✔
81
        }
82

83
        if ($graphql && null !== ($before = $this->getParameterFromContext($context, 'before'))) {
769✔
84
            return ($offset = (false === ($before = base64_decode($before, true)) ? 0 : (int) $before - $limit)) < 0 ? 0 : $offset;
2✔
85
        }
86

87
        if ($graphql && null !== ($last = $this->getParameterFromContext($context, 'last'))) {
769✔
88
            return ($offset = ($context['count'] ?? 0) - $last) < 0 ? 0 : $offset;
2✔
89
        }
90

91
        $offset = ($this->getPage($context) - 1) * $limit;
767✔
92

93
        if (!\is_int($offset)) {
767✔
94
            throw new InvalidArgumentException('Page parameter is too large.');
2✔
95
        }
96

97
        return $offset;
765✔
98
    }
99

100
    /**
101
     * Gets the current limit.
102
     *
103
     * @throws InvalidArgumentException
104
     */
105
    public function getLimit(?Operation $operation = null, array $context = []): int
106
    {
107
        $graphql = (bool) ($context['graphql_operation_name'] ?? false);
769✔
108

109
        $limit = $operation?->getPaginationItemsPerPage() ?? $this->options['items_per_page'];
769✔
110
        $clientLimit = $operation?->getPaginationClientItemsPerPage() ?? $this->options['client_items_per_page'];
769✔
111

112
        if ($graphql && null !== ($first = $this->getParameterFromContext($context, 'first'))) {
769✔
113
            $limit = $first;
11✔
114
        }
115

116
        if ($graphql && null !== ($last = $this->getParameterFromContext($context, 'last'))) {
769✔
117
            $limit = $last;
2✔
118
        }
119

120
        if ($graphql && null !== ($before = $this->getParameterFromContext($context, 'before'))
769✔
121
            && (false === ($before = base64_decode($before, true)) ? 0 : (int) $before - $limit) < 0) {
769✔
122
            $limit = (int) $before;
2✔
123
        }
124

125
        if ($clientLimit) {
769✔
126
            $limit = (int) $this->getParameterFromContext($context, $this->options['items_per_page_parameter_name'], $limit);
769✔
127
            $maxItemsPerPage = $operation?->getPaginationMaximumItemsPerPage() ?? $this->options['maximum_items_per_page'];
769✔
128

129
            if (null !== $maxItemsPerPage && $limit > $maxItemsPerPage) {
769✔
UNCOV
130
                $limit = $maxItemsPerPage;
1✔
131
            }
132
        }
133

134
        if (0 > $limit) {
769✔
135
            throw new InvalidArgumentException('Limit should not be less than 0');
×
136
        }
137

138
        return $limit;
769✔
139
    }
140

141
    /**
142
     * Gets info about the pagination.
143
     *
144
     * Returns an array with the following info as values:
145
     *   - the page {@see Pagination::getPage}
146
     *   - the offset {@see Pagination::getOffset}
147
     *   - the limit {@see Pagination::getLimit}
148
     *
149
     * @throws InvalidArgumentException
150
     */
151
    public function getPagination(?Operation $operation = null, array $context = []): array
152
    {
153
        $page = $this->getPage($context);
771✔
154
        $limit = $this->getLimit($operation, $context);
769✔
155

156
        if (0 === $limit && 1 < $page) {
769✔
157
            throw new InvalidArgumentException('Page should not be greater than 1 if limit is equal to 0');
2✔
158
        }
159

160
        return [$page, $this->getOffset($operation, $context), $limit];
769✔
161
    }
162

163
    /**
164
     * Is the pagination enabled?
165
     */
166
    public function isEnabled(?Operation $operation = null, array $context = []): bool
167
    {
168
        return $this->getEnabled($context, $operation);
726✔
169
    }
170

171
    /**
172
     * Is the pagination enabled for GraphQL?
173
     */
174
    public function isGraphQlEnabled(?Operation $operation = null, array $context = []): bool
175
    {
176
        return $this->getGraphQlEnabled($operation);
284✔
177
    }
178

179
    /**
180
     * Is the partial pagination enabled?
181
     */
182
    public function isPartialEnabled(?Operation $operation = null, array $context = []): bool
183
    {
184
        return $this->getEnabled($context, $operation, true);
498✔
185
    }
186

187
    public function getOptions(): array
188
    {
189
        return $this->options;
278✔
190
    }
191

192
    public function getGraphQlPaginationType(Operation $operation): string
193
    {
194
        return $operation->getPaginationType() ?? 'cursor';
282✔
195
    }
196

197
    /**
198
     * Is the classic or partial pagination enabled?
199
     */
200
    private function getEnabled(array $context, ?Operation $operation = null, bool $partial = false): bool
201
    {
202
        $enabled = $this->options[$partial ? 'partial' : 'enabled'];
776✔
203
        $clientEnabled = $this->options[$partial ? 'client_partial' : 'client_enabled'];
776✔
204

205
        $enabled = ($partial ? $operation?->getPaginationPartial() : $operation?->getPaginationEnabled()) ?? $enabled;
776✔
206
        $clientEnabled = ($partial ? $operation?->getPaginationClientPartial() : $operation?->getPaginationClientEnabled()) ?? $clientEnabled;
776✔
207

208
        if ($clientEnabled) {
776✔
209
            return filter_var($this->getParameterFromContext($context, $this->options[$partial ? 'partial_parameter_name' : 'enabled_parameter_name'], $enabled), \FILTER_VALIDATE_BOOLEAN);
776✔
210
        }
211

212
        return (bool) $enabled;
×
213
    }
214

215
    private function getGraphQlEnabled(?Operation $operation): bool
216
    {
217
        $enabled = $this->graphQlOptions['enabled'];
284✔
218

219
        return $operation?->getPaginationEnabled() ?? $enabled;
284✔
220
    }
221

222
    /**
223
     * Gets the given pagination parameter name from the given context.
224
     */
225
    private function getParameterFromContext(array $context, string $parameterName, mixed $default = null)
226
    {
227
        $filters = $context['filters'] ?? [];
776✔
228

229
        return \array_key_exists($parameterName, $filters) ? $filters[$parameterName] : $default;
776✔
230
    }
231
}
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