Completed
Push — master ( af7b98...388b72 )
by Vitaliy
02:45
created

src/Components/RecordsPerPage.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Nayjest\Grids\Components;
3
4
use Nayjest\Grids\Components\Base\RenderableComponent;
5
6
/**
7
 * Class RecordsPerPage
8
 *
9
 * The component renders control
10
 * for switching count of records displayed per page.
11
 *
12
 * @package Nayjest\Grids\Components
13
 */
14
class RecordsPerPage extends RenderableComponent
15
{
16
17
    protected $name = 'records_per_page';
18
19
    protected $variants = [
20
        50,
21
        100,
22
        300,
23
        1000
24
    ];
25
26
    protected $template = '*.components.records_per_page';
27
28
    /**
29
     * Returns variants.
30
     *
31
     * @return array|int[]
32
     */
33
    public function getVariants()
34
    {
35
        return array_combine(array_values($this->variants),array_values($this->variants));
36
    }
37
38
    /**
39
     * Sets variants.
40
     *
41
     * @param array|int[] $variants
42
     * @return $this
43
     */
44
    public function setVariants(array $variants)
45
    {
46
        $this->variants = $variants;
47
        return $this;
48
    }
49
50
    /**
51
     * Returns name of related input.
52
     *
53
     * @return string
54
     */
55
    public function getInputName()
56
    {
57
        $key = $this->grid->getInputProcessor()->getKey();
58
        return "{$key}[filters][records_per_page]";
59
    }
60
61
    /**
62
     * Returns current value from input.
63
     * Default grids pre-configured page size will be returned if there is no input.
64
     *
65
     * @return int|null
66
     */
67
    public function getValue()
68
    {
69
        $from_input = $this
70
            ->grid
71
            ->getInputProcessor()
72
            ->getFilterValue('records_per_page');
73
        if ($from_input === null) {
74
            return $this->grid->getConfig()->getPageSize();
75
        } else {
76
            return $from_input;
77
        }
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function prepare()
84
    {
85
        $value = $this->getValue();
86
        if (!$value || !is_numeric($value)) return;
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
87
        $this->grid->getConfig()->getDataProvider()->setPageSize($value);
88
    }
89
}
90