Completed
Pull Request — master (#15)
by Daniel
02:00
created

Header::getSortParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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 \InvalidArgumentException(
33
                'Cannot determine if sort is asending when the field is not sorted.'
34
            );
35
        }
36
37
        return $this->options->getOrderings()[$this->name] === 'asc';
38
    }
39
40
    public function getSortParameters($order = 'asc')
41
    {
42
        $options = $this->options->getOptions();
43
        $options['orderings'] = [
44
            $this->name => $order
45
        ];
46
47
        return $options;
48
    }
49
}
50