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

api-platform / core / 11176269767

04 Oct 2024 08:02AM UTC coverage: 7.725% (+0.3%) from 7.441%
11176269767

push

github

soyuka
chore: remove useless require-dev

12744 of 164973 relevant lines covered (7.72%)

27.04 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
    {
30
        if ($maxResults > 0) {
22✔
31
            $this->iterator = new \LimitIterator(new \ArrayIterator($results), $firstResult, $maxResults);
16✔
32
        } else {
33
            $this->iterator = new \EmptyIterator();
6✔
34
        }
35
        $this->firstResult = $firstResult;
22✔
36
        $this->maxResults = $maxResults;
22✔
37
        $this->totalItems = \count($results);
22✔
38
    }
39

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

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

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

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

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

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

80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function count(): int
84
    {
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
    {
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