makeFromSimpleArray() public static method

See {@link addRowsFromSimpleArray()}.
public static makeFromSimpleArray ( array $array ) : DataTable
$array array
return DataTable
Esempio n. 1
0
 /**
  * Computes the output for the given data table
  *
  * @param DataTable $table
  * @return string
  */
 protected function renderTable($table)
 {
     if (is_array($table)) {
         $table = DataTable::makeFromSimpleArray($table);
     }
     if ($table instanceof DataTable\Map) {
         foreach ($table->getDataTables() as $date => $subtable) {
             if ($subtable->getRowsCount()) {
                 $this->buildTableStructure($subtable, '_' . $table->getKeyName(), $date);
             }
         }
     } else {
         if ($table->getRowsCount()) {
             $this->buildTableStructure($table);
         }
     }
     $out = $this->renderDataTable();
     return $out;
 }
Esempio n. 2
0
 private function generateDataTableWithManySubtables($numSubtables)
 {
     $dataTable = new DataTable();
     for ($i = 1; $i <= $numSubtables; $i++) {
         $row = new Row(array(Row::COLUMNS => array('label' => 'Label Test ' . $i, 'nb_visits' => $i)));
         $subtable = DataTable::makeFromSimpleArray(array(array('label' => 'subtable' . $i, 'nb_visits' => $i)));
         $row->setSubtable($subtable);
         $dataTable->addRow($row);
     }
     return $dataTable->getSerialized();
 }
Esempio n. 3
0
 /**
  * @group Core
  */
 public function test_disableFilter_DoesActuallyDisableAFilter()
 {
     $dataTable = DataTable::makeFromSimpleArray(array_fill(0, 100, array()));
     $this->assertSame(100, $dataTable->getRowsCount());
     $dataTable2 = clone $dataTable;
     // verify here the filter is applied
     $dataTable->filter('Limit', array(10, 10));
     $this->assertSame(10, $dataTable->getRowsCount());
     // verify here the filter is not applied as it is disabled
     $dataTable2->disableFilter('Limit');
     $dataTable2->filter('Limit', array(10, 10));
     $this->assertSame(100, $dataTable2->getRowsCount());
     // passing a whole className is expected to work. This way we also make sure not all filters are disabled
     // and it only blocks the given one
     $dataTable2->filter('Piwik\\DataTable\\Filter\\Limit', array(10, 10));
     $this->assertSame(10, $dataTable2->getRowsCount());
 }
Esempio n. 4
0
 /**
  * Computes the output of the given data table
  *
  * @param DataTable|array $table
  * @param array $allColumns
  * @return string
  */
 protected function renderTable($table, &$allColumns = array())
 {
     if (is_array($table)) {
         $table = DataTable::makeFromSimpleArray($table);
     }
     if ($table instanceof DataTable\Map) {
         $str = $this->renderDataTableMap($table, $allColumns);
     } else {
         $str = $this->renderDataTable($table, $allColumns);
     }
     return $str;
 }
Esempio n. 5
0
 /**
  * Computes the given dataTable output and returns the string/binary
  *
  * @param DataTable $table data table to render
  * @param string $prefix prefix to output before table data
  * @return string
  */
 protected function renderTable($table, $prefix = "")
 {
     if (is_array($table)) {
         // convert array to DataTable
         $table = DataTable::makeFromSimpleArray($table);
     }
     if ($table instanceof DataTable\Map) {
         return $this->renderDataTableMap($table, $prefix);
     }
     if ($table->getRowsCount() == 0) {
         return "Empty table<br />\n";
     }
     static $depth = 0;
     $output = '';
     $i = 1;
     foreach ($table->getRows() as $row) {
         $dataTableMapBreak = false;
         $columns = array();
         foreach ($row->getColumns() as $column => $value) {
             if ($value instanceof DataTable\Map) {
                 $output .= $this->renderDataTableMap($value, $prefix);
                 $dataTableMapBreak = true;
                 break;
             }
             if (is_string($value)) {
                 $value = "'{$value}'";
             } elseif (is_array($value)) {
                 $value = var_export($value, true);
             }
             $columns[] = "'{$column}' => {$value}";
         }
         if ($dataTableMapBreak === true) {
             continue;
         }
         $columns = implode(", ", $columns);
         $metadata = array();
         foreach ($row->getMetadata() as $name => $value) {
             if (is_string($value)) {
                 $value = "'{$value}'";
             } elseif (is_array($value)) {
                 $value = var_export($value, true);
             }
             $metadata[] = "'{$name}' => {$value}";
         }
         $metadata = implode(", ", $metadata);
         $output .= str_repeat($this->prefixRows, $depth) . "- {$i} [" . $columns . "] [" . $metadata . "] [idsubtable = " . $row->getIdSubDataTable() . "]<br />\n";
         if (!is_null($row->getIdSubDataTable())) {
             $subTable = $row->getSubtable();
             if ($subTable) {
                 $depth++;
                 $output .= $this->renderTable($subTable, $prefix . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
                 $depth--;
             } else {
                 $output .= "-- Sub DataTable not loaded<br />\n";
             }
         }
         $i++;
     }
     $metadata = $table->getAllTableMetadata();
     if (!empty($metadata)) {
         $output .= "<hr />Metadata<br />";
         foreach ($metadata as $id => $metadataIn) {
             $output .= "<br />";
             $output .= $prefix . " <b>{$id}</b><br />";
             if (is_array($metadataIn)) {
                 foreach ($metadataIn as $name => $value) {
                     $output .= $prefix . $prefix . "{$name} => {$value}";
                 }
             }
         }
     }
     return $output;
 }
Esempio n. 6
0
 /**
  * Another example method that returns a data table.
  * @param int    $idSite
  * @param string $period
  * @param string $date
  * @param bool|string $segment
  * @return DataTable
  */
 public function getExampleReport($idSite, $period, $date, $segment = false)
 {
     $table = DataTable::makeFromSimpleArray(array(array('label' => 'My Label 1', 'nb_visits' => '1'), array('label' => 'My Label 2', 'nb_visits' => '5')));
     return $table;
 }