Skip to content

Commit

Permalink
loop refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tyz910 committed Mar 20, 2015
1 parent 23c8f64 commit cbcaa38
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
Binary file modified examples/docs/processed/02-blocks.docx
Binary file not shown.
13 changes: 13 additions & 0 deletions src/DocxTemplate/Content/Collection/LoopCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public function getCurrentItem()
return $this->currentItem;
}

/**
* @param array $row
* @return $this
*/
public function assignRow(array $row)
{
$item = $this->itemStart();
$item->assign($row);
$this->itemEnd();

return $this;
}

/**
* @return MarkedContent
*/
Expand Down
19 changes: 19 additions & 0 deletions src/DocxTemplate/Content/MarkedContent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace DocxTemplate\Content;

use DocxTemplate\Content\Collection\LoopCollection;
use DocxTemplate\Matcher;

class MarkedContent extends Content
Expand Down Expand Up @@ -54,6 +55,24 @@ public function extractContent($name)
return $content;
}

/**
* @param string $name
* @param string[][] $rows
* @return $this
*/
public function loop($name, $rows = [])
{
$block = $this->extractContent($name);
$loop = new LoopCollection($block);

foreach ($rows as $row) {
$loop->assignRow($row);
}

$loop->finish();
return $this;
}

/**
* @return string[]
*/
Expand Down
11 changes: 1 addition & 10 deletions src/DocxTemplate/Template.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace DocxTemplate;

use DocxTemplate\Content\Collection\LoopCollection;
use DocxTemplate\Content\MarkedContent;

class Template
Expand Down Expand Up @@ -70,16 +69,8 @@ public function assign($key, $value = null)
*/
public function loop($name, $rows = [])
{
$block = $this->docContent->extractContent($name);
$loop = new LoopCollection($block);
$this->docContent->loop($name, $rows);

foreach ($rows as $row) {
$item = $loop->itemStart();
$item->assign($row);
$loop->itemEnd();
}

$loop->finish();
return $this;
}
}

0 comments on commit cbcaa38

Please sign in to comment.