Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 function jsonSerialize()
 {
     $totals = $this->result->getTotalsForAllResults();
     $sanitizedTotals = array();
     foreach ($totals as $key => $value) {
         $replacedKey = str_replace(':', '_', $key);
         $sanitizedTotals[$replacedKey] = $value;
     }
     $columnHeaders = $this->result->getColumnHeaders();
     foreach ($columnHeaders as &$columnHeader) {
         $columnHeader['name'] = str_replace(':', '_', $columnHeader['name']);
     }
     $rows = $this->result->getRows();
     if (!is_array($rows)) {
         $rows = array();
     }
     $sanitizedRows = array();
     foreach ($rows as $rowIndex => $row) {
         foreach ($row as $columnIndex => $columnValue) {
             $columnName = $columnHeaders[$columnIndex]['name'];
             if ($columnName === 'ga_date') {
                 $columnValue = substr($columnValue, 0, 4) . '-' . substr($columnValue, 4, 2) . '-' . substr($columnValue, 6, 2);
             }
             $sanitizedRows[$rowIndex][$columnName] = $columnValue;
         }
         // The simple case that we have 2 columns with 1 dimension and 1 metric
         if (count($columnHeaders) == 2 && $columnHeaders[0]['columnType'] === 'DIMENSION' && $columnHeaders[1]['columnType'] === 'METRIC') {
             $sanitizedTotal = $sanitizedTotals[$columnHeaders[1]['name']];
             if ($sanitizedTotal > 0) {
                 $sanitizedRows[$rowIndex]['percent'] = round($row[1] / $sanitizedTotal * 100, 2);
             } else {
                 $sanitizedRows[$rowIndex]['percent'] = 0;
             }
         }
     }
     return array('totals' => $sanitizedTotals, 'rows' => $sanitizedRows);
 }
Exemplo n.º 2
-1
 /**
  * @test
  */
 public function jsonSerializeWithZeroTotalReturnsZeroPercent()
 {
     $data = new \Google_Service_Analytics_GaData();
     $data->setColumnHeaders(array(array('name' => 'ga:userType', 'columnType' => 'DIMENSION'), array('name' => 'ga:sessions', 'columnType' => 'METRIC')));
     $data->setTotalsForAllResults(array('ga:sessions' => 0));
     $data->setRows(array(array(0, 0)));
     $result = new DataResult($data);
     $serializableValue = $result->jsonSerialize();
     $this->assertArrayHasKey('rows', $serializableValue);
     $this->assertArrayHasKey(0, $serializableValue['rows']);
     $this->assertArrayHasKey('percent', $serializableValue['rows'][0]);
     $this->assertSame(0, $serializableValue['rows'][0]['percent'], 'Value for rows.0.percent should match');
 }