Overview

Namespaces

  • mechanicious
    • Columnizer
    • Extensions
      • Bs3Table
    • Support
    • Tableman
    • TablemanExtension
    • Test
      • Tableman
  • PHP

Classes

  • Column
  • ColumnBag
  • Columnizer
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php namespace mechanicious\Columnizer;
  2: 
  3: use Illuminate\Support\Collection;
  4: 
  5: class Columnizer
  6: {
  7:   /**
  8:    *  Holds the items
  9:    * @var array
 10:    */
 11:   public $items = array();
 12: 
 13:   /**
 14:    *  Initialize the columnizer by providing an array with data
 15:    * @param array $data
 16:    */
 17:   public function __construct(array $data = array())
 18:   {
 19:     $this->items = $data; 
 20:   }
 21: 
 22:   /**
 23:    *  Split an array of rows into symmetric columns 
 24:    * 
 25:    * @return  mechanicious\Columnizer\ColumnBag
 26:    */
 27:   public function columnizeRowArray()
 28:   {
 29:     if($this->items instanceof ColumnBag) return $this->items;
 30:     // Somtimes the data has already a columnized structure, then you
 31:     // just want to wrap it in the bag.
 32: 
 33:     $columnized = $this->columnizeItems();
 34:     $assemble   = array();
 35:     array_walk($columnized, function($column, $columnName) use(&$assemble) {
 36:       $assemble[$columnName] = new Column($column, $columnName);
 37:     });
 38:     return new ColumnBag($assemble);
 39:   }
 40: 
 41:   /**
 42:    *  Pre-fill missing columns to line the rows up 
 43:    * @return  void
 44:    */
 45:   protected function symmetrize()
 46:   {
 47:     $columnNames = $this->identifyColumns();
 48:     $columnCount = count($this->identifyColumns());
 49:     $items = &$this->items;
 50:     array_walk($items, function(&$row, $rowIndex) use($columnCount, $columnNames) {
 51:       if(count($row) < $columnCount)
 52:       {
 53:         // We have to pre-fill the missing columns in other rows
 54:         // to avoid complications later on.
 55:         foreach($row as $cellIndex => $cell) {
 56:           foreach($columnNames as $columnName) {
 57:             if( ! isset($row[$columnName]))
 58:               $row[$columnName] = null;
 59:           }
 60:         }
 61:       }
 62:     });
 63:   }
 64: 
 65:   /**
 66:    *  Columnize data
 67:    * 
 68:    * @return  array
 69:    */
 70:   protected function columnizeItems()
 71:   {
 72:     $this->symmetrize();
 73:     $items = &$this->items;
 74:     $columnNames = $this->identifyColumns();
 75:     $columnized = array();
 76: 
 77:     array_walk($columnNames, function($columnName, $index) use ($items, &$columnized) {
 78:       $columnized[$columnName] = array_column($items, $columnName);
 79:     });
 80:     return $columnized;
 81:   }
 82: 
 83:   /**
 84:    *  Identify what the columns are 
 85:    * @return array
 86:    */
 87:   public function identifyColumns()
 88:   {
 89:     $largest = array();
 90:     $items = &$this->items;
 91:     // We can deal with a situation when the data is
 92:     // inconsistent. One row may have one extra column
 93:     // somehow. So we'll assume that the largest row
 94:     // contains a complete set of columns.
 95:     array_walk($items, function($item, $key) use($items, &$largest) {
 96:       if(count($items[$key]) > count($largest)) $largest = $items[$key];
 97:     });
 98: 
 99:     return $columns = array_keys($largest);
100:   }
101: }
API documentation generated by ApiGen 2.8.0