Passed
Branch code (e65214)
by Arnaud
03:34
created

Collection::showable()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Collection\Page;
12
13
use Cecil\Collection\Collection as CecilCollection;
14
15
/**
16
 * Class Collection.
17
 */
18
class Collection extends CecilCollection
19
{
20
    /**
21
     * Returns all "showable" pages.
22
     */
23 1
    public function showable(): self
24
    {
25
        $filteredPages = $this->filter(function (Page $page) {
26 1
            if ($page->getVariable('published') === true
27 1
                && $page->isVirtual() === false
28 1
                && $page->getVariable('redirect') === null
29 1
                && $page->getVariable('exclude') !== true) {
30 1
                return true;
31
            }
32 1
        });
33
34 1
        return $filteredPages;
35
    }
36
37
    /**
38
     * Alias of showable().
39
     */
40 1
    public function all(): self
41
    {
42 1
        return $this->showable();
43
    }
44
45
    /**
46
     * Sorts pages by date: the most recent first.
47
     */
48 1
    public function sortByDate(): self
49
    {
50
        return $this->usort(function ($a, $b) {
51 1
            if ($a['date'] == $b['date']) {
52 1
                return 0;
53
            }
54
55 1
            return ($a['date'] > $b['date']) ? -1 : 1;
56 1
        });
57
    }
58
59
    /**
60
     * Sorts pages by title (natural sort).
61
     */
62 1
    public function sortByTitle(): self
63
    {
64
        return $this->usort(function ($a, $b) {
65 1
            return strnatcmp($a['title'], $b['title']);
66 1
        });
67
    }
68
69
    /**
70
     * Sorts by weight (the heaviest first).
71
     */
72
    public function sortByWeight(): self
73
    {
74
        return $this->usort(function ($a, $b) {
75
            if ($a['weight'] == $b['weight']) {
76
                return 0;
77
            }
78
79
            return ($a['weight'] < $b['weight']) ? -1 : 1;
80
        });
81
    }
82
}
83