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

Cecilapp / Cecil / 21594637680

02 Feb 2026 02:46PM UTC coverage: 82.582%. First build
21594637680

Pull #2306

github

web-flow
Merge 9bec826e9 into b7f499eae
Pull Request #2306: Add page status + feed/template improvements

4 of 6 new or added lines in 2 files covered. (66.67%)

3314 of 4013 relevant lines covered (82.58%)

0.83 hits per line

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

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

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
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 Cecil\Collection\Page;
15

16
use Cecil\Collection\Collection as CecilCollection;
17
use Cecil\Exception\RuntimeException;
18

19
/**
20
 * Pages collection class.
21
 *
22
 * Represents a collection of pages, providing methods to filter and sort them.
23
 */
24
class Collection extends CecilCollection
25
{
26
    /**
27
     * Returns all "showable" pages.
28
     */
29
    public function showable(array $includeStatus = []): self
30
    {
31
        return $this->filter(function (Page $page) use ($includeStatus) {
1✔
32
            // include by status
33
            if (\in_array($page->getVariable('status'), $includeStatus, true)) {
1✔
NEW
34
                return true;
×
35
            }
36
            // standard showable criteria
37
            if (
38
                $page->getVariable('published') === true      // page is published
1✔
39
                && (
40
                    $page->getVariable('excluded') !== true   // page is listed
1✔
41
                    && $page->getVariable('exclude') !== true // backward compatibility
1✔
42
                )
43
                && $page->isVirtual() === false               // page is created from a file
1✔
44
                && $page->getVariable('redirect') === null    // page is not a redirection
1✔
45
            ) {
46
                return true;
1✔
47
            }
48
            return false;
1✔
49
        });
1✔
50
    }
51

52
    /**
53
     * Alias of showable().
54
     */
55
    public function all(array $includeStatus = []): self
56
    {
NEW
57
        return $this->showable($includeStatus);
×
58
    }
59

60
    /**
61
     * Sorts pages by.
62
     *
63
     * $options:
64
     * [date|updated|title|weight]
65
     * or
66
     * [
67
     *   variable   => date|updated|title|weight
68
     *   desc_title => false|true
69
     *   reverse    => false|true
70
     * ]
71
     */
72
    public function sortBy(string|array|null $options): self
73
    {
74
        $sortBy = \is_string($options) ? $options : $options['variable'] ?? 'date';
1✔
75
        $sortMethod = \sprintf('sortBy%s', ucfirst(str_replace('updated', 'date', $sortBy)));
1✔
76
        if (!method_exists($this, $sortMethod)) {
1✔
77
            throw new RuntimeException(\sprintf('"%s" is not a valid value for `sortby` to sort collection "%s".', $sortBy, $this->getId()));
×
78
        }
79

80
        return $this->$sortMethod($options);
1✔
81
    }
82

83
    /**
84
     * Sorts pages by date (or 'updated'): the most recent first.
85
     */
86
    public function sortByDate(string|array|null $options = null): self
87
    {
88
        $opt = [];
1✔
89
        // backward compatibility (i.e. $options = 'updated')
90
        if (\is_string($options)) {
1✔
91
            $opt['variable'] = $options;
1✔
92
        }
93
        // options
94
        $opt['variable'] = $options['variable'] ?? 'date';
1✔
95
        $opt['descTitle'] = $options['descTitle'] ?? false;
1✔
96
        $opt['reverse'] = $options['reverse'] ?? false;
1✔
97
        // sort
98
        $pages = $this->usort(function ($a, $b) use ($opt) {
1✔
99
            if ($a[$opt['variable']] == $b[$opt['variable']]) {
1✔
100
                // if dates are equal and "descTitle" is true
101
                if ($opt['descTitle'] && (isset($a['title']) && isset($b['title']))) {
1✔
102
                    return strnatcmp($b['title'], $a['title']);
×
103
                }
104

105
                return 0;
1✔
106
            }
107

108
            return $a[$opt['variable']] > $b[$opt['variable']] ? -1 : 1;
1✔
109
        });
1✔
110
        if ($opt['reverse']) {
1✔
111
            $pages = $pages->reverse();
×
112
        }
113

114
        return $pages;
1✔
115
    }
116

117
    /**
118
     * Sorts pages by title (natural sort).
119
     */
120
    public function sortByTitle(string|array|null $options = null): self
121
    {
122
        $opt = [];
1✔
123
        // options
124
        $opt['reverse'] = $options['reverse'] ?? false;
1✔
125
        // sort
126
        return $this->usort(function ($a, $b) use ($opt) {
1✔
127
            return ($opt['reverse'] ? -1 : 1) * strnatcmp($a['title'], $b['title']);
1✔
128
        });
1✔
129
    }
130

131
    /**
132
     * Sorts by weight (the heaviest first).
133
     */
134
    public function sortByWeight(string|array|null $options = null): self
135
    {
136
        $opt = [];
×
137
        // options
138
        $opt['reverse'] = $options['reverse'] ?? false;
×
139
        // sort
140
        return $this->usort(function ($a, $b) use ($opt) {
×
141
            if ($a['weight'] == $b['weight']) {
×
142
                return 0;
×
143
            }
144

145
            return ($opt['reverse'] ? -1 : 1) * ($a['weight'] < $b['weight'] ? -1 : 1);
×
146
        });
×
147
    }
148

149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function get(string $id): Page
153
    {
154
        return parent::get($id);
1✔
155
    }
156

157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function first(): ?Page
161
    {
162
        return parent::first();
1✔
163
    }
164

165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function filter(\Closure $callback): self
169
    {
170
        return parent::filter($callback);
1✔
171
    }
172

173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function usort(?\Closure $callback = null): self
177
    {
178
        return parent::usort($callback);
1✔
179
    }
180

181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function reverse(): self
185
    {
186
        return parent::reverse();
×
187
    }
188
}
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