Completed
Push — feature-output-formats ( a13454...2db023 )
by Arnaud
02:07
created

Collection::sortByTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Collection\Page;
10
11
use Cecil\Collection\Collection as CecilCollection;
12
13
/**
14
 * Class Collection.
15
 */
16
class Collection extends CecilCollection
17
{
18
    /**
19
     * Sort items by date.
20
     *
21
     * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
22
     */
23
    public function sortByDate(): self
24
    {
25
        return $this->usort(function ($a, $b) {
26
            if (!isset($a['date'])) {
27
                return -1;
28
            }
29
            if (!isset($b['date'])) {
30
                return 1;
31
            }
32
            if ($a['date'] == $b['date']) {
33
                return 0;
34
            }
35
36
            return ($a['date'] > $b['date']) ? -1 : 1;
37
        });
38
    }
39
40
    /**
41
     * Sort items by title.
42
     *
43
     * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45
    public function sortByTitle(): self
46
    {
47
        return $this->usort(function ($a, $b) {
48
            return strnatcmp($a['title'], $b['title']);
49
        });
50
    }
51
}
52