Completed
Pull Request — feature-output-formats (#275)
by Arnaud
69:12 queued 66:47
created

Collection::all()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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