| Total Complexity | 86 |
| Total Lines | 424 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like HtmlTableContent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlTableContent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class HtmlTableContent extends HtmlSemCollection { |
||
| 17 | protected $_tdTagNames=[ "thead" => "th","tbody" => "td","tfoot" => "th" ]; |
||
| 18 | protected $_merged=false; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * |
||
| 22 | * @param string $identifier |
||
| 23 | * @param string $tagName |
||
| 24 | * @param int $rowCount |
||
| 25 | * @param int $colCount |
||
| 26 | */ |
||
| 27 | public function __construct($identifier, $tagName="tbody", $rowCount=NULL, $colCount=NULL) { |
||
| 28 | parent::__construct($identifier, $tagName, ""); |
||
| 29 | if (isset($rowCount) && isset($colCount)) |
||
| 30 | $this->setRowCount($rowCount, $colCount); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * |
||
| 35 | * @param int $rowCount |
||
| 36 | * @param int $colCount |
||
| 37 | * @return HtmlTableContent |
||
| 38 | */ |
||
| 39 | public function setRowCount($rowCount, $colCount) { |
||
| 40 | $count=$this->count(); |
||
| 41 | for($i=$count; $i < $rowCount; $i++) { |
||
| 42 | $this->addItem($colCount); |
||
| 43 | } |
||
| 44 | return $this; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function getTdTagName($tagName) { |
||
| 49 | } |
||
| 50 | |||
| 51 | public function refreshTR(){ |
||
| 52 | $this->_template="%wrapContentBefore%%content%%wrapContentAfter%"; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * |
||
| 57 | * {@inheritDoc} |
||
| 58 | * |
||
| 59 | * @see \Ajax\common\html\HtmlCollection::createItem() |
||
| 60 | * @return HtmlTR |
||
| 61 | */ |
||
| 62 | protected function createItem($value) { |
||
| 63 | $count=$this->count(); |
||
| 64 | $tr=new HtmlTR(""); |
||
| 65 | $tr->setContainer($this, $count); |
||
| 66 | $tr->setTdTagName($this->_tdTagNames[$this->tagName]); |
||
| 67 | if (isset($value) === true) { |
||
| 68 | $tr->setColCount($value); |
||
| 69 | } |
||
| 70 | return $tr; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function newRow($value) { |
||
| 74 | return $this->createItem($value); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param int $colCount |
||
| 79 | * @return HtmlTR |
||
| 80 | */ |
||
| 81 | public function addRow($colCount) { |
||
| 82 | return $this->addItem($colCount); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param mixed $row |
||
| 87 | * @return HtmlTR |
||
| 88 | */ |
||
| 89 | public function _addRow($row) { |
||
| 90 | return $this->addItem($row); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function addMergeRow($colCount,$value=null){ |
||
| 94 | $row=$this->addRow($colCount); |
||
| 95 | $row->mergeCol(); |
||
| 96 | if(isset($value)){ |
||
| 97 | $row->setValues([$value]); |
||
| 98 | } |
||
| 99 | return $row; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return HtmlTR |
||
| 104 | */ |
||
| 105 | public function getItem($index){ |
||
| 106 | return parent::getItem($index); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the cell (HtmlTD) at position $row,$col |
||
| 111 | * @param int $row |
||
| 112 | * @param int $col |
||
| 113 | * @return HtmlTD|HtmlDoubleElement |
||
| 114 | */ |
||
| 115 | public function getCell($row, $col) { |
||
| 116 | $row=$this->getItem($row); |
||
| 117 | if (isset($row) && $row instanceof HtmlCollection) { |
||
| 118 | $col=$row->getItem($col); |
||
| 119 | }else{ |
||
| 120 | $col=$row; |
||
| 121 | } |
||
| 122 | return $col; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * |
||
| 127 | * @param int $index |
||
| 128 | * @return HtmlTR |
||
| 129 | */ |
||
| 130 | public function getRow($index) { |
||
| 131 | return $this->getItem($index); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * |
||
| 136 | * @param int $row |
||
| 137 | * @param int $col |
||
| 138 | * @param mixed $value |
||
| 139 | * @return HtmlTableContent |
||
| 140 | */ |
||
| 141 | public function setCellValue($row, $col, $value="") { |
||
| 142 | $cell=$this->getCell($row, $col); |
||
| 143 | if (isset($cell) === true) { |
||
| 144 | $cell->setValue($value); |
||
| 145 | } |
||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Sets the cells values |
||
| 151 | * @param mixed $values |
||
| 152 | */ |
||
| 153 | public function setValues($values=array()) { |
||
| 154 | return $this->_addOrSetValues($values, function(HtmlTR $row,$_values){$row->setValues($_values);}); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Adds the cells values |
||
| 159 | * @param mixed $values |
||
| 160 | */ |
||
| 161 | public function addValues($values=array()) { |
||
| 162 | return $this->_addOrSetValues($values, function(HtmlTR $row,$_values){$row->addValues($_values);}); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Adds or sets the cells values |
||
| 167 | * @param mixed $values |
||
| 168 | * @param callable $callback |
||
| 169 | */ |
||
| 170 | protected function _addOrSetValues($values,$callback) { |
||
| 171 | $count=$this->count(); |
||
| 172 | $isArray=true; |
||
| 173 | if (!\is_array($values)) { |
||
| 174 | $values=\array_fill(0, $count, $values); |
||
| 175 | $isArray=false; |
||
| 176 | } |
||
| 177 | if (JArray::dimension($values) == 1 && $isArray) |
||
| 178 | $values=[ $values ]; |
||
| 179 | |||
| 180 | $count=\min(\sizeof($values), $count); |
||
| 181 | |||
| 182 | for($i=0; $i < $count; $i++) { |
||
| 183 | $row=$this->content[$i]; |
||
| 184 | $callback($row,$values[$i]); |
||
| 185 | } |
||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function setColValues($colIndex, $values=array()) { |
||
| 190 | $count=$this->count(); |
||
| 191 | if (!\is_array($values)) { |
||
| 192 | $values=\array_fill(0, $count, $values); |
||
| 193 | } |
||
| 194 | $count=\min(\sizeof($values), $count); |
||
| 195 | for($i=0; $i < $count; $i++) { |
||
| 196 | $this->getCell($i, $colIndex)->setValue($values[$i]); |
||
| 197 | } |
||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function addColVariations($colIndex, $variations=array()) { |
||
| 202 | $count=$this->count(); |
||
| 203 | for($i=0; $i < $count; $i++) { |
||
| 204 | $cell=$this->getCell($i, $colIndex); |
||
| 205 | if($cell instanceof BaseTrait) |
||
| 206 | $cell->addVariations($variations); |
||
| 207 | } |
||
| 208 | return $this; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function addPropertyCol($colIndex, $name,$value) { |
||
| 212 | $count=$this->count(); |
||
| 213 | for($i=0; $i < $count; $i++) { |
||
| 214 | $cell=$this->getCell($i, $colIndex); |
||
| 215 | if(isset($cell)) |
||
| 216 | $cell->addToProperty($name,$value); |
||
| 217 | } |
||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | public function setRowValues($rowIndex, $values=array()) { |
||
| 222 | $count=$this->count(); |
||
| 223 | if (!\is_array($values)) { |
||
| 224 | $values=\array_fill(0, $count, $values); |
||
| 225 | } |
||
| 226 | $this->getItem($rowIndex)->setValues($values); |
||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | |||
| 230 | private function colAlign($colIndex, $function) { |
||
| 231 | $count=$this->count(); |
||
| 232 | for($i=0; $i < $count; $i++) { |
||
| 233 | $index=$this->content[$i]->getColPosition($colIndex); |
||
| 234 | if ($index !== NULL) |
||
| 235 | $this->getCell($i, $index)->$function(); |
||
| 236 | } |
||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | private function colAlignFromRight($colIndex, $function) { |
||
| 241 | $count=$this->count(); |
||
| 242 | for($i=0; $i < $count; $i++) { |
||
| 243 | $maxRow=$this->content[$i]->count(); |
||
| 244 | $index=$maxRow-$colIndex-1; |
||
| 245 | if (($cell=$this->getCell($i, $index))!== NULL){ |
||
| 246 | if($cell->getColspan()==1) |
||
|
|
|||
| 247 | $cell->$function(); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function colCenter($colIndex) { |
||
| 254 | return $this->colAlign($colIndex, "textCenterAligned"); |
||
| 255 | } |
||
| 256 | |||
| 257 | public function colRight($colIndex) { |
||
| 258 | return $this->colAlign($colIndex, "textRightAligned"); |
||
| 259 | } |
||
| 260 | |||
| 261 | public function colLeft($colIndex) { |
||
| 262 | return $this->colAlign($colIndex, "textLeftAligned"); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function colCenterFromRight($colIndex) { |
||
| 266 | return $this->colAlignFromRight($colIndex, "textCenterAligned"); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function colRightFromRight($colIndex) { |
||
| 270 | return $this->colAlignFromRight($colIndex, "textRightAligned"); |
||
| 271 | } |
||
| 272 | |||
| 273 | public function colLeftFromRight($colIndex) { |
||
| 274 | return $this->colAlignFromRight($colIndex, "textLeftAligned"); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the number of rows (TR) |
||
| 279 | * @return int |
||
| 280 | */ |
||
| 281 | public function getRowCount() { |
||
| 282 | return $this->count(); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the number of columns (TD) |
||
| 287 | * @return int |
||
| 288 | */ |
||
| 289 | public function getColCount() { |
||
| 290 | $result=0; |
||
| 291 | if ($this->count() > 0) |
||
| 292 | $result=$this->getItem(0)->count(); |
||
| 293 | return $result; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Removes the cell at position $rowIndex,$colIndex |
||
| 298 | * @param int $rowIndex |
||
| 299 | * @param int $colIndex |
||
| 300 | * @return HtmlTableContent |
||
| 301 | */ |
||
| 302 | public function delete($rowIndex, $colIndex=NULL) { |
||
| 303 | if (isset($colIndex)) { |
||
| 304 | $row=$this->getItem($rowIndex); |
||
| 305 | if (isset($row) === true) { |
||
| 306 | $row->delete($colIndex); |
||
| 307 | } |
||
| 308 | } else { |
||
| 309 | $this->removeItem($rowIndex); |
||
| 310 | } |
||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | public function toDelete($rowIndex, $colIndex){ |
||
| 319 | } |
||
| 320 | |||
| 321 | public function mergeCol($rowIndex=0, $colIndex=0) { |
||
| 322 | return $this->getItem($rowIndex)->mergeCol($colIndex); |
||
| 323 | } |
||
| 324 | |||
| 325 | public function mergeRow($rowIndex=0, $colIndex=0) { |
||
| 326 | return $this->getItem($rowIndex)->mergeRow($colIndex); |
||
| 327 | } |
||
| 328 | |||
| 329 | public function setFullWidth() { |
||
| 330 | return $this->addToProperty("class", "full-width"); |
||
| 331 | } |
||
| 332 | |||
| 333 | public function sort($colIndex) { |
||
| 334 | $this->content[0]->getItem($colIndex)->addToProperty("class", "default-sort"); |
||
| 335 | return $this; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param mixed $callback |
||
| 340 | * @param string $format |
||
| 341 | * @return HtmlTableContent |
||
| 342 | */ |
||
| 343 | public function conditionalCellFormat($callback, $format) { |
||
| 344 | $rows=$this->content; |
||
| 345 | foreach ( $rows as $row ) { |
||
| 346 | $row->conditionalCellFormat($callback, $format); |
||
| 347 | } |
||
| 348 | return $this; |
||
| 349 | } |
||
| 350 | |||
| 351 | public function conditionalColFormat($colIndex,$callback,$format){ |
||
| 352 | $rows=$this->content; |
||
| 353 | foreach ( $rows as $row ) { |
||
| 354 | $cell=$row->getItem($colIndex); |
||
| 355 | $cell->conditionnalCellFormat($callback,$format); |
||
| 356 | } |
||
| 357 | return $this; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param mixed $callback |
||
| 362 | * @param string $format |
||
| 363 | * @return HtmlTableContent |
||
| 364 | */ |
||
| 365 | public function conditionalRowFormat($callback, $format) { |
||
| 366 | $rows=$this->content; |
||
| 367 | foreach ( $rows as $row ) { |
||
| 368 | $row->conditionalRowFormat($callback, $format); |
||
| 369 | } |
||
| 370 | return $this; |
||
| 371 | } |
||
| 372 | |||
| 373 | public function hideColumn($colIndex){ |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param mixed $callback |
||
| 384 | * @return HtmlTableContent |
||
| 385 | */ |
||
| 386 | public function applyCells($callback) { |
||
| 387 | $rows=$this->content; |
||
| 388 | foreach ( $rows as $row ) { |
||
| 389 | $row->applyCells($callback); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param mixed $callback |
||
| 396 | * @return HtmlTableContent |
||
| 397 | */ |
||
| 398 | public function applyRows($callback) { |
||
| 399 | $rows=$this->content; |
||
| 400 | foreach ( $rows as $row ) { |
||
| 401 | $row->apply($callback); |
||
| 402 | } |
||
| 403 | return $this; |
||
| 404 | } |
||
| 405 | |||
| 406 | public function mergeIdentiqualValues($colIndex,$function="strip_tags"){ |
||
| 407 | $rows=$this->content; |
||
| 408 | $identiqual=null; |
||
| 409 | $counter=0; |
||
| 410 | $cellToMerge=null; |
||
| 411 | $functionExists=\function_exists($function); |
||
| 412 | foreach ( $rows as $row ) { |
||
| 413 | $cell=$row->getItem($colIndex); |
||
| 414 | $value=$cell->getContent(); |
||
| 415 | if($functionExists) |
||
| 416 | $value=\call_user_func($function,$value); |
||
| 417 | if($value!==$identiqual){ |
||
| 418 | if($counter>0 && isset($cellToMerge)){ |
||
| 419 | $cellToMerge->setRowspan($counter); |
||
| 420 | } |
||
| 421 | $counter=0; |
||
| 422 | $cellToMerge=$cell; |
||
| 423 | $identiqual=$value; |
||
| 424 | } |
||
| 425 | $counter++; |
||
| 426 | } |
||
| 427 | if($counter>0 && isset($cellToMerge)){ |
||
| 428 | $cellToMerge->setRowspan($counter); |
||
| 429 | } |
||
| 430 | return $this; |
||
| 431 | } |
||
| 432 | |||
| 433 | public function _isMerged(){ |
||
| 434 | return $this->_merged; |
||
| 435 | } |
||
| 436 | |||
| 437 | public function _setMerged($value){ |
||
| 440 | } |
||
| 441 | } |
||
| 442 |