Completed
Pull Request — master (#4)
by Daniel
04:01 queued 02:02
created

GridOptions::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 6
Ratio 26.09 %

Importance

Changes 0
Metric Value
dl 6
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 1
1
<?php
2
3
namespace Psi\Component\Grid;
4
5
final class GridOptions
6
{
7
    private $variant;
8
    private $page;
9
    private $pageSize;
10
    private $orderings;
11
12
    public function __construct(array $options)
13
    {
14
        $defaults = [
15
            'page_size' => 50,
16
            'page' => 0,
17
            'orderings' => [],
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
30
        $this->page = $options['page'];
31
        $this->pageSize = $options['page_size'];
32
        $this->orderings = $options['orderings'];
33
        $this->variant = $options['variant'];
34
    }
35
36
    public function getPage()
37
    {
38
        return $this->page;
39
    }
40
41
    public function getPageSize()
42
    {
43
        return $this->pageSize;
44
    }
45
46
    public function getPageOffset()
47
    {
48
        return $this->page * $this->pageSize;
49
    }
50
51
    public function getOrderings()
52
    {
53
        return $this->orderings;
54
    }
55
56
    public function getVariant()
57
    {
58
        return $this->variant;
59
    }
60
}
61