Skip to content

Commit 9572972

Browse files
Correction d'un problème de double foreach et d'indexation des lignes/cells
1 parent edfe2ad commit 9572972

File tree

5 files changed

+119
-10
lines changed

5 files changed

+119
-10
lines changed

src/Xport/Spreadsheet/Builder/Content/HorizontalTableModelBuilder.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ public function build(Sheet $sheet, $yamlContent, Scope $scope)
5555
protected function parseLine(Table $table, $yamlLine, Scope $scope)
5656
{
5757
if (is_array($yamlLine) && array_key_exists('foreach', $yamlLine)) {
58-
$this->parseForeach($yamlLine, $scope, [$this, 'parseLine'], [$table]);
58+
return $this->parseForeach($yamlLine, $scope, [$this, 'parseLine'], [$table]);
5959
} else {
6060
$this->createLine($table, $yamlLine, $scope);
61+
return 1;
6162
}
6263
}
6364

@@ -79,9 +80,10 @@ protected function createLine(Table $table, $yamlLine, Scope $scope)
7980
protected function parseColumn(Table $table, $yamlColumn, Scope $scope)
8081
{
8182
if (is_array($yamlColumn) && array_key_exists('foreach', $yamlColumn)) {
82-
$this->parseForeach($yamlColumn, $scope, [$this, 'parseColumn'], [$table]);
83+
return $this->parseForeach($yamlColumn, $scope, [$this, 'parseColumn'], [$table]);
8384
} else {
8485
$this->createColumn($table, $yamlColumn, $scope);
86+
return 1;
8587
}
8688
}
8789

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

107109
if (is_array($yamlCell) && array_key_exists('foreach', $yamlCell)) {
108-
return $this->parseForeach($yamlCell, $scope, [$this, 'parseCell'], [$table, $column, $lineIndex]);
110+
return $this->parseForeach($yamlCell, $scope, [$this, 'parseCell'], [$table, $column, ($lineIndex + $lineIteration)]);
109111
} else {
110112
$this->createCell($table, $column, $lines[$lineIndex + $lineIteration], $yamlCell, $scope);
111113
return 1;

src/Xport/Spreadsheet/Builder/Content/VerticalTableModelBuilder.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ public function build(Sheet $sheet, $yamlContent, Scope $scope)
5555
protected function parseColumn(Table $table, $yamlColumn, Scope $scope)
5656
{
5757
if (is_array($yamlColumn) && array_key_exists('foreach', $yamlColumn)) {
58-
$this->parseForeach($yamlColumn, $scope, [$this, 'parseColumn'], [$table]);
58+
return $this->parseForeach($yamlColumn, $scope, [$this, 'parseColumn'], [$table]);
5959
} else {
6060
$this->createColumn($table, $yamlColumn, $scope);
61+
return 1;
6162
}
6263
}
6364

@@ -79,9 +80,10 @@ protected function createColumn(Table $table, $yamlColumn, Scope $scope)
7980
protected function parseLine(Table $table, $yamlLine, Scope $scope)
8081
{
8182
if (is_array($yamlLine) && array_key_exists('foreach', $yamlLine)) {
82-
$this->parseForeach($yamlLine, $scope, [$this, 'parseLine'], [$table]);
83+
return $this->parseForeach($yamlLine, $scope, [$this, 'parseLine'], [$table]);
8384
} else {
8485
$this->createLine($table, $yamlLine, $scope);
86+
return 1;
8587
}
8688
}
8789

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

107109
if (is_array($yamlCell) && array_key_exists('foreach', $yamlCell)) {
108-
return $this->parseForeach($yamlCell, $scope, [$this, 'parseCell'], [$table, $line, $columnIndex]);
110+
return $this->parseForeach($yamlCell, $scope, [$this, 'parseCell'], [$table, $line, ($columnIndex + $columnIteration)]);
109111
} else {
110112
$this->createCell($table, $line, $columns[$columnIndex + $columnIteration], $yamlCell, $scope);
111113
return 1;

src/Xport/Spreadsheet/Builder/ModelBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ protected function doLoop($yamlLoop, array $scopes, callable $callback, array $p
6161
foreach ($scopes as $scope) {
6262
// Traverse all sub-elements.
6363
foreach ($yamlLoop['do'] as $yamlElement) {
64-
call_user_func_array($callback, array_merge($parameters, [$yamlElement, $scope, $iterationCallback]));
65-
$iterationCallback ++;
64+
$iterationCallback += call_user_func_array(
65+
$callback,
66+
array_merge($parameters, [$yamlElement, $scope, $iterationCallback])
67+
);
6668
}
6769
}
6870

src/Xport/Spreadsheet/Builder/SpreadsheetModelBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ protected function parseRoot(Document $document, $yamlRoot, Scope $scope)
119119
protected function parseSheet(Document $document, $yamlSheet, Scope $scope)
120120
{
121121
if (array_key_exists('foreach', $yamlSheet)) {
122-
$this->parseForeach($yamlSheet, $scope, [$this, 'parseSheet'], [$document]);
122+
return $this->parseForeach($yamlSheet, $scope, [$this, 'parseSheet'], [$document]);
123123
} else {
124124
$this->createSheet($document, $yamlSheet, $scope);
125+
return 1;
125126
}
126127
}
127128

@@ -145,9 +146,10 @@ protected function createSheet(Document $document, $yamlSheet, Scope $scope)
145146
protected function parseContent(Sheet $sheet, $yamlContent, Scope $scope)
146147
{
147148
if (array_key_exists('foreach', $yamlContent)) {
148-
$this->parseForeach($yamlContent, $scope, [$this, 'parseContent'], [$sheet]);
149+
return $this->parseForeach($yamlContent, $scope, [$this, 'parseContent'], [$sheet]);
149150
} else {
150151
$this->createContent($sheet, $yamlContent, $scope);
152+
return 1;
151153
}
152154
}
153155

tests/XportTest/Builder/SpreadsheetModelBuilderTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,107 @@ public function testContentForEachForEach()
352352
$this->assertEquals($sheet->getTables()[0], $sheet->getTables()[2]);
353353
}
354354

355+
public function testCellsForEachForEach()
356+
{
357+
$mapping = [
358+
'sheets' => [
359+
[
360+
'content' => [
361+
[
362+
'type' => 'VerticalTable',
363+
'columns' => [
364+
[
365+
'foreach' => 'headerList as header',
366+
'do' => [
367+
[
368+
'foreach' => 'header as letter',
369+
'do' => [
370+
'{{ letter }}'
371+
]
372+
]
373+
]
374+
],
375+
'Col1',
376+
'Col2',
377+
],
378+
'lines' => [
379+
[
380+
'foreach' => 'foo as i => bar',
381+
'do' => [
382+
[
383+
'cells' => [
384+
[
385+
'foreach' => 'indexList as index',
386+
'do' => [
387+
[
388+
'foreach' => 'index as letter',
389+
'do' => [
390+
'{{ letter }}'
391+
]
392+
]
393+
],
394+
],
395+
'{{ i }}',
396+
'{{ bar }}',
397+
],
398+
],
399+
],
400+
],
401+
],
402+
],
403+
],
404+
],
405+
],
406+
];
407+
408+
$mappingReader = $this->getMockForAbstractClass('Xport\MappingReader\MappingReader');
409+
$mappingReader->expects($this->once())->method('getMapping')->will($this->returnValue($mapping));
410+
411+
$modelBuilder = new SpreadsheetModelBuilder();
412+
$modelBuilder->bind('headerList', [['A', 'B'], ['C', 'D', 'E']]);
413+
$modelBuilder->bind('indexList', [['1', '2'], ['3', '4', '5']]);
414+
$modelBuilder->bind('foo', ['test1', 'test2']);
415+
$result = $modelBuilder->build($mappingReader);
416+
417+
$this->assertTrue($result instanceof Document);
418+
$this->assertCount(1, $result->getSheets());
419+
420+
$sheet = $result->getSheets()[0];
421+
$this->assertCount(1, $sheet->getTables());
422+
423+
// Table
424+
$table = $sheet->getTables()[0];
425+
$this->assertCount(2, $table->getLines());
426+
$this->assertNull($table->getLabel());
427+
428+
// Columns
429+
$this->assertCount(7, $table->getColumns());
430+
$this->assertEquals('A', $table->getColumns()[0]->getLabel());
431+
$this->assertEquals('B', $table->getColumns()[1]->getLabel());
432+
$this->assertEquals('C', $table->getColumns()[2]->getLabel());
433+
$this->assertEquals('D', $table->getColumns()[3]->getLabel());
434+
$this->assertEquals('E', $table->getColumns()[4]->getLabel());
435+
$this->assertEquals('Col1', $table->getColumns()[5]->getLabel());
436+
$this->assertEquals('Col2', $table->getColumns()[6]->getLabel());
437+
438+
// Cells
439+
$this->assertCount(14, $table->getCells());
440+
$this->assertEquals('1', $table->getCell($table->getLines()[0], $table->getColumns()[0])->getContent());
441+
$this->assertEquals('2', $table->getCell($table->getLines()[0], $table->getColumns()[1])->getContent());
442+
$this->assertEquals('3', $table->getCell($table->getLines()[0], $table->getColumns()[2])->getContent());
443+
$this->assertEquals('4', $table->getCell($table->getLines()[0], $table->getColumns()[3])->getContent());
444+
$this->assertEquals('5', $table->getCell($table->getLines()[0], $table->getColumns()[4])->getContent());
445+
$this->assertEquals('0', $table->getCell($table->getLines()[0], $table->getColumns()[5])->getContent());
446+
$this->assertEquals('test1', $table->getCell($table->getLines()[0], $table->getColumns()[6])->getContent());
447+
$this->assertEquals('1', $table->getCell($table->getLines()[1], $table->getColumns()[0])->getContent());
448+
$this->assertEquals('2', $table->getCell($table->getLines()[1], $table->getColumns()[1])->getContent());
449+
$this->assertEquals('3', $table->getCell($table->getLines()[1], $table->getColumns()[2])->getContent());
450+
$this->assertEquals('4', $table->getCell($table->getLines()[1], $table->getColumns()[3])->getContent());
451+
$this->assertEquals('5', $table->getCell($table->getLines()[1], $table->getColumns()[4])->getContent());
452+
$this->assertEquals('1', $table->getCell($table->getLines()[1], $table->getColumns()[5])->getContent());
453+
$this->assertEquals('test2', $table->getCell($table->getLines()[1], $table->getColumns()[6])->getContent());
454+
}
455+
355456
public function testComplexForeachStructure()
356457
{
357458
/** @var Document $result */

0 commit comments

Comments
 (0)