Completed
Push — master ( 7f4b5a...549eec )
by Michał
47:24 queued 31:36
created

Grid::setDriverConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Sylius package.
4
 *
5
 * (c) Paweł Jędrzejewski
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Sylius\Component\Grid\Definition;
12
13
/**
14
 * @author Paweł Jędrzejewski <[email protected]>
15
 */
16
class Grid
17
{
18
    /**
19
     * @var string
20
     */
21
    private $code;
22
23
    /**
24
     * @var string
25
     */
26
    private $driver;
27
28
    /**
29
     * @var array
30
     */
31
    private $driverConfiguration;
32
33
    /**
34
     * @var array
35
     */
36
    private $sorting = [];
37
38
    /**
39
     * @var array
40
     */
41
    private $fields = [];
42
43
    /**
44
     * @var array
45
     */
46
    private $filters = [];
47
48
    /**
49
     * @var array
50
     */
51
    private $actionGroups = [];
52
53
    /**
54
     * @param string $code
55
     * @param string $driver
56
     * @param array $driverConfiguration
57
     */
58
    private function __construct($code, $driver, array $driverConfiguration)
59
    {
60
        $this->code = $code;
61
        $this->driver = $driver;
62
        $this->driverConfiguration = $driverConfiguration;
63
    }
64
65
    /**
66
     * @param string $code
67
     * @param string $driver
68
     * @param array $driverConfiguration
69
     *
70
     * @return Grid
71
     */
72
    public static function fromCodeAndDriverConfiguration($code, $driver, array $driverConfiguration)
73
    {
74
        return new Grid($code, $driver, $driverConfiguration);
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getCode()
81
    {
82
        return $this->code;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getDriver()
89
    {
90
        return $this->driver;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getDriverConfiguration()
97
    {
98
        return $this->driverConfiguration;
99
    }
100
101
    /**
102
     * @param array $driverConfiguration
103
     */
104
    public function setDriverConfiguration(array $driverConfiguration)
105
    {
106
        $this->driverConfiguration = $driverConfiguration;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getSorting()
113
    {
114
        return $this->sorting;
115
    }
116
117
    /**
118
     * @param array $sorting
119
     */
120
    public function setSorting(array $sorting)
121
    {
122
        $this->sorting = $sorting;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getFields()
129
    {
130
        return $this->fields;
131
    }
132
133
    /**
134
     * @param Field $field
135
     */
136
    public function addField(Field $field)
137
    {
138
        $name = $field->getName();
139
140
        if ($this->hasField($name)) {
141
            throw new \InvalidArgumentException(sprintf('Field "%s" already exists.', $name));
142
        }
143
144
        $this->fields[$name] = $field;
145
    }
146
147
    /**
148
     * @param string $name
149
     */
150
    public function getField($name)
151
    {
152
        if (!$this->hasField($name)) {
153
            throw new \InvalidArgumentException(sprintf('Field "%s" does not exist.', $name));
154
        }
155
156
        return $this->fields[$name];
157
    }
158
159
    /**
160
     * @param string $name
161
     */
162
    public function hasField($name)
163
    {
164
        return array_key_exists($name, $this->fields);
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function getActionGroups()
171
    {
172
        return $this->actionGroups;
173
    }
174
175
    /**
176
     * @param ActionGroup $actionGroup
177
     */
178
    public function addActionGroup(ActionGroup $actionGroup)
179
    {
180
        $name = $actionGroup->getName();
181
182
        if ($this->hasActionGroup($name)) {
183
            throw new \InvalidArgumentException(sprintf('ActionGroup "%s" already exists.', $name));
184
        }
185
186
        $this->actionGroups[$name] = $actionGroup;
187
    }
188
189
    /**
190
     * @param string $name
191
     */
192
    public function getActionGroup($name)
193
    {
194
        if (!$this->hasActionGroup($name)) {
195
            throw new \InvalidArgumentException(sprintf('ActionGroup "%s" does not exist.', $name));
196
        }
197
198
        return $this->actionGroups[$name];
199
    }
200
201
    /**
202
     * @param string $groupName
203
     *
204
     * @return Action[]
205
     */
206
    public function getActions($groupName)
207
    {
208
        return $this->getActionGroup($groupName)->getActions();
209
    }
210
211
    /**
212
     * @param string $name
213
     */
214
    public function hasActionGroup($name)
215
    {
216
        return array_key_exists($name, $this->actionGroups);
217
    }
218
219
    /**
220
     * @return array
221
     */
222
    public function getFilters()
223
    {
224
        return $this->filters;
225
    }
226
227
    /**
228
     * @param Filter $filter
229
     */
230
    public function addFilter(Filter $filter)
231
    {
232
        if ($this->hasFilter($name = $filter->getName())) {
233
            throw new \InvalidArgumentException(sprintf('Filter "%s" already exists.', $name));
234
        }
235
236
        $this->filters[$name] = $filter;
237
    }
238
239
    /**
240
     * @param string $name
241
     *
242
     * @return Filter
243
     */
244
    public function getFilter($name)
245
    {
246
        if (!$this->hasFilter($name)) {
247
            throw new \InvalidArgumentException(sprintf('Filter "%s" does not exist.', $name));
248
        }
249
250
        return $this->filters[$name];
251
    }
252
253
    /**
254
     * @param string $name
255
     */
256
    public function hasFilter($name)
257
    {
258
        return array_key_exists($name, $this->filters);
259
    }
260
}
261