Completed
Branch feature/rework (8c4d59)
by Pavel
07:53
created

DataGrid::setPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Pfilsx\DataGrid\Grid;
5
6
7
use Doctrine\Common\Collections\Criteria;
8
use Symfony\Component\Routing\RouterInterface;
9
use Twig\Environment;
10
use Twig\Template;
11
12
/**
13
 * Class DataGrid
14
 * @package Pfilsx\DataGrid\Grid
15
 * @internal
16
 */
17
class DataGrid
18
{
19
    /**
20
     * @var bool
21
     */
22
    protected $showTitles = true;
23
24
    /**
25
     * @var Template
26
     */
27
    protected $template;
28
    /**
29
     * @var RouterInterface
30
     */
31
    protected $router;
32
    /**
33
     * @var Environment
34
     */
35
    protected $twig;
36
37
    protected $noDataMessage = 'No data found';
38
39
40
    /**
41
     * @var Criteria
42
     */
43
    protected $filtersCriteria;
44
    /**
45
     * @var DataGridBuilderInterface
46
     */
47
    protected $builder;
48
49
    /**
50
     * DataGrid constructor.
51
     * @param DataGridBuilderInterface $builder
52
     * @param array $defaultOptions
53
     * @internal
54
     */
55
    public function __construct(DataGridBuilderInterface $builder, array $defaultOptions = [])
56
    {
57
        $this->builder = $builder;
58
        $this->twig = $defaultOptions['twig'];
59
        $this->setConfigurationOptions(array_merge($defaultOptions, $builder->getOptions()));
60
    }
61
62
    /**
63
     * @internal
64
     * @param $options
65
     */
66
    protected function setConfigurationOptions($options)
67
    {
68
        foreach ($options as $key => $value) {
69
            $setter = 'set' . ucfirst($key);
70
            if (method_exists($this, $setter)) {
71
                $this->$setter($value);
72
            }
73
        }
74
    }
75
76
    public function getShowTitles()
77
    {
78
        return $this->showTitles;
79
    }
80
81
    protected function setShowTitles($value)
82
    {
83
        $this->showTitles = (bool)$value;
84
    }
85
86
    public function getProvider()
87
    {
88
        return $this->builder->getProvider();
89
    }
90
91
    public function getColumns()
92
    {
93
        return $this->builder->getColumns();
94
    }
95
96
    public function hasFilters()
97
    {
98
        return $this->builder->hasFilters();
99
    }
100
101
    public function getData()
102
    {
103
        return $this->getProvider()->getItems();
104
    }
105
106
    /**
107
     * @return Template
108
     */
109
    public function getTemplate()
110
    {
111
        return $this->template;
112
    }
113
114
    /**
115
     * @internal
116
     * @param string $path
117
     * @throws \Twig\Error\LoaderError
118
     * @throws \Twig\Error\RuntimeError
119
     * @throws \Twig\Error\SyntaxError
120
     */
121
    public function setTemplate(string $path)
122
    {
123
        $template = $this->twig->loadTemplate($path);
124
        $this->template = $template;
125
    }
126
127
    public function getRouter()
128
    {
129
        return $this->router;
130
    }
131
132
    protected function setRouter(RouterInterface $router)
133
    {
134
        $this->router = $router;
135
    }
136
137
    public function getNoDataMessage()
138
    {
139
        return $this->noDataMessage;
140
    }
141
142
    protected function setNoDataMessage(string $message)
143
    {
144
        $this->noDataMessage = $message;
145
    }
146
147
    public function hasPagination()
148
    {
149
        return $this->builder->hasPagination();
150
    }
151
152
    public function getPaginationOptions()
153
    {
154
        return $this->builder->getPager()->getPaginationOptions();
155
    }
156
}
157