/** * @param TableCell $cell */ public function addCell(TableCell $cell) { if (!$cell->getValue()->isNull()) { $value = $cell->getValue()->getRawValue(); if ($this->min > $value) { $this->min = $value; } else { if ($this->min === null) { $this->min = $value; } } } }
/** * @param TableCell $cell */ public function addCell(TableCell $cell) { $this->numCells++; $this->totalValue += $cell->getValue()->getRawValue(); }
/** * @param TableCell $cell * @param bool $includeFormatting * @param bool $optimizeNullValues [optional] Default=FALSE * @param bool $renderDateAsDateConstructor [optional] Default=TRUE * @return string JSON formatted string representing this TableCell * @throws NoSuchValueTypeException */ public function renderCellJson(TableCell $cell, $includeFormatting, $optimizeNullValues = false, $renderDateAsDateConstructor = true) { $value = $cell->getValue(); $valueType = $cell->getValueType(); $valueTypeCode = $valueType->getCode(); $isJsonNull = false; $escapedFormattedValue = null; $json = ""; if ($value->isNull()) { $json .= "null"; $isJsonNull = true; } else { switch ($valueTypeCode) { case ValueType::BOOLEAN: $json .= $value->__toString(); break; case ValueType::NUMBER: case ValueType::STRING: if ($value->isNull()) { $json .= "null"; } else { $json .= json_encode($value->__toString()); } break; case ValueType::DATE: case ValueType::DATETIME: if ($value->isNull()) { $json .= "null"; } else { $date = $value->getRawValue(); $year = $date->getYear(); $month = $date->getMonth(); $day = $date->getMonthDay(); $dateValue = "Date({$year},{$month},{$day}"; if ($valueTypeCode === ValueType::DATETIME) { $hours = $date->getHours(); $mins = $date->getMinutes(); $secs = $date->getSeconds(); $dateValue .= ",{$hours},{$mins},{$secs}"; } $dateValue .= ")"; if ($renderDateAsDateConstructor) { $json .= "new " . $dateValue; } else { $json .= "\"" . $dateValue . "\""; } } break; case ValueType::TIMEOFDAY: if ($value->isNull()) { $json .= "null"; } else { $date = $value->getRawValue(); $hours = $date->getHours(); $mins = $date->getMinutes(); $secs = $date->getSeconds(); $millis = $date->getMilliseconds(); $json .= "[{$hours},{$mins},{$secs},{$millis}]"; } break; default: throw new NoSuchValueTypeException($valueTypeCode); } } if ($includeFormatting) { $formattedValue = $cell->getFormattedValue(); if (!$value->isNull() && $formattedValue !== null) { // For strings if the value === formattedValue then // don't send the formattedValue if ($valueTypeCode !== ValueType::STRING || $value->getValue() !== $formattedValue) { $escapedFormattedValue = json_encode($formattedValue); } } } // Add a Json for this cell. And, // 1) If the formatted value is empty drop it. // 2) If the value is null, and it is not the last column in the row drop the entire Json. if ($isJsonNull && $optimizeNullValues) { $output = ""; } else { $output = "{"; // Value $output .= "\"v\":" . $json; // Formatted value if ($includeFormatting && $escapedFormattedValue != '') { $output .= ",\"f\":" . $escapedFormattedValue; } // Custom properties $customProperties = $this->renderCustomPropertiesString($cell->getCustomProperties()); if ($customProperties !== null) { $output .= ",\"p\":" . $customProperties; } $output .= "}"; } return $output; }
public function testRenderCellJsonWithFormattingAndProperties() { $expected = '{"v":new Date(2013,9,19),"f":"2013-10-19","p":{"prop":"val"}}'; $date = new Date('Oct 19, 2013'); $formattedDate = $date->format("Y-m-d"); $cell = new TableCell(new DateValue($date), $formattedDate); $cell->setCustomProperty('prop', 'val'); $renderer = new JsonRenderer(); $actual = $renderer->renderCellJson($cell, true, false, true); $this->assertSame($expected, $actual, "TableCell rendered json must match expected value"); }
public function testGetCustomNonexistentProperty() { $cell = new TableCell(new TextValue('test')); $this->assertNull($cell->getCustomProperty('no-such-property'), "Expect null return for no-such-property"); }