Completed
Pull Request — master (#74)
by Daniel
02:22 queued 11s
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
    private $template;
16
17
    public function __construct(
18
        GridContext $gridContext,
19
        string $name,
20
        string $label,
21
        string $template,
22
        string $sortField = null
23
    ) {
24
        $this->name = $name;
25
        $this->label = $label;
26
        $this->gridContext = $gridContext;
27
        $this->sortField = $sortField;
28
        $this->template = $template;
29
    }
30
31
    public function getName()
32
    {
33
        return $this->name;
34
    }
35
36
    public function getLabel()
37
    {
38
        return $this->label;
39
    }
40
41
    public function getTemplate()
42
    {
43
        return $this->template;
44
    }
45
46
    public function isSorted(): bool
47
    {
48
        $ordering = $this->gridContext->getOrderings();
49
50
        return isset($ordering[$this->name]);
51
    }
52
53
    public function isSortAscending(): bool
54
    {
55
        if (false === $this->isSorted()) {
56
            throw new \RuntimeException(sprintf(
57
                'Cannot determine if sort is ascending when the field ("%s") is not sorted.',
58
                $this->name
59
            ));
60
        }
61
62
        return $this->gridContext->getOrderings()[$this->name] === 'asc';
63
    }
64
65
    public function getSortField(): string
66
    {
67
        return $this->sortField;
68
    }
69
70
    public function canBeSorted(): bool
71
    {
72
        return null !== $this->sortField;
73
    }
74
75
    public function getUrlParametersForSort($order = 'asc')
76
    {
77
        $gridContext = $this->gridContext->getUrlParameters();
78
        $gridContext['orderings'] = [
79
            $this->name => $order,
80
        ];
81
82
        return $gridContext;
83
    }
84
}
85