1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of cwdFancyGridBundle |
4
|
|
|
* |
5
|
|
|
* (c)2017 cwd.at GmbH <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
namespace Cwd\FancyGridBundle\Grid; |
12
|
|
|
|
13
|
|
|
use Cwd\FancyGridBundle\Column\ColumnInterface; |
14
|
|
|
use Cwd\FancyGridBundle\Grid\Exception; |
15
|
|
|
use Cwd\FancyGridBundle\Grid\GridBuilderInterface; |
16
|
|
|
use Cwd\FancyGridBundle\Grid\GridInterface; |
17
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
18
|
|
|
use Doctrine\ORM\EntityManager; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class GridBuilder |
23
|
|
|
* @package Cwd\FancyGridBundle\Grid\Exception |
24
|
|
|
* @author Ludwig Ruderstaler <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class GridBuilder implements GridBuilderInterface, \IteratorAggregate |
27
|
|
|
{ |
28
|
|
|
protected $dispatcher; |
29
|
|
|
public $options; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The children of the grid builder. |
33
|
|
|
* |
34
|
|
|
* @var ColumnInterface[] |
35
|
|
|
*/ |
36
|
|
|
public $children = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ObjectManager |
40
|
|
|
*/ |
41
|
|
|
public $doctrine; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $data; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates an empty form configuration. |
50
|
|
|
* |
51
|
|
|
* @param ObjectManager $doctrine The EntityManager |
52
|
|
|
* @param EventDispatcherInterface $dispatcher The event dispatcher |
53
|
|
|
* @param array $options The form options |
54
|
|
|
* |
55
|
|
|
* @throws InvalidArgumentException If the data class is not a valid class or if |
56
|
|
|
* the name contains invalid characters. |
57
|
|
|
*/ |
58
|
|
|
public function __construct(ObjectManager $doctrine, EventDispatcherInterface $dispatcher, array $options = array()) |
59
|
|
|
{ |
60
|
|
|
$this->dispatcher = $dispatcher; |
61
|
|
|
$this->options = $options; |
62
|
|
|
$this->doctrine = $doctrine; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ColumnInterface $child |
67
|
|
|
* @param null $type |
68
|
|
|
* @param array $options |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function add(ColumnInterface $child, $type = null, array $options = array()) |
72
|
|
|
{ |
73
|
|
|
$this->children[$child->getName()] = $child; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @return ColumnInterface |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function get($name) : ColumnInterface |
83
|
|
|
{ |
84
|
|
|
if (isset($this->children[$name])) { |
85
|
|
|
return $this->children[$name]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
throw new InvalidArgumentException(sprintf('The child with the name "%s" does not exist.', $name)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $name |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function remove($name) |
96
|
|
|
{ |
97
|
|
|
unset($this->children[$name]); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function has($name) |
107
|
|
|
{ |
108
|
|
|
return isset($this->children[$name]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return \Cwd\FancyGridBundle\Column\ColumnInterface[] |
113
|
|
|
*/ |
114
|
|
|
public function all() |
115
|
|
|
{ |
116
|
|
|
return $this->children; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return GridBuilder |
121
|
|
|
*/ |
122
|
|
|
protected function getGridConfig() |
123
|
|
|
{ |
124
|
|
|
$config = clone $this; |
125
|
|
|
|
126
|
|
|
return $config; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
|
|
public function count() |
133
|
|
|
{ |
134
|
|
|
return count($this->children); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
* |
140
|
|
|
* @return \ArrayIterator |
141
|
|
|
*/ |
142
|
|
|
public function getIterator() |
143
|
|
|
{ |
144
|
|
|
return new \ArrayIterator($this->all()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function getOptions() |
152
|
|
|
{ |
153
|
|
|
return $this->options; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param array $options |
158
|
|
|
*/ |
159
|
|
|
public function setOptions(array $options) |
160
|
|
|
{ |
161
|
|
|
$this->options = $options; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $name |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function hasOption($name) |
169
|
|
|
{ |
170
|
|
|
return array_key_exists($name, $this->options); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $name |
175
|
|
|
* @param string|null $default |
176
|
|
|
* @return misc |
177
|
|
|
*/ |
178
|
|
|
public function getOption($name, $default = null) |
179
|
|
|
{ |
180
|
|
|
return array_key_exists($name, $this->options) ? $this->options[$name] : $default; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|