|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the SgDatatablesBundle package. |
| 5 | + * |
| 6 | + * (c) stwe <https://github.com/stwe/DatatablesBundle> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Sg\DatatablesBundle\Datatable\Extension; |
| 13 | + |
| 14 | +use Sg\DatatablesBundle\Datatable\OptionsTrait; |
| 15 | + |
| 16 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 17 | +use Exception; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class RowGroup |
| 21 | + * |
| 22 | + * @package Sg\DatatablesBundle\Datatable\Extension |
| 23 | + */ |
| 24 | +class RowGroup |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Use the OptionsResolver. |
| 28 | + */ |
| 29 | + use OptionsTrait; |
| 30 | + |
| 31 | + //------------------------------------------------- |
| 32 | + // DataTables - RowGroup Extension |
| 33 | + //------------------------------------------------- |
| 34 | + |
| 35 | + /** |
| 36 | + * The data point to get the grouping information. |
| 37 | + * Required option. |
| 38 | + * |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + protected $dataSrc; |
| 42 | + |
| 43 | + /** |
| 44 | + * Provide a function that can be used to control the data shown in the start grouping row. |
| 45 | + * Optional. |
| 46 | + * |
| 47 | + * @var array |
| 48 | + */ |
| 49 | + protected $startRender; |
| 50 | + |
| 51 | + /** |
| 52 | + * Provide a function that can be used to control the data shown in the end grouping row. |
| 53 | + * Optional. |
| 54 | + * |
| 55 | + * @var array |
| 56 | + */ |
| 57 | + protected $endRender; |
| 58 | + |
| 59 | + /** |
| 60 | + * Set the class name to be used for the grouping rows. |
| 61 | + * Optional. |
| 62 | + * |
| 63 | + * @var string |
| 64 | + */ |
| 65 | + protected $className; |
| 66 | + |
| 67 | + /** |
| 68 | + * Text to show for rows which have null or undefined group data. |
| 69 | + * Optional. |
| 70 | + * |
| 71 | + * @var string |
| 72 | + */ |
| 73 | + protected $emptyDataGroup; |
| 74 | + |
| 75 | + /** |
| 76 | + * Provides the ability to disable row grouping at initialisation |
| 77 | + * Optional. |
| 78 | + * |
| 79 | + * @var bool |
| 80 | + */ |
| 81 | + protected $enable; |
| 82 | + |
| 83 | + /** |
| 84 | + * Set the class name to be used for the grouping end rows |
| 85 | + * Optional. |
| 86 | + * |
| 87 | + * @var string |
| 88 | + */ |
| 89 | + protected $endClassName; |
| 90 | + |
| 91 | + /** |
| 92 | + * Set the class name to be used for the grouping start rows |
| 93 | + * Optional. |
| 94 | + * |
| 95 | + * @var string |
| 96 | + */ |
| 97 | + protected $startClassName; |
| 98 | + |
| 99 | + //------------------------------------------------- |
| 100 | + // Ctor. |
| 101 | + //------------------------------------------------- |
| 102 | + |
| 103 | + /** |
| 104 | + * RowGroup constructor. |
| 105 | + */ |
| 106 | + public function __construct() |
| 107 | + { |
| 108 | + $this->initOptions(); |
| 109 | + } |
| 110 | + |
| 111 | + //------------------------------------------------- |
| 112 | + // Options |
| 113 | + //------------------------------------------------- |
| 114 | + |
| 115 | + /** |
| 116 | + * Config options. |
| 117 | + * |
| 118 | + * @param OptionsResolver $resolver |
| 119 | + * |
| 120 | + * @return $this |
| 121 | + */ |
| 122 | + public function configureOptions(OptionsResolver $resolver) |
| 123 | + { |
| 124 | + $resolver->setRequired('data_src'); |
| 125 | + $resolver->setDefined('start_render'); |
| 126 | + $resolver->setDefined('end_render'); |
| 127 | + $resolver->setDefined('enable'); |
| 128 | + $resolver->setDefined('class_name'); |
| 129 | + $resolver->setDefined('empty_data_group'); |
| 130 | + $resolver->setDefined('end_class_name'); |
| 131 | + $resolver->setDefined('start_class_name'); |
| 132 | + |
| 133 | + $resolver->setDefaults(array( |
| 134 | + 'enable' => true, |
| 135 | + )); |
| 136 | + |
| 137 | + $resolver->setAllowedTypes('data_src', array('string')); |
| 138 | + $resolver->setAllowedTypes('start_render', array('array')); |
| 139 | + $resolver->setAllowedTypes('end_render', array('array')); |
| 140 | + $resolver->setAllowedTypes('enable', array('bool')); |
| 141 | + $resolver->setAllowedTypes('class_name', array('string')); |
| 142 | + $resolver->setAllowedTypes('empty_data_group', array('string')); |
| 143 | + $resolver->setAllowedTypes('end_class_name', array('string')); |
| 144 | + $resolver->setAllowedTypes('start_class_name', array('string')); |
| 145 | + |
| 146 | + return $this; |
| 147 | + } |
| 148 | + |
| 149 | + //------------------------------------------------- |
| 150 | + // Getters && Setters |
| 151 | + //------------------------------------------------- |
| 152 | + |
| 153 | + /** |
| 154 | + * Get details. |
| 155 | + * |
| 156 | + * @return string |
| 157 | + */ |
| 158 | + public function getDataSrc() |
| 159 | + { |
| 160 | + return $this->dataSrc; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Set details. |
| 165 | + * |
| 166 | + * @param string $dataSrc |
| 167 | + * |
| 168 | + * @return $this |
| 169 | + * @throws Exception |
| 170 | + */ |
| 171 | + public function setDataSrc($dataSrc) |
| 172 | + { |
| 173 | + if (is_string($dataSrc) && empty($dataSrc)) { |
| 174 | + throw new \Exception( |
| 175 | + "RowGroup::setDataSrc(): the column name is empty." |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + $this->dataSrc = $dataSrc; |
| 180 | + |
| 181 | + return $this; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @return string |
| 186 | + */ |
| 187 | + public function getStartRender() |
| 188 | + { |
| 189 | + return $this->startRender; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @param string $startRender |
| 194 | + * @return RowGroup |
| 195 | + */ |
| 196 | + public function setStartRender($startRender) |
| 197 | + { |
| 198 | + if (false === array_key_exists('template', $startRender)) { |
| 199 | + throw new Exception( |
| 200 | + 'RowGroup::setStartRender(): The "template" option is required.' |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + foreach ($startRender as $key => $value) { |
| 205 | + if (false === in_array($key, array('template', 'vars',))) { |
| 206 | + throw new Exception( |
| 207 | + "RowGroup::setStartRender(): $key is not a valid option." |
| 208 | + ); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + $this->startRender = $startRender; |
| 213 | + |
| 214 | + return $this; |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * @return string |
| 219 | + */ |
| 220 | + public function getEndRender() |
| 221 | + { |
| 222 | + return $this->endRender; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * @param string $endRender |
| 227 | + * @return RowGroup |
| 228 | + */ |
| 229 | + public function setEndRender($endRender) |
| 230 | + { |
| 231 | + if (false === array_key_exists('template', $startRender)) { |
| 232 | + throw new Exception( |
| 233 | + 'RowGroup::setEndRender(): The "template" option is required.' |
| 234 | + ); |
| 235 | + } |
| 236 | + |
| 237 | + foreach ($startRender as $key => $value) { |
| 238 | + if (false === in_array($key, array('template', 'vars',))) { |
| 239 | + throw new Exception( |
| 240 | + "RowGroup::setEndRender(): $key is not a valid option." |
| 241 | + ); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + $this->endRender = $endRender; |
| 246 | + |
| 247 | + return $this; |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * @return string |
| 252 | + */ |
| 253 | + public function getClassName() |
| 254 | + { |
| 255 | + return $this->className; |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * @param string $className |
| 260 | + * @return RowGroup |
| 261 | + */ |
| 262 | + public function setClassName($className) |
| 263 | + { |
| 264 | + if (is_string($className) && empty($className)) { |
| 265 | + throw new \Exception( |
| 266 | + "RowGroup::setClassName(): the class name is empty." |
| 267 | + ); |
| 268 | + } |
| 269 | + |
| 270 | + $this->className = $className; |
| 271 | + |
| 272 | + return $this; |
| 273 | + } |
| 274 | + |
| 275 | + /** |
| 276 | + * @return string |
| 277 | + */ |
| 278 | + public function getEmptyDataGroup() |
| 279 | + { |
| 280 | + return $this->emptyDataGroup; |
| 281 | + } |
| 282 | + |
| 283 | + /** |
| 284 | + * @param string $emptyDataGroup |
| 285 | + * @return RowGroup |
| 286 | + */ |
| 287 | + public function setEmptyDataGroup($emptyDataGroup) |
| 288 | + { |
| 289 | + if (is_string($emptyDataGroup) && empty($emptyDataGroup)) { |
| 290 | + throw new \Exception( |
| 291 | + "RowGroup::setEmptyDataGroup(): the empty data group text is empty." |
| 292 | + ); |
| 293 | + } |
| 294 | + |
| 295 | + $this->emptyDataGroup = $emptyDataGroup; |
| 296 | + |
| 297 | + return $this; |
| 298 | + } |
| 299 | + |
| 300 | + /** |
| 301 | + * @return bool |
| 302 | + */ |
| 303 | + public function getEnable() |
| 304 | + { |
| 305 | + return $this->enable; |
| 306 | + } |
| 307 | + |
| 308 | + /** |
| 309 | + * @param bool $enable |
| 310 | + * @return RowGroup |
| 311 | + */ |
| 312 | + public function setEnable($enable) |
| 313 | + { |
| 314 | + $this->enable = $enable; |
| 315 | + |
| 316 | + return $this; |
| 317 | + } |
| 318 | + |
| 319 | + /** |
| 320 | + * @return string |
| 321 | + */ |
| 322 | + public function getEndClassName() |
| 323 | + { |
| 324 | + return $this->endClassName; |
| 325 | + } |
| 326 | + |
| 327 | + /** |
| 328 | + * @param string $endClassName |
| 329 | + * @return RowGroup |
| 330 | + */ |
| 331 | + public function setEndClassName($endClassName) |
| 332 | + { |
| 333 | + if (is_string($endClassName) && empty($endClassName)) { |
| 334 | + throw new \Exception( |
| 335 | + "RowGroup::setEndClassName(): the end class name is empty." |
| 336 | + ); |
| 337 | + } |
| 338 | + |
| 339 | + $this->endClassName = $endClassName; |
| 340 | + |
| 341 | + return $this; |
| 342 | + } |
| 343 | + |
| 344 | + /** |
| 345 | + * @return string |
| 346 | + */ |
| 347 | + public function getStartClassName() |
| 348 | + { |
| 349 | + return $this->startClassName; |
| 350 | + } |
| 351 | + |
| 352 | + /** |
| 353 | + * @param string $startClassName |
| 354 | + * @return RowGroup |
| 355 | + */ |
| 356 | + public function setStartClassName($startClassName) |
| 357 | + { |
| 358 | + if (is_string($startClassName) && empty($startClassName)) { |
| 359 | + throw new \Exception( |
| 360 | + "RowGroup::setStartClassName(): the start class name is empty." |
| 361 | + ); |
| 362 | + } |
| 363 | + |
| 364 | + $this->startClassName = $startClassName; |
| 365 | + |
| 366 | + return $this; |
| 367 | + } |
| 368 | +} |
0 commit comments