Completed
Push — master ( 2f0a37...277b55 )
by Daniel
9s
created

GridOptions::__construct()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 33
Code Lines 21

Duplication

Lines 6
Ratio 18.18 %

Importance

Changes 0
Metric Value
dl 6
loc 33
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 21
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
final class GridOptions
8
{
9
    private $options;
10
11
    public function __construct(array $options)
12
    {
13
        $defaults = [
14
            'page_size' => 50,
15
            'current_page' => 0,
16
            'orderings' => [],
17
            'filter' => [],
18
            'variant' => null,
19
        ];
20
21 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...
22
            throw new \InvalidArgumentException(sprintf(
23
                'Invalid grid options "%s". Valid options: "%s"',
24
                implode('", "', $diff), implode('", "', array_keys($defaults))
25
            ));
26
        }
27
28
        $options = array_merge($defaults, $options);
29
        $options['orderings'] = array_map(function ($order) {
30
            $order = strtolower($order);
31
32
            if (false === in_array($order, ['asc', 'desc'])) {
33
                throw new \InvalidArgumentException(sprintf(
34
                    'Order must be either "asc" or "desc" got "%s"',
35
                    $order
36
                ));
37
            }
38
39
            return $order;
40
        }, $options['orderings']);
41
42
        $this->options = $options;
43
    }
44
45
    public function getCurrentPage(): int
46
    {
47
        return $this->options['current_page'];
48
    }
49
50
    public function getPageSize(): int
51
    {
52
        return $this->options['page_size'];
53
    }
54
55
    public function getPageOffset(): int
56
    {
57
        return $this->getCurrentPage() * $this->getPageSize();
58
    }
59
60
    public function getOrderings(): array
61
    {
62
        return $this->options['orderings'];
63
    }
64
65
    public function getVariant()
66
    {
67
        return $this->options['variant'];
68
    }
69
70
    public function getFilter()
71
    {
72
        return $this->options['filter'];
73
    }
74
75
    public function getFilterActionOptions(): array
76
    {
77
        return array_filter($this->options, function ($key) {
78
            return $key !== 'filter';
79
        }, ARRAY_FILTER_USE_KEY);
80
    }
81
82
    public function getOptions(): array
83
    {
84
        return $this->options;
85
    }
86
}
87