Completed
Push — master ( 1b9caf...d8ff9a )
by Jesse
01:55
created

Sort   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 15
c 3
b 0
f 0
dl 0
loc 58
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A andThenDescendingBy() 0 3 1
A __construct() 0 8 1
A ascendingBy() 0 5 2
A isRequired() 0 3 1
A andThenAscendingBy() 0 3 1
A field() 0 3 1
A descendingBy() 0 5 2
A next() 0 3 1
A ascends() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Sorting;
5
6
use Stratadox\Sorting\Contracts\ExtensibleSorting;
7
use Stratadox\Sorting\Contracts\Sorting;
8
9
/**
10
 * Sort according to this definition.
11
 *
12
 * Contains the field, the sorting direction and the sorting definition for
13
 * unresolved elements.
14
 *
15
 * @author  Stratadox
16
 * @package Stratadox\Sorting
17
 */
18
final class Sort implements ExtensibleSorting
19
{
20
    private $field;
21
    private $ascends;
22
    private $next;
23
24
    private function __construct(
25
        string $field,
26
        bool $ascends,
27
        Sorting $next
28
    ) {
29
        $this->field = $field;
30
        $this->ascends = $ascends;
31
        $this->next = $next;
32
    }
33
34
    public static function descendingBy(
35
        string $field,
36
        Sorting $next = null
37
    ): ExtensibleSorting {
38
        return new Sort($field, false, $next ?: DoNotSort::atAll());
39
    }
40
41
    public static function ascendingBy(
42
        string $field,
43
        Sorting $next = null
44
    ): ExtensibleSorting {
45
        return new Sort($field, true, $next ?: DoNotSort::atAll());
46
    }
47
48
    public function field(): string
49
    {
50
        return $this->field;
51
    }
52
53
    public function next(): Sorting
54
    {
55
        return $this->next;
56
    }
57
58
    public function ascends(): bool
59
    {
60
        return $this->ascends;
61
    }
62
63
    public function isRequired(): bool
64
    {
65
        return true;
66
    }
67
68
    public function andThenAscendingBy(string $field): ExtensibleSorting
69
    {
70
        return new Sort($this->field, $this->ascends, Sort::ascendingBy($field));
71
    }
72
73
    public function andThenDescendingBy(string $field): ExtensibleSorting
74
    {
75
        return new Sort($this->field, $this->ascends, Sort::descendingBy($field));
76
    }
77
}
78