|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cecil. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
|
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(): self |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->filter(function (Page $page) { |
|
32
|
|
|
if ( |
|
33
|
|
|
$page->getVariable('published') === true // page is published |
|
34
|
|
|
&& ( |
|
35
|
|
|
$page->getVariable('excluded') !== true // page is listed |
|
36
|
|
|
&& $page->getVariable('exclude') !== true // backward compatibility |
|
37
|
|
|
) |
|
38
|
|
|
&& $page->isVirtual() === false // page is created from a file |
|
39
|
|
|
&& $page->getVariable('redirect') === null // page is not a redirection |
|
40
|
|
|
) { |
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
}); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Alias of showable(). |
|
48
|
|
|
*/ |
|
49
|
|
|
public function all(): self |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->showable(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sorts pages by. |
|
56
|
|
|
* |
|
57
|
|
|
* $options: |
|
58
|
|
|
* [date|updated|title|weight] |
|
59
|
|
|
* or |
|
60
|
|
|
* [ |
|
61
|
|
|
* variable => date|updated|title|weight |
|
62
|
|
|
* desc_title => false|true |
|
63
|
|
|
* reverse => false|true |
|
64
|
|
|
* ] |
|
65
|
|
|
*/ |
|
66
|
|
|
public function sortBy(string|array|null $options): self |
|
67
|
|
|
{ |
|
68
|
|
|
$sortBy = \is_string($options) ? $options : $options['variable'] ?? 'date'; |
|
|
|
|
|
|
69
|
|
|
$sortMethod = \sprintf('sortBy%s', ucfirst(str_replace('updated', 'date', $sortBy))); |
|
70
|
|
|
if (!method_exists($this, $sortMethod)) { |
|
71
|
|
|
throw new RuntimeException(\sprintf('"%s" is not a valid value for `sortby` to sort collection "%s".', $sortBy, $this->getId())); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->$sortMethod($options); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sorts pages by date (or 'updated'): the most recent first. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function sortByDate(string|array|null $options = null): self |
|
81
|
|
|
{ |
|
82
|
|
|
$opt = []; |
|
83
|
|
|
// backward compatibility (i.e. $options = 'updated') |
|
84
|
|
|
if (\is_string($options)) { |
|
|
|
|
|
|
85
|
|
|
$opt['variable'] = $options; |
|
86
|
|
|
} |
|
87
|
|
|
// options |
|
88
|
|
|
$opt['variable'] = $options['variable'] ?? 'date'; |
|
89
|
|
|
$opt['descTitle'] = $options['descTitle'] ?? false; |
|
90
|
|
|
$opt['reverse'] = $options['reverse'] ?? false; |
|
91
|
|
|
// sort |
|
92
|
|
|
$pages = $this->usort(function ($a, $b) use ($opt) { |
|
93
|
|
|
if ($a[$opt['variable']] == $b[$opt['variable']]) { |
|
94
|
|
|
// if dates are equal and "descTitle" is true |
|
95
|
|
|
if ($opt['descTitle'] && (isset($a['title']) && isset($b['title']))) { |
|
96
|
|
|
return strnatcmp($b['title'], $a['title']); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return 0; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $a[$opt['variable']] > $b[$opt['variable']] ? -1 : 1; |
|
103
|
|
|
}); |
|
104
|
|
|
if ($opt['reverse']) { |
|
105
|
|
|
$pages = $pages->reverse(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $pages; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Sorts pages by title (natural sort). |
|
113
|
|
|
*/ |
|
114
|
|
|
public function sortByTitle(string|array|null $options = null): self |
|
115
|
|
|
{ |
|
116
|
|
|
$opt = []; |
|
117
|
|
|
// options |
|
118
|
|
|
$opt['reverse'] = $options['reverse'] ?? false; |
|
119
|
|
|
// sort |
|
120
|
|
|
return $this->usort(function ($a, $b) use ($opt) { |
|
121
|
|
|
return ($opt['reverse'] ? -1 : 1) * strnatcmp($a['title'], $b['title']); |
|
122
|
|
|
}); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sorts by weight (the heaviest first). |
|
127
|
|
|
*/ |
|
128
|
|
|
public function sortByWeight(string|array|null $options = null): self |
|
129
|
|
|
{ |
|
130
|
|
|
$opt = []; |
|
131
|
|
|
// options |
|
132
|
|
|
$opt['reverse'] = $options['reverse'] ?? false; |
|
133
|
|
|
// sort |
|
134
|
|
|
return $this->usort(function ($a, $b) use ($opt) { |
|
135
|
|
|
if ($a['weight'] == $b['weight']) { |
|
136
|
|
|
return 0; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return ($opt['reverse'] ? -1 : 1) * ($a['weight'] < $b['weight'] ? -1 : 1); |
|
140
|
|
|
}); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
public function get(string $id): Page |
|
147
|
|
|
{ |
|
148
|
|
|
return parent::get($id); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
|
|
public function first(): ?Page |
|
155
|
|
|
{ |
|
156
|
|
|
return parent::first(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritdoc} |
|
161
|
|
|
*/ |
|
162
|
|
|
public function filter(\Closure $callback): self |
|
163
|
|
|
{ |
|
164
|
|
|
return parent::filter($callback); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* {@inheritdoc} |
|
169
|
|
|
*/ |
|
170
|
|
|
public function usort(?\Closure $callback = null): self |
|
171
|
|
|
{ |
|
172
|
|
|
return parent::usort($callback); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
*/ |
|
178
|
|
|
public function reverse(): self |
|
179
|
|
|
{ |
|
180
|
|
|
return parent::reverse(); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|