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