Passed
Push — coverage ( 46da49...c64418 )
by Arnaud
03:07
created

Collection::sortByWeight()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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 non virtual pages.
22
     *
23
     * @return self
24
     */
25
    public function all(): self
26
    {
27
        $filteredPages = $this->filter(function (Page $page) {
28
            if ($page->isVirtual() === false) {
29
                return true;
30
            }
31
        });
32
33
        return $filteredPages;
34
    }
35
36
    /**
37
     * Sorts pages by date: the most recent first.
38
     *
39
     * @return self
40
     */
41
    public function sortByDate(): self
42
    {
43
        return $this->usort(function ($a, $b) {
44
            if ($a['date'] == $b['date']) {
45
                return 0;
46
            }
47
48
            return ($a['date'] > $b['date']) ? -1 : 1;
49
        });
50
    }
51
52
    /**
53
     * Sorts pages by title (natural sort).
54
     *
55
     * @return self
56
     */
57
    public function sortByTitle(): self
58
    {
59
        return $this->usort(function ($a, $b) {
60
            return strnatcmp($a['title'], $b['title']);
61
        });
62
    }
63
64
    /**
65
     * Sorts by weight (the heaviest first).
66
     *
67
     * @return self
68
     */
69
    public function sortByWeight(): self
70
    {
71
        return $this->usort(function ($a, $b) {
72
            if ($a['weight'] == $b['weight']) {
73
                return 0;
74
            }
75
76
            return ($a['weight'] < $b['weight']) ? -1 : 1;
77
        });
78
    }
79
}
80