Overview

Namespaces

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

Classes

  • Tableman
  • TablemanCollection
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php namespace mechanicious\Tableman;
  2: 
  3: use mechanicious\Columnizer\ColumnBag;
  4: use mechanicious\Columnizer\Column;
  5: use mechanicious\Columnizer\Columnizer;
  6: use mechanicious\TablemanExtension\TablemanExtension;
  7: use mechanicious\TablemanExtension\Config;
  8: use Illuminate\Support\Collection;
  9: 
 10: /**
 11:  * Responsiblity to make Collection support Tableman. 
 12:  * Adds a Collection compatibility(kind of) layer for Tableman.
 13:  */
 14: class TablemanCollection extends Collection
 15: {
 16:   /**
 17:    * Create a whole new Tableman instance. Extensions will be purged.
 18:    * 
 19:    * @return mechanicious\Tableman\Tableman
 20:    */
 21:   static function make(ColumnBag $columns)
 22:   {
 23:     if (is_null($columns)) return new static($columns);
 24: 
 25:     if ($columns instanceof Tableman) return $columns;
 26: 
 27:     return new static($columns);
 28:   }
 29: 
 30:  /**
 31:    * Collapse the collection items into a single array.
 32:    *
 33:    * @return \Illuminate\Support\Collection
 34:    */
 35:   public function collapse()
 36:   {
 37:     $results = array();
 38:     foreach ($this->items as $column)
 39:     {
 40:       $results = array_merge($results, $column->toArray());
 41:     }
 42: 
 43:     return new Collection($results);
 44:   } 
 45: 
 46:   /**
 47:    * Returns what the object running this method has but what $items doesn't have. Using row-set comparison.
 48:    *
 49:    * @param  \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array  $items
 50:    * @return array
 51:    */
 52:   public function diff($items)
 53:   {
 54:     $items = $this->getArrayableItems($items);
 55:     // What $has has, but $leaks leaks.
 56:     return $this->array_diff_recursive($has = $this->getRows(), $leaks = $items);
 57:   }
 58: 
 59:   /**
 60:    * Execute a callback over each item. Alias of Tableman::eachColumn
 61:    * 
 62:    * @param  closure $callback
 63:    * @return mechanicious\Tableman\Tableman
 64:    */
 65:   public function each(\closure $callback)
 66:   {
 67:     $this->eachColumn($cllback);
 68:   }
 69: 
 70:   /**
 71:    * Fetch a nested element of the collection.
 72:    *
 73:    * @param  string  $key
 74:    * @return \Illuminate\Support\Collection
 75:    */
 76:   public function fetch($key)
 77:   {
 78:     return new Collection(array_fetch(with(new Collection($this->getRows()))->items, $key));
 79:   }
 80: 
 81:   /**
 82:    * Run a filter over each of the items.
 83:    *
 84:    * @param  Closure  $callback
 85:    * @return \Illuminate\Support\Collection
 86:    */
 87:   public function filter(\Closure $callback)
 88:   {
 89:     return new Collection(array_filter(with(new Collection($this->getRows()))->items, $callback));
 90:   }
 91: 
 92:   /**
 93:    * Get a flattened array of the items in the collection.
 94:    *
 95:    * @return array
 96:    */
 97:   public function flatten()
 98:   {
 99:     return new Collection(array_flatten($this->items));
100:   }
101: 
102:     /**
103:    * Group an associative array by a field or Closure value.
104:    *
105:    * @param  callable|string  $groupBy
106:    * @return \Illuminate\Support\Collection
107:    */
108:   public function groupBy($groupBy)
109:   {
110:     // I'll just leave it like that for now.
111:     $results = array();
112: 
113:     foreach ($this->toArray() as $key => $value)
114:     {
115:       $key = is_callable($groupBy) ? $groupBy($value, $key) : data_get($value, $groupBy);
116: 
117:       $results[$key][] = $value;
118:     }
119: 
120:     return new Collection($results);
121:   }
122: 
123:   /**
124:    * Concatenate values of a given header as a string.
125:    * 
126:    * @param  string $header
127:    * @param  string $glue
128:    * @return string
129:    */
130:   public function implode($header, $glue = ", ")
131:   {
132:     $items = $this->items[$header];
133:     return implode($glue, $items->toArray());
134:   }
135: 
136:   /**
137:    * Results array of items from Collection or ArrayableInterface.
138:    *
139:    * @param  \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array  $items
140:    * @return array
141:    */
142:   protected function getArrayableItems($items)
143:   {
144:     if(is_array($items)) return $items;
145:     if ($items instanceof ColumnBag || $items instanceof ColumnBag || $items instanceof ArrayableInterface)
146:     {
147:       return $items->toArray();
148:     }
149:     return new \Exception(__METHOD__ . ' could not convert to array');
150:   }
151: }
API documentation generated by ApiGen 2.8.0