|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Cecil. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
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
|
1 |
|
public function showable(): self |
|
27
|
|
|
{ |
|
28
|
1 |
|
return $this->filter(function (Page $page) { |
|
29
|
|
|
if ( |
|
30
|
1 |
|
$page->getVariable('published') === true |
|
31
|
1 |
|
&& $page->isVirtual() === false |
|
32
|
1 |
|
&& $page->getVariable('redirect') === null |
|
33
|
1 |
|
&& $page->getVariable('exclude') !== true |
|
34
|
|
|
) { |
|
35
|
1 |
|
return true; |
|
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
|
1 |
|
public function sortByDate($options = null): self |
|
54
|
|
|
{ |
|
55
|
|
|
// backward compatibility |
|
56
|
1 |
|
if (\is_string($options)) { |
|
57
|
|
|
$options['variable'] = $options; |
|
58
|
|
|
} |
|
59
|
|
|
// options |
|
60
|
1 |
|
$options['variable'] = $options['variable'] ?? 'date'; |
|
61
|
1 |
|
$options['descTitle'] = $options['descTitle'] ?? false; |
|
62
|
1 |
|
$options['reverse'] = $options['reverse'] ?? false; |
|
63
|
|
|
|
|
64
|
1 |
|
$pages = $this->usort(function ($a, $b) use ($options) { |
|
65
|
1 |
|
if ($a[$options['variable']] == $b[$options['variable']]) { |
|
66
|
|
|
// if dates are equal and "descTitle" is true |
|
67
|
1 |
|
if ($options['descTitle'] && (isset($a['title']) && isset($b['title']))) { |
|
68
|
|
|
return strnatcmp($b['title'], $a['title']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return $a[$options['variable']] > $b[$options['variable']] ? -1 : 1; |
|
75
|
1 |
|
}); |
|
76
|
1 |
|
if ($options['reverse']) { |
|
77
|
|
|
$pages = $pages->reverse(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return $pages; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sorts pages by title (natural sort). |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function sortByTitle($options = null): self |
|
87
|
|
|
{ |
|
88
|
|
|
// options |
|
89
|
1 |
|
if (!isset($options['reverse'])) { |
|
90
|
|
|
$options['reverse'] = false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
return $this->usort(function ($a, $b) use ($options) { |
|
94
|
1 |
|
return ($options['reverse'] ? -1 : 1) * strnatcmp($a['title'], $b['title']); |
|
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
|
1 |
|
public function get(string $id): Page |
|
121
|
|
|
{ |
|
122
|
1 |
|
return parent::get($id); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function first(): ?Page |
|
129
|
|
|
{ |
|
130
|
1 |
|
return parent::first(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function filter(\Closure $callback): self |
|
137
|
|
|
{ |
|
138
|
1 |
|
return parent::filter($callback); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function usort(\Closure $callback = null): self |
|
145
|
|
|
{ |
|
146
|
1 |
|
return parent::usort($callback); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function reverse(): self |
|
153
|
|
|
{ |
|
154
|
|
|
return parent::reverse(); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|