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

api-platform / core / 11182119487

04 Oct 2024 02:50PM UTC coverage: 7.837% (+0.4%) from 7.441%
11182119487

push

github

soyuka
Merge 4.0

0 of 266 new or added lines in 25 files covered. (0.0%)

9900 existing lines in 360 files now uncovered.

12939 of 165109 relevant lines covered (7.84%)

27.02 hits per line

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

100.0
/src/State/Pagination/ArrayPaginator.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
/**
17
 * Paginator for arrays.
18
 *
19
 * @author Alan Poulain <contact@alanpoulain.eu>
20
 */
21
final class ArrayPaginator implements \IteratorAggregate, PaginatorInterface, HasNextPagePaginatorInterface
22
{
23
    private \Traversable $iterator;
24
    private readonly int $firstResult;
25
    private readonly int $maxResults;
26
    private readonly int $totalItems;
27

28
    public function __construct(array $results, int $firstResult, int $maxResults)
29
    {
UNCOV
30
        if ($maxResults > 0) {
22✔
UNCOV
31
            $this->iterator = new \LimitIterator(new \ArrayIterator($results), $firstResult, $maxResults);
16✔
32
        } else {
UNCOV
33
            $this->iterator = new \EmptyIterator();
6✔
34
        }
UNCOV
35
        $this->firstResult = $firstResult;
22✔
UNCOV
36
        $this->maxResults = $maxResults;
22✔
UNCOV
37
        $this->totalItems = \count($results);
22✔
38
    }
39

40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getCurrentPage(): float
44
    {
UNCOV
45
        if (0 >= $this->maxResults) {
12✔
UNCOV
46
            return 1.;
3✔
47
        }
48

UNCOV
49
        return floor($this->firstResult / $this->maxResults) + 1.;
9✔
50
    }
51

52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getLastPage(): float
56
    {
UNCOV
57
        if (0 >= $this->maxResults) {
12✔
UNCOV
58
            return 1.;
3✔
59
        }
60

UNCOV
61
        return ceil($this->totalItems / $this->maxResults) ?: 1.;
9✔
62
    }
63

64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getItemsPerPage(): float
68
    {
UNCOV
69
        return (float) $this->maxResults;
12✔
70
    }
71

72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getTotalItems(): float
76
    {
UNCOV
77
        return (float) $this->totalItems;
12✔
78
    }
79

80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function count(): int
84
    {
UNCOV
85
        return iterator_count($this->iterator);
12✔
86
    }
87

88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getIterator(): \Traversable
92
    {
93
        return $this->iterator;
7✔
94
    }
95

96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function hasNextPage(): bool
100
    {
UNCOV
101
        return $this->getCurrentPage() < $this->getLastPage();
12✔
102
    }
103
}
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