public function setUp()
 {
     $request = new Request();
     $data = new DataTable();
     $data->addColumn(new ColumnDescription('date', ValueType::DATE));
     $data->addColumn(new ColumnDescription('name', ValueType::STRING));
     $data->addColumn(new ColumnDescription('count', ValueType::NUMBER));
     $row = new TableRow();
     $row->addCell(new TableCell(new DateValue(new Date('2013-10-19'))));
     $row->addCell(new TableCell(new TextValue('eleven')));
     $row->addCell(new TableCell(new NumberValue(11)));
     $data->addRow($row);
     $row = new TableRow();
     $row->addCell(new TableCell(new DateValue(new Date('2013-01-01'))));
     $row->addCell(new TableCell(new TextValue('twenty_three')));
     $row->addCell(new TableCell(new NumberValue(23)));
     $data->addRow($row);
     // A row with a NULL value in the middle
     $row = new TableRow();
     $row->addCell(new TableCell(new DateValue(new Date('2013-07-01'))));
     $row->addCell(new TableCell(new TextValue(null)));
     $row->addCell(new TableCell(new NumberValue(35)));
     $data->addRow($row);
     // A row with a NULL value in the last cell
     $row = new TableRow();
     $row->addCell(new TableCell(new DateValue(new Date('1999-12-31'))));
     $row->addCell(new TableCell(new TextValue('null_value')));
     $row->addCell(new TableCell(new NumberValue(null)));
     $data->addRow($row);
     $this->dataTable = $data;
     $this->response = new Response($request);
     $this->response->setDataTable($data);
 }
 public function setUp()
 {
     $data = new DataTable();
     $data->addColumn(new ColumnDescription('day', ValueType::DATE));
     $data->addColumn(new ColumnDescription('country', ValueType::STRING));
     $data->addColumn(new ColumnDescription('region', ValueType::STRING));
     $data->addColumn(new ColumnDescription('income', ValueType::STRING));
     $data->addColumn(new ColumnDescription('expense', ValueType::STRING));
     $this->dataTable = $data;
 }
 public function testSetDataTable()
 {
     $request = new Request();
     $data = new DataTable();
     $data->addColumn(new ColumnDescription('textfield', ValueType::STRING));
     $data->addColumn(new ColumnDescription('intfield', ValueType::NUMBER));
     $response = new Response($request);
     $response->setDataTable($data);
     $getRequest = $response->getRequest();
     $getDataTable = $response->getDataTable();
     $this->assertEquals($request, $getRequest, "Request must match");
     $this->assertEquals($data, $getDataTable, "DataTable must match");
 }
 /**
  * @param Request $request
  * @return DataTable
  */
 public function &getDataTable(Request $request)
 {
     $data = new DataTable();
     $data->addColumn(new ColumnDescription('date', ValueType::DATE, 'Date'));
     $data->addColumn(new ColumnDescription('income', ValueType::NUMBER, 'Gross Income'));
     $data->addColumn(new ColumnDescription('expense', ValueType::NUMBER, 'Expenses'));
     $data->addColumn(new ColumnDescription('net', ValueType::NUMBER, 'Net Income'));
     for ($i = 0; $i < 10; $i++) {
         $date = "2013-01-" . sprintf("%02d", 1 + $i);
         $income = 1000 + rand(0, (1 + $i) * 10);
         $expense = -800 - rand(0, (1 + $i) * 10);
         $data->addRow(new TableRow(array(new Date($date), $income, $expense, $income + $expense)));
     }
     return $data;
 }
 public function setUp()
 {
     $data = new DataTable();
     // DO NOT change the order of this stuff it will break the tests
     $data->addColumn(new ColumnDescription('day', ValueType::DATE, 'Date'));
     $data->addColumn(new ColumnDescription('country', ValueType::STRING, 'Country'));
     $data->addColumn(new ColumnDescription('region', ValueType::STRING, 'Region'));
     $data->addColumn(new ColumnDescription('income', ValueType::NUMBER, 'Income'));
     $data->addColumn(new ColumnDescription('expense', ValueType::NUMBER, 'Expense'));
     // DO NOT change the order of this stuff it will break the tests
     $this->rawData = array(array(new Date('2013-10-01'), 'US', 'TX', 1000, 900), array(new Date('2013-10-01'), 'US', 'CA', 1000, 900), array(new Date('2013-10-01'), 'US', 'WA', 1000, 900), array(new Date('2013-10-02'), 'US', 'TX', 1000, 900), array(new Date('2013-10-02'), 'US', 'WA', 1000, 900), array(new Date('2013-10-03'), 'US', 'TX', 1000, 900), array(new Date('2013-10-03'), 'US', 'WA', 1000, 900));
     foreach ($this->rawData as $row) {
         $data->addRow(new TableRow($row));
     }
     $this->dataTable = $data;
 }
 public function &initPivotTable()
 {
     $dataOut = new DataTable();
     $dataIn =& $this->pivotDescription->getDataTable();
     // First add all the row fields.
     // Just copy these as-is from the input DataTable
     $rowFieldIndexes = $this->getRowFieldIndexes();
     foreach ($rowFieldIndexes as $columnIndex) {
         $columnDescription = $dataIn->getColumnDescription($columnIndex);
         $columnDescription->setCustomProperty('rowFieldIndex', $columnIndex);
         $dataOut->addColumn($columnDescription);
     }
     // Next add a column for each unique column in
     // the column key index
     $columnDescriptions = array();
     $columnFieldIndexes = $this->getColumnFieldIndexes();
     $numColumnFields = count($columnFieldIndexes);
     foreach ($columnFieldIndexes as $columnIndex) {
         $columnDescription = $dataIn->getColumnDescription($columnIndex);
         $columnDescriptions[] = $columnDescription;
     }
     $dataColumnDescriptions = array();
     $dataFieldIndexes = $this->getDataFieldIndexes();
     $numDataFields = count($dataFieldIndexes);
     foreach ($dataFieldIndexes as $columnIndex) {
         $columnDescription = $dataIn->getColumnDescription($columnIndex);
         $dataColumnDescriptions[] = $columnDescription;
     }
     $uniqueColumnKeys = $this->columnKeyIndex->getKeys();
     foreach ($uniqueColumnKeys as $hash => $key) {
         $idParts = array();
         $labelParts = array();
         $columnValues = $key->getColumnValues();
         for ($i = 0; $i < $numColumnFields; $i++) {
             $columnDescription = $columnDescriptions[$i];
             $columnValue = $columnValues[$i];
             $idParts[] = $columnDescription->getId() . '=' . $columnValue;
             $labelParts[] = $columnValue;
         }
         for ($di = 0; $di < $numDataFields; $di++) {
             $dataFieldIndex = $dataFieldIndexes[$di];
             $dataColumnDescription = $dataColumnDescriptions[$di];
             $fieldId = $dataColumnDescription->getId();
             $fieldLabel = $dataColumnDescription->getLabel();
             $aggregationType = $this->pivotDescription->getAggregationType($fieldId);
             $id = implode("__", $idParts) . "__" . $fieldId;
             $label = ($aggregationType ? $aggregationType->getCode() . ' ' : '') . implode(" ", $labelParts) . " " . $fieldLabel;
             // For now copy the same type as the input column.
             // In the future we may need to set this explicitly based on the calculation.
             $type = $dataColumnDescription->getType();
             $columnDescription = new ColumnDescription($id, $type, $label);
             $columnDescription->setCustomProperty('columnKeyId', $key->__toString());
             $columnDescription->setCustomProperty('dataFieldIndex', $dataFieldIndex);
             $dataOut->addColumn($columnDescription);
         }
     }
     return $dataOut;
 }
 /**
  * @param Request $request
  * @return DataTable
  */
 public function &getDataTable(Request $request)
 {
     $data = new DataTable();
     $data->addColumn(new ColumnDescription('date', ValueType::DATE, 'Date'));
     $data->addColumn(new ColumnDescription('country', ValueType::STRING, 'Country'));
     $data->addColumn(new ColumnDescription('region', ValueType::STRING, 'Region'));
     $data->addColumn(new ColumnDescription('income', ValueType::NUMBER, 'Income'));
     $data->addColumn(new ColumnDescription('expense', ValueType::NUMBER, 'Expense'));
     $countryRegions = array('US' => array('TX', 'CA', 'WA'), 'CA' => array(null));
     for ($i = 0; $i < 10; $i++) {
         $date = "2013-01-" . sprintf("%02d", 1 + $i);
         foreach ($countryRegions as $country => $regions) {
             foreach ($regions as $region) {
                 $income = 1000 + rand(0, (1 + $i) * 10);
                 $expense = -800 - rand(0, (1 + $i) * 10);
                 $row = new TableRow(array(new Date($date), $country, $region, $income, $expense));
                 $data->addRow($row);
             }
         }
     }
     return $data;
 }
 public static function &reduceData(Query $query, DataTable &$data)
 {
     // If we aren't narrowing the selection, return the source data
     $select = $query->getSelect();
     if ($select === null) {
         return $data;
     }
     // Get a list of the fields we want to select
     $selectFieldIds = $select->getAllColumnIds();
     $neededFieldIds = $selectFieldIds;
     // If we're going to pivot, we also need to preserve any/all
     // of the pivot fields.
     $pivot = $query->getPivot();
     if ($pivot !== null) {
         $neededFieldIds = array_merge($neededFieldIds, $pivot->getAllColumnIds());
     }
     // Keep just the unique field ids we need
     $neededFieldIds = array_unique($neededFieldIds);
     // If the number of unique selected fields equals the number of
     // input columns, we need all the data in the source table, so
     // just return the source table itself.
     $numInputColumns = $data->getNumberOfColumns();
     if (count($neededFieldIds) == $numInputColumns) {
         return $data;
     }
     // We're removing at least one column from the source table
     $result = new DataTable();
     // Copy the columns we're saving
     $preservedColumnIndexes = array();
     for ($ci = 0; $ci < $numInputColumns; $ci++) {
         $column = $data->getColumnDescription($ci);
         if (!in_array($column->getId(), $neededFieldIds)) {
             continue;
         }
         $result->addColumn($column);
         $preservedColumnIndexes[] = $ci;
     }
     // For each row, copy the cells from the columns we're saving
     $numInputRows = $data->getNumberOfRows();
     for ($ri = 0; $ri < $numInputRows; $ri++) {
         $inputRow = $data->getRow($ri);
         // Copy just the columns we're saving
         $row = new TableRow();
         foreach ($preservedColumnIndexes as $ci) {
             $row->addCell($inputRow->getCell($ci));
         }
         $result->addRow($row);
     }
     return $result;
 }
 public function testNullCellTypeCast()
 {
     $data = new DataTable();
     // a number column
     $data->addColumn(new ColumnDescription('val', ValueType::NUMBER));
     // add a NULL string value to the number column
     $row = new TableRow();
     $row->addCell(new TextValue(null));
     $data->addRow($row);
     // It should have been casted to a NULL number value
     $value = $data->getRow(0)->getCell(0)->getValue();
     $this->assertSame(1, $data->getNumberOfRows(), "row count must match");
     $this->assertTrue($value instanceof NumberValue, "expect a TimeOfDayValue in the table");
     $this->assertTrue($value->isNull(), "expect value->isNull == true");
 }