Completed
Push — master ( 7a0973...f168c4 )
by Daniel
06:09 queued 04:02
created

Header::getLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\View;
6
7
use Psi\Component\Grid\GridContext;
8
9
class Header
10
{
11
    private $name;
12
    private $label;
13
    private $gridContext;
14
    private $sortField;
15
16
    public function __construct(GridContext $gridContext, string $name, string $label, string $sortField = null)
17
    {
18
        $this->name = $name;
19
        $this->label = $label;
20
        $this->gridContext = $gridContext;
21
        $this->sortField = $sortField;
22
    }
23
24
    public function getName()
25
    {
26
        return $this->name;
27
    }
28
29
    public function getLabel()
30
    {
31
        return $this->label;
32
    }
33
34
    public function isSorted(): bool
35
    {
36
        $ordering = $this->gridContext->getOrderings();
37
38
        return isset($ordering[$this->name]);
39
    }
40
41
    public function isSortAscending(): bool
42
    {
43
        if (false === $this->isSorted()) {
44
            throw new \RuntimeException(sprintf(
45
                'Cannot determine if sort is ascending when the field ("%s") is not sorted.',
46
                $this->name
47
            ));
48
        }
49
50
        return $this->gridContext->getOrderings()[$this->name] === 'asc';
51
    }
52
53
    public function getSortField(): string
54
    {
55
        return $this->sortField;
56
    }
57
58
    public function canBeSorted(): bool
59
    {
60
        return null !== $this->sortField;
61
    }
62
63
    public function getUrlParametersForSort($order = 'asc')
64
    {
65
        $gridContext = $this->gridContext->getUrlParameters();
66
        $gridContext['orderings'] = [
67
            $this->name => $order,
68
        ];
69
70
        return $gridContext;
71
    }
72
}
73