Skip to content

Commit

Permalink
Correction d'un problème de double foreach et d'indexation des lignes…
Browse files Browse the repository at this point in the history
…/cells
  • Loading branch information
valentin-claras committed Feb 13, 2014
1 parent edfe2ad commit 9572972
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ public function build(Sheet $sheet, $yamlContent, Scope $scope)
protected function parseLine(Table $table, $yamlLine, Scope $scope)
{
if (is_array($yamlLine) && array_key_exists('foreach', $yamlLine)) {
$this->parseForeach($yamlLine, $scope, [$this, 'parseLine'], [$table]);
return $this->parseForeach($yamlLine, $scope, [$this, 'parseLine'], [$table]);
} else {
$this->createLine($table, $yamlLine, $scope);
return 1;
}
}

Expand All @@ -79,9 +80,10 @@ protected function createLine(Table $table, $yamlLine, Scope $scope)
protected function parseColumn(Table $table, $yamlColumn, Scope $scope)
{
if (is_array($yamlColumn) && array_key_exists('foreach', $yamlColumn)) {
$this->parseForeach($yamlColumn, $scope, [$this, 'parseColumn'], [$table]);
return $this->parseForeach($yamlColumn, $scope, [$this, 'parseColumn'], [$table]);
} else {
$this->createColumn($table, $yamlColumn, $scope);
return 1;
}
}

Expand All @@ -105,7 +107,7 @@ protected function parseCell(Table $table, Column $column, $lineIndex, $yamlCell
$lines = $table->getLines();

if (is_array($yamlCell) && array_key_exists('foreach', $yamlCell)) {
return $this->parseForeach($yamlCell, $scope, [$this, 'parseCell'], [$table, $column, $lineIndex]);
return $this->parseForeach($yamlCell, $scope, [$this, 'parseCell'], [$table, $column, ($lineIndex + $lineIteration)]);
} else {
$this->createCell($table, $column, $lines[$lineIndex + $lineIteration], $yamlCell, $scope);
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ public function build(Sheet $sheet, $yamlContent, Scope $scope)
protected function parseColumn(Table $table, $yamlColumn, Scope $scope)
{
if (is_array($yamlColumn) && array_key_exists('foreach', $yamlColumn)) {
$this->parseForeach($yamlColumn, $scope, [$this, 'parseColumn'], [$table]);
return $this->parseForeach($yamlColumn, $scope, [$this, 'parseColumn'], [$table]);
} else {
$this->createColumn($table, $yamlColumn, $scope);
return 1;
}
}

Expand All @@ -79,9 +80,10 @@ protected function createColumn(Table $table, $yamlColumn, Scope $scope)
protected function parseLine(Table $table, $yamlLine, Scope $scope)
{
if (is_array($yamlLine) && array_key_exists('foreach', $yamlLine)) {
$this->parseForeach($yamlLine, $scope, [$this, 'parseLine'], [$table]);
return $this->parseForeach($yamlLine, $scope, [$this, 'parseLine'], [$table]);
} else {
$this->createLine($table, $yamlLine, $scope);
return 1;
}
}

Expand All @@ -105,7 +107,7 @@ protected function parseCell(Table $table, Line $line, $columnIndex, $yamlCell,
$columns = $table->getColumns();

if (is_array($yamlCell) && array_key_exists('foreach', $yamlCell)) {
return $this->parseForeach($yamlCell, $scope, [$this, 'parseCell'], [$table, $line, $columnIndex]);
return $this->parseForeach($yamlCell, $scope, [$this, 'parseCell'], [$table, $line, ($columnIndex + $columnIteration)]);
} else {
$this->createCell($table, $line, $columns[$columnIndex + $columnIteration], $yamlCell, $scope);
return 1;
Expand Down
6 changes: 4 additions & 2 deletions src/Xport/Spreadsheet/Builder/ModelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ protected function doLoop($yamlLoop, array $scopes, callable $callback, array $p
foreach ($scopes as $scope) {
// Traverse all sub-elements.
foreach ($yamlLoop['do'] as $yamlElement) {
call_user_func_array($callback, array_merge($parameters, [$yamlElement, $scope, $iterationCallback]));
$iterationCallback ++;
$iterationCallback += call_user_func_array(
$callback,
array_merge($parameters, [$yamlElement, $scope, $iterationCallback])
);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Xport/Spreadsheet/Builder/SpreadsheetModelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ protected function parseRoot(Document $document, $yamlRoot, Scope $scope)
protected function parseSheet(Document $document, $yamlSheet, Scope $scope)
{
if (array_key_exists('foreach', $yamlSheet)) {
$this->parseForeach($yamlSheet, $scope, [$this, 'parseSheet'], [$document]);
return $this->parseForeach($yamlSheet, $scope, [$this, 'parseSheet'], [$document]);
} else {
$this->createSheet($document, $yamlSheet, $scope);
return 1;
}
}

Expand All @@ -145,9 +146,10 @@ protected function createSheet(Document $document, $yamlSheet, Scope $scope)
protected function parseContent(Sheet $sheet, $yamlContent, Scope $scope)
{
if (array_key_exists('foreach', $yamlContent)) {
$this->parseForeach($yamlContent, $scope, [$this, 'parseContent'], [$sheet]);
return $this->parseForeach($yamlContent, $scope, [$this, 'parseContent'], [$sheet]);
} else {
$this->createContent($sheet, $yamlContent, $scope);
return 1;
}
}

Expand Down
101 changes: 101 additions & 0 deletions tests/XportTest/Builder/SpreadsheetModelBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,107 @@ public function testContentForEachForEach()
$this->assertEquals($sheet->getTables()[0], $sheet->getTables()[2]);
}

public function testCellsForEachForEach()
{
$mapping = [
'sheets' => [
[
'content' => [
[
'type' => 'VerticalTable',
'columns' => [
[
'foreach' => 'headerList as header',
'do' => [
[
'foreach' => 'header as letter',
'do' => [
'{{ letter }}'
]
]
]
],
'Col1',
'Col2',
],
'lines' => [
[
'foreach' => 'foo as i => bar',
'do' => [
[
'cells' => [
[
'foreach' => 'indexList as index',
'do' => [
[
'foreach' => 'index as letter',
'do' => [
'{{ letter }}'
]
]
],
],
'{{ i }}',
'{{ bar }}',
],
],
],
],
],
],
],
],
],
];

$mappingReader = $this->getMockForAbstractClass('Xport\MappingReader\MappingReader');
$mappingReader->expects($this->once())->method('getMapping')->will($this->returnValue($mapping));

$modelBuilder = new SpreadsheetModelBuilder();
$modelBuilder->bind('headerList', [['A', 'B'], ['C', 'D', 'E']]);
$modelBuilder->bind('indexList', [['1', '2'], ['3', '4', '5']]);
$modelBuilder->bind('foo', ['test1', 'test2']);
$result = $modelBuilder->build($mappingReader);

$this->assertTrue($result instanceof Document);
$this->assertCount(1, $result->getSheets());

$sheet = $result->getSheets()[0];
$this->assertCount(1, $sheet->getTables());

// Table
$table = $sheet->getTables()[0];
$this->assertCount(2, $table->getLines());
$this->assertNull($table->getLabel());

// Columns
$this->assertCount(7, $table->getColumns());
$this->assertEquals('A', $table->getColumns()[0]->getLabel());
$this->assertEquals('B', $table->getColumns()[1]->getLabel());
$this->assertEquals('C', $table->getColumns()[2]->getLabel());
$this->assertEquals('D', $table->getColumns()[3]->getLabel());
$this->assertEquals('E', $table->getColumns()[4]->getLabel());
$this->assertEquals('Col1', $table->getColumns()[5]->getLabel());
$this->assertEquals('Col2', $table->getColumns()[6]->getLabel());

// Cells
$this->assertCount(14, $table->getCells());
$this->assertEquals('1', $table->getCell($table->getLines()[0], $table->getColumns()[0])->getContent());
$this->assertEquals('2', $table->getCell($table->getLines()[0], $table->getColumns()[1])->getContent());
$this->assertEquals('3', $table->getCell($table->getLines()[0], $table->getColumns()[2])->getContent());
$this->assertEquals('4', $table->getCell($table->getLines()[0], $table->getColumns()[3])->getContent());
$this->assertEquals('5', $table->getCell($table->getLines()[0], $table->getColumns()[4])->getContent());
$this->assertEquals('0', $table->getCell($table->getLines()[0], $table->getColumns()[5])->getContent());
$this->assertEquals('test1', $table->getCell($table->getLines()[0], $table->getColumns()[6])->getContent());
$this->assertEquals('1', $table->getCell($table->getLines()[1], $table->getColumns()[0])->getContent());
$this->assertEquals('2', $table->getCell($table->getLines()[1], $table->getColumns()[1])->getContent());
$this->assertEquals('3', $table->getCell($table->getLines()[1], $table->getColumns()[2])->getContent());
$this->assertEquals('4', $table->getCell($table->getLines()[1], $table->getColumns()[3])->getContent());
$this->assertEquals('5', $table->getCell($table->getLines()[1], $table->getColumns()[4])->getContent());
$this->assertEquals('1', $table->getCell($table->getLines()[1], $table->getColumns()[5])->getContent());
$this->assertEquals('test2', $table->getCell($table->getLines()[1], $table->getColumns()[6])->getContent());
}

public function testComplexForeachStructure()
{
/** @var Document $result */
Expand Down

0 comments on commit 9572972

Please sign in to comment.