Completed
Pull Request — master (#15)
by Daniel
02:00
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
        return isset($ordering[$this->name]);
27
    }
28
29
    public function isSortAscending(): bool
30
    {
31
        if (false === $this->isSorted()) {
32
            throw new \RuntimeException(sprintf(
33
                'Cannot determine if sort is ascending when the field ("%s") is not sorted.',
34
                $this->name
35
            ));
36
        }
37
38
        return $this->options->getOrderings()[$this->name] === 'asc';
39
    }
40
41
    public function getUrlParametersForSort($order = 'asc')
42
    {
43
        $options = $this->options->getUrlParameters();
44
        $options['orderings'] = [
45
            $this->name => $order
46
        ];
47
48
        return $options;
49
    }
50
}
51