Passed
Push — quality ( aaf71d...3e536e )
by Arnaud
09:55
created

Collection::usort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
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 1
            if ($page->getVariable('published') === true
30 1
                && $page->isVirtual() === false
31 1
                && $page->getVariable('redirect') === null
32 1
                && $page->getVariable('exclude') !== true) {
33 1
                return true;
34
            }
35 1
        });
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 1
    public function sortByDate($options = null): self
52
    {
53
        // backward compatibility
54 1
        if (is_string($options)) {
55
            $options['variable'] = $options;
56
        }
57
        // options
58 1
        $options['variable'] = $options['variable'] ?? 'date';
59 1
        $options['descTitle'] = $options['descTitle'] ?? false;
60 1
        $options['reverse'] = $options['reverse'] ?? false;
61
62 1
        $pages = $this->usort(function ($a, $b) use ($options) {
63 1
            if ($a[$options['variable']] == $b[$options['variable']]) {
64
                // if dates are equal and "descTitle" is true
65 1
                if ($options['descTitle'] && (isset($a['title']) && isset($b['title']))) {
66
                    return strnatcmp($b['title'], $a['title']);
67
                }
68
69 1
                return 0;
70
            }
71
72 1
            return $a[$options['variable']] > $b[$options['variable']] ? -1 : 1;
73 1
        });
74 1
        if ($options['reverse']) {
75
            $pages = $pages->reverse();
76
        }
77
78 1
        return $pages;
79
    }
80
81
    /**
82
     * Sorts pages by title (natural sort).
83
     */
84 1
    public function sortByTitle($options = null): self
85
    {
86
        // options
87 1
        if (!isset($options['reverse'])) {
88 1
            $options['reverse'] = false;
89
        }
90
91 1
        return $this->usort(function ($a, $b) use ($options) {
92 1
            return ($options['reverse'] ? -1 : 1) * strnatcmp($a['title'], $b['title']);
93 1
        });
94
    }
95
96
    /**
97
     * Sorts by weight (the heaviest first).
98
     */
99
    public function sortByWeight($options = null): self
100
    {
101
        // options
102
        if (!isset($options['reverse'])) {
103
            $options['reverse'] = false;
104
        }
105
106
        return $this->usort(function ($a, $b) use ($options) {
107
            if ($a['weight'] == $b['weight']) {
108
                return 0;
109
            }
110
111
            return ($options['reverse'] ? -1 : 1) * ($a['weight'] < $b['weight'] ? -1 : 1);
112
        });
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function get(string $id): Page
119
    {
120
        return parent::get($id);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function first(): ?Page
127
    {
128
        return parent::first();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function filter(\Closure $callback): self
135
    {
136
        return parent::filter($callback);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function usort(\Closure $callback = null): self
143
    {
144
        return parent::usort($callback);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function reverse(): self
151
    {
152
        return parent::reverse();
153
    }
154
}
155