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; }
/** * Apply default settings to rowFields and dataFields if needed * @throws NoDataFieldsException If there are no row fields * @throws Exception if the DataTable cannot answer this pivot request */ protected function fixupFields() { $this->isFixed = true; // If they didn't give us any column fields, there is nothing to pivot! if (empty($this->columnFields)) { return; } // If they didn't give us any row fields, by default the first field is the row field if ($this->rowFields === null) { $this->rowFields = array($this->dataTable->getColumnDescription(0)->getId()); } // If they didn't give us any data fields, by default ALL fields that are neither // column fields nor row fields are data fields if ($this->dataFields === null) { $dataFields = array(); $allColumnIds = $this->getAllColumnIds($this->dataTable->getColumnDescriptions()); foreach ($allColumnIds as $fieldId) { if (!in_array($fieldId, $this->columnFields) && !in_array($fieldId, $this->rowFields)) { $dataFields[] = $fieldId; } } $this->dataFields = $dataFields; } if (count($this->dataFields) === 0) { throw new NoDataFieldsException(); } // Verify that the same field is not specified in multiple types $this->verifyNoDuplicateFields($this->columnFields, $this->rowFields, 'column', 'row'); $this->verifyNoDuplicateFields($this->columnFields, $this->dataFields, 'column', 'data'); $this->verifyNoDuplicateFields($this->rowFields, $this->dataFields, 'row', 'data'); // Verify the minimum number of fields exist in this DataTable to perform this pivot $numColumns = $this->dataTable->getNumberOfColumns(); $minimumNumColumns = count($this->columnFields) + count($this->rowFields) + count($this->dataFields); if ($numColumns < $minimumNumColumns) { throw new Exception("Not enough columns in this DataTable to pivot (you have " . $numColumns . ", minimum is " . $minimumNumColumns . ")"); } }
public function testGetColumnDescriptionOutOfBounds() { $data = new DataTable(); $this->setExpectedException('\\Vube\\GoogleVisualization\\DataSource\\Exception\\IndexOutOfBoundsException'); $cd = $data->getColumnDescription(0); }