Exemplo n.º 1
0
 /**
  * Returns a DataTable from a simple matrix (array of array, with first row = label if hasHeader = true)
  * @param array $array
  * @param boolean $hasHeader if true, indicates that the first row contains the labels
  * @return DataTable 
  */
 public static function fromSimpleMatrix(array $array, $hasHeader = true)
 {
     $dataTable = new DataTable();
     $labelArray = array();
     if ($hasHeader) {
         $labelArray = $array[0];
         array_shift($array);
     }
     //need to found out the data type... just string or number
     $firstDataRow = $array[0];
     foreach ($firstDataRow as $key => $value) {
         $type = "string";
         $label = isset($labelArray[$key]) ? $labelArray[$key] : 'c' . $key;
         if (is_object($value) && $value instanceof \DateTime) {
             $type = 'datetime';
         } elseif (is_bool($value)) {
             $type = 'boolean';
         } elseif (is_numeric($value)) {
             $type = 'number';
         }
         $dataTable->addColumn('id' . $key, $label, $type);
     }
     //now the data...
     foreach ($array as $key => $row) {
         $dataTable->addRow($row);
     }
     return $dataTable;
 }