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
|
|
|
public function showable(): self |
27
|
|
|
{ |
28
|
|
|
return $this->filter(function (Page $page) { |
29
|
|
|
if ($page->getVariable('published') === true |
30
|
|
|
&& $page->isVirtual() === false |
31
|
|
|
&& $page->getVariable('redirect') === null |
32
|
|
|
&& $page->getVariable('exclude') !== true) { |
33
|
|
|
return true; |
34
|
|
|
} |
35
|
|
|
}); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Alias of showable(). |
40
|
|
|
*/ |
41
|
|
|
public function all(): self |
42
|
|
|
{ |
43
|
|
|
return $this->showable(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sorts pages by date (or 'updated' date): the most recent first. |
48
|
|
|
* |
49
|
|
|
* @param array|string $options |
50
|
|
|
*/ |
51
|
|
|
public function sortByDate($options = null): self |
52
|
|
|
{ |
53
|
|
|
// backward compatibility |
54
|
|
|
if (is_string($options)) { |
55
|
|
|
$options['variable'] = $options; |
56
|
|
|
} |
57
|
|
|
// options |
58
|
|
|
$options['variable'] = $options['variable'] ?? 'date'; |
59
|
|
|
$options['descTitle'] = $options['descTitle'] ?? false; |
60
|
|
|
$options['reverse'] = $options['reverse'] ?? false; |
61
|
|
|
|
62
|
|
|
return $this->usort(function ($a, $b) use ($options) { |
63
|
|
|
if ($a[$options['variable']] == $b[$options['variable']]) { |
64
|
|
|
// if dates are equal and "descTitle" is true |
65
|
|
|
if ($options['descTitle'] && (isset($a['title']) && isset($b['title']))) { |
66
|
|
|
return $a['title'] > $b['title'] ? -1 : 1; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return 0; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return ($options['reverse'] ? -1 : 1) * ($a['date'] > $b['date'] ? -1 : 1); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sorts pages by title (natural sort). |
78
|
|
|
*/ |
79
|
|
|
public function sortByTitle($options = null): self |
80
|
|
|
{ |
81
|
|
|
// options |
82
|
|
|
if (!isset($options['reverse'])) { |
83
|
|
|
$options['reverse'] = false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->usort(function ($a, $b) use ($options) { |
87
|
|
|
return ($options['reverse'] ? -1 : 1) * strnatcmp($a['title'], $b['title']); |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sorts by weight (the heaviest first). |
93
|
|
|
*/ |
94
|
|
|
public function sortByWeight($options = null): self |
95
|
|
|
{ |
96
|
|
|
// options |
97
|
|
|
if (!isset($options['reverse'])) { |
98
|
|
|
$options['reverse'] = false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->usort(function ($a, $b) use ($options) { |
102
|
|
|
if ($a['weight'] == $b['weight']) { |
103
|
|
|
return 0; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return ($options['reverse'] ? -1 : 1) * ($a['weight'] < $b['weight'] ? -1 : 1); |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|