Completed
Push — master ( 406296...0a0b96 )
by Daniel
07:00 queued 04:29
created

Header::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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 $options;
13
14
    public function __construct(string $name, GridContext $options)
15
    {
16
        $this->name = $name;
17
        $this->options = $options;
18
    }
19
20
    public function getName()
21
    {
22
        return $this->name;
23
    }
24
25
    public function isSorted(): bool
26
    {
27
        $ordering = $this->options->getOrderings();
28
29
        return isset($ordering[$this->name]);
30
    }
31
32
    public function isSortAscending(): bool
33
    {
34
        if (false === $this->isSorted()) {
35
            throw new \RuntimeException(sprintf(
36
                'Cannot determine if sort is ascending when the field ("%s") is not sorted.',
37
                $this->name
38
            ));
39
        }
40
41
        return $this->options->getOrderings()[$this->name] === 'asc';
42
    }
43
44
    public function getUrlParametersForSort($order = 'asc')
45
    {
46
        $options = $this->options->getUrlParameters();
47
        $options['orderings'] = [
48
            $this->name => $order,
49
        ];
50
51
        return $options;
52
    }
53
}
54