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

Cecilapp / Cecil / 5051201718

pending completion
5051201718

push

github

Arnaud Ligny
fix: pagination sorting

10 of 10 new or added lines in 1 file covered. (100.0%)

2789 of 4129 relevant lines covered (67.55%)

0.68 hits per line

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

67.5
/src/Collection/Page/Collection.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Cecil\Collection\Page;
15

16
use Cecil\Collection\Collection as CecilCollection;
17

18
/**
19
 * Class Collection.
20
 */
21
class Collection extends CecilCollection
22
{
23
    /**
24
     * Returns all "showable" pages.
25
     */
26
    public function showable(): self
27
    {
28
        return $this->filter(function (Page $page) {
1✔
29
            if (
30
                $page->getVariable('published') === true
1✔
31
                && $page->isVirtual() === false
1✔
32
                && $page->getVariable('redirect') === null
1✔
33
                && $page->getVariable('exclude') !== true
1✔
34
            ) {
35
                return true;
1✔
36
            }
37
        });
1✔
38
    }
39

40
    /**
41
     * Alias of showable().
42
     */
43
    public function all(): self
44
    {
45
        return $this->showable();
×
46
    }
47

48
    /**
49
     * Sorts pages by date (or 'updated' date): the most recent first.
50
     *
51
     * @param array|string $options
52
     */
53
    public function sortByDate($options = null): self
54
    {
55
        // backward compatibility
56
        if (\is_string($options)) {
1✔
57
            $options['variable'] = $options;
×
58
        }
59
        // options
60
        $options['variable'] = $options['variable'] ?? 'date';
1✔
61
        $options['descTitle'] = $options['descTitle'] ?? false;
1✔
62
        $options['reverse'] = $options['reverse'] ?? false;
1✔
63

64
        $pages = $this->usort(function ($a, $b) use ($options) {
1✔
65
            if ($a[$options['variable']] == $b[$options['variable']]) {
1✔
66
                // if dates are equal and "descTitle" is true
67
                if ($options['descTitle'] && (isset($a['title']) && isset($b['title']))) {
1✔
68
                    return strnatcmp($b['title'], $a['title']);
×
69
                }
70

71
                return 0;
1✔
72
            }
73

74
            return $a[$options['variable']] > $b[$options['variable']] ? -1 : 1;
1✔
75
        });
1✔
76
        if ($options['reverse']) {
1✔
77
            $pages = $pages->reverse();
×
78
        }
79

80
        return $pages;
1✔
81
    }
82

83
    /**
84
     * Sorts pages by title (natural sort).
85
     */
86
    public function sortByTitle($options = null): self
87
    {
88
        // options
89
        if (!isset($options['reverse'])) {
1✔
90
            $options['reverse'] = false;
×
91
        }
92

93
        return $this->usort(function ($a, $b) use ($options) {
1✔
94
            return ($options['reverse'] ? -1 : 1) * strnatcmp($a['title'], $b['title']);
1✔
95
        });
1✔
96
    }
97

98
    /**
99
     * Sorts by weight (the heaviest first).
100
     */
101
    public function sortByWeight($options = null): self
102
    {
103
        // options
104
        if (!isset($options['reverse'])) {
×
105
            $options['reverse'] = false;
×
106
        }
107

108
        return $this->usort(function ($a, $b) use ($options) {
×
109
            if ($a['weight'] == $b['weight']) {
×
110
                return 0;
×
111
            }
112

113
            return ($options['reverse'] ? -1 : 1) * ($a['weight'] < $b['weight'] ? -1 : 1);
×
114
        });
×
115
    }
116

117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function get(string $id): Page
121
    {
122
        return parent::get($id);
1✔
123
    }
124

125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function first(): ?Page
129
    {
130
        return parent::first();
1✔
131
    }
132

133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function filter(\Closure $callback): self
137
    {
138
        return parent::filter($callback);
1✔
139
    }
140

141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function usort(\Closure $callback = null): self
145
    {
146
        return parent::usort($callback);
1✔
147
    }
148

149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function reverse(): self
153
    {
154
        return parent::reverse();
×
155
    }
156
}
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

© 2026 Coveralls, Inc