public function displayGridValue($row, Field $field) { // parse field name and get value after the dot $fieldNameTab = explode('.', $field->getFieldName()); if (in_array($fieldNameTab[0], $this->rootAliases)) { array_shift($fieldNameTab); } $value = $row; while (count($fieldNameTab) > 0) { $fieldName = array_shift($fieldNameTab); // get parameter in the $row $value = $value[$fieldName]; } // $fieldName = array_shift($fieldNameTab); // $value = $row[$fieldNameTab]; // real treatment if (is_callable($field->getFormatValueCallback())) { $callback = $field->getFormatValueCallback(); $reflection = new \ReflectionFunction($callback); if ($reflection->getNumberOfParameters() == 1) { $returnValue = $callback($value); } elseif ($reflection->getNumberOfParameters() == 2) { $returnValue = $callback($value, $row); } else { throw new DataGridException("Wrong number of parameters in the callback for field " . $field->getFieldName()); } } elseif (is_scalar($value)) { $returnValue = $value; } elseif ($value instanceof \DateTime) { $returnValue = $value->format("Y-m-d H:i:s"); } else { $returnValue = $value; } // auto escape ? if ($field->getAutoEscape()) { $returnValue = htmlspecialchars($returnValue); } return $returnValue; }
public function testConstructor() { $field = new Field("id"); $this->assertEquals($field->getFieldName(), "id"); $field = new Field("phone", array("label" => "Phone", "sortable" => true, "filterable" => true, "visible" => false, "formatValueCallback" => function ($value) { return strtoupper($value); }, "autoEscape" => true, "translatable" => true)); $this->assertEquals("Phone", $field->getLabel()); $this->assertTrue($field->getSortable()); $this->assertTrue($field->getFilterable()); $this->assertFalse($field->getVisible()); $this->assertNotNull($field->getFormatValueCallback()); $this->assertTrue($field->getFilterable()); $this->assertTrue($field->getFilterable()); }