Completed
Pull Request — master (#15)
by Daniel
01:58
created

Header::getUrlParametersForSort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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