public function testMapRows() { $column = new Column(); $column->setProperty('name'); $column->setCellType(new TextCell()); $column->setLabel('User name'); $columnCollection = new ArrayCollection(); $columnCollection->add($column); $user = new User(); $user->setId(1); $user->setName('some random name'); $user->setEmail('*****@*****.**'); $rowCollection = new ArrayCollection(); $rowCollection->add($user); $mapper = new DatagridMapper(); $actual = $mapper->mapRows($rowCollection, $columnCollection); $cell = new Cell(); $cell->setProperty('name'); $cell->setValue('some random name'); $cell->setExportValue('some random name'); $cell->setType('text'); $cell->setView('text'); $cell->setAttributes([]); $row = new Row(); $row->setId(1); $row->setName(1); $row->setObject($user); $row->addCell($cell); $expected = new ArrayCollection(); $expected->add($row); $this->assertEquals($actual, $expected); }
public function testCreateExportObject() { $this->factory->shouldReceive('createPHPExcelObject')->andReturn(new PHPExcel()); $column = new Column(); $column->setLabel('Label'); $datagrid = new Datagrid(); $datagrid->addColumn($column); $export = new ExcelExport($this->factory); $exportObject = $export->createExportObject($datagrid); $this->assertInstanceOf('PHPExcel', $exportObject); $this->assertEquals('Label', $exportObject->getActiveSheet()->getCell('A1')->getValue()); }
/** * Create a cell * * @param Column $column * @param object $row * * @return Cell */ public function create(Column $column, $row) { $cellType = $column->getCellType(); $cell = new Cell(); $cell->setValue($cellType->getData($row, $column, $column->getCellAttributes())); $cell->setExportValue($cellType->getExportData($row, $column, $column->getCellAttributes())); $cell->setAttributes($column->getCellAttributes()); $cell->setType($cellType->getName()); $cell->setView($cellType->getView()); $cell->setProperty($column->getProperty()); return $cell; }
/** * {@inheritDoc} */ public function addColumn($property, $cell, array $options = array(), $sortable = true) { if (!$cell instanceof CellTypeInterface) { $cell = $this->container->get('opifer.crud.cell_registry')->getCellType($cell); } $column = new Column(); $column->setProperty($property); $column->setCellType($cell); $column->setAttributes($options); $column->setSortable($sortable); if (isset($options['label'])) { $column->setLabel($options['label']); } $this->mapper->addColumn($column); return $this; }
/** * {@inheritDoc} */ public function getData($row, Column $column, array $attributes) { $accessor = PropertyAccess::getPropertyAccessor(); return $accessor->getValue($row, $column->getProperty()); }
/** * Transform the columns to be used within the grid * * @param array $columns * @return array */ public function mapColumns($columns) { if ($columns instanceof ArrayCollection) { return $columns; } $collection = new ArrayCollection(); foreach ($columns as $columnArray) { if (isset($columnArray['options']['label'])) { $label = $columnArray['options']['label']; } else { $label = ucfirst(trim(preg_replace(['/[_\\W]+/', '/([a-z])([A-Z])/'], [' ', '$1 $2'], $columnArray['property']))); } $column = new Column(); $column->setProperty($columnArray['property']); if ($columnArray['type'] == 'datetime') { $column->setCellType(new DateTimeCell()); } else { $column->setCellType(new TextCell()); } $column->setLabel($label); $collection->add($column); } return $collection; }