Completed
Pull Request — master (#64)
by Daniel
02:07
created

GridContext::getPageOffset()   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;
6
7
final class GridContext
8
{
9
    private $options;
10
    private $classFqn;
11
12
    public function __construct(string $classFqn, array $options)
13
    {
14
        $this->classFqn = $classFqn;
15
        $defaults = [
16
            'page_size' => 50,
17
            'page' => 1,
18
            'orderings' => [],
19
            'filter' => [],
20
            'variant' => null,
21
        ];
22
23
        // check for invalid keys
24 View Code Duplication
        if ($diff = array_diff(array_keys($options), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
            throw new \InvalidArgumentException(sprintf(
26
                'Invalid grid context options "%s". Valid options: "%s"',
27
                implode('", "', $diff), implode('", "', array_keys($defaults))
28
            ));
29
        }
30
31
        // set defaults
32
        $options = array_merge($defaults, $options);
33
34
        // normalize the orderings
35
        $options['orderings'] = array_map(function ($order) {
36
            $order = strtolower($order);
37
38
            if (false === in_array($order, ['asc', 'desc'])) {
39
                throw new \InvalidArgumentException(sprintf(
40
                    'Order must be either "asc" or "desc" got "%s"',
41
                    $order
42
                ));
43
            }
44
45
            return $order;
46
        }, $options['orderings']);
47
48
        // cast integer values where applicable
49
        foreach (['page', 'page_size'] as $key) {
50
            $options[$key] = $options[$key] !== null ? (int) $options[$key] : null;
51
        }
52
53
        // ensure current page is > 0
54
        if ($options['page'] < 1) {
55
            $options['page'] = 1;
56
        }
57
58
        $this->options = $options;
59
    }
60
61
    public function getCurrentPage(): int
62
    {
63
        return $this->options['page'];
64
    }
65
66
    public function isPaginated()
67
    {
68
        return null !== $this->options['page_size'];
69
    }
70
71
    public function getPageSize(): int
72
    {
73
        return $this->options['page_size'];
74
    }
75
76
    public function getPageOffset(): int
77
    {
78
        return ($this->getCurrentPage() - 1) * $this->getPageSize();
79
    }
80
81
    public function getOrderings(): array
82
    {
83
        return $this->options['orderings'];
84
    }
85
86
    public function getVariant()
87
    {
88
        return $this->options['variant'];
89
    }
90
91
    public function getFilter()
92
    {
93
        return $this->options['filter'];
94
    }
95
96
    public function getUrlParameters(): array
97
    {
98
        return array_merge([
99
            'class' => $this->classFqn,
100
        ], $this->options);
101
    }
102
103
    public function getClassFqn()
104
    {
105
        return $this->classFqn;
106
    }
107
}
108