Completed
Pull Request — master (#5)
by Daniel
06:54
created

GridOptions::getOrderings()   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
namespace Psi\Component\Grid;
4
5
final class GridOptions
6
{
7
    private $variant;
8
    private $page;
0 ignored issues
show
Unused Code introduced by
The property $page is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
    private $pageSize;
10
    private $orderings;
11
12
    public function __construct(array $options)
13
    {
14
        $defaults = [
15
            'page_size' => 50,
16
            'current_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->currentPage = $options['current_page'];
0 ignored issues
show
Bug introduced by
The property currentPage does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
        $this->pageSize = $options['page_size'];
32
        $this->orderings = $options['orderings'];
33
        $this->variant = $options['variant'];
34
    }
35
36
    public function getCurrentPage()
37
    {
38
        return $this->currentPage;
39
    }
40
41
    public function getPageSize()
42
    {
43
        return $this->pageSize;
44
    }
45
46
    public function getPageOffset()
47
    {
48
        return $this->currentPage * $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