Skip to content

Commit

Permalink
new contents concept + loop
Browse files Browse the repository at this point in the history
  • Loading branch information
tyz910 committed Mar 20, 2015
1 parent 2505eb8 commit 23c8f64
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 181 deletions.
Binary file modified examples/docs/processed/01-simple.docx
Binary file not shown.
Binary file modified examples/docs/processed/02-blocks.docx
Binary file not shown.
15 changes: 12 additions & 3 deletions examples/scripts/02-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
$doc = new ExampleDoc("02-blocks.docx");
$template = TemplateFactory::load($doc->getOriginalPath());

$block = $template->extractBlock("block");
$template->assign([
"block_placement" => $block
$template->loop("block", [
[
'block_var' => "var1"
],

[
'block_var' => "var2"
],

[
'block_var' => "var3"
]
]);

$template->save($doc->getProcessedPath());
47 changes: 47 additions & 0 deletions src/DocxTemplate/Content/Collection/ContentCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace DocxTemplate\Content\Collection;

use DocxTemplate\Content\ContentInterface;

class ContentCollection implements ContentInterface
{
/**
* @var ContentInterface[]
*/
private $contents = [];

/**
* @param ContentInterface $content
* @param bool $clone
* @return $this
*/
public function addContent(ContentInterface $content, $clone = false)
{
if ($clone) {
$this->contents[] = clone $content;
} else {
$this->contents[] = $content;
}

return $this;
}

/**
* @return $this
*/
public function clearContents()
{
$this->contents = [];
return $this;
}

/**
* @return string
*/
public function getContent()
{
return implode('', array_map(function (ContentInterface $content) {
return $content->getContent();
}, $this->contents));
}
}
70 changes: 70 additions & 0 deletions src/DocxTemplate/Content/Collection/LoopCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace DocxTemplate\Content\Collection;

use DocxTemplate\Content\MarkedContent;

class LoopCollection extends ContentCollection
{
/**
* @var MarkedContent
*/
private $baseContent;

/**
* @var MarkedContent
*/
private $currentItem;

/**
* @param MarkedContent $content
*/
public function __construct(MarkedContent $content)
{
$this->baseContent = $content;
}

/**
* @return MarkedContent
*/
public function getBaseContent()
{
return $this->baseContent;
}

/**
* @return MarkedContent
*/
public function getCurrentItem()
{
return $this->currentItem;
}

/**
* @return MarkedContent
*/
public function itemStart()
{
if (!$this->currentItem) {
$this->currentItem = clone $this->baseContent;
}

return $this->currentItem;
}

public function itemEnd()
{
if ($this->currentItem) {
$this->addContent($this->currentItem);
$this->currentItem = null;
}
}

public function finish()
{
$this->baseContent->getBindedTo()->assign([
$this->baseContent->getBindedMark() => $this
]);

$this->clearContents();
}
}
70 changes: 70 additions & 0 deletions src/DocxTemplate/Content/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace DocxTemplate\Content;

class Content implements ContentInterface
{
/**
* @var string
*/
private $content;

/**
* @var MarkedContent
*/
private $bindedTo;

/**
* @var string
*/
private $bindedMark;

/**
* @param string $content
*/
public function __construct($content)
{
$this->setContent($content);
}

/**
* @return string
*/
public function getContent()
{
return $this->content;
}

/**
* @param string $content
*/
public function setContent($content)
{
$this->content = $content;
}

/**
* @param string $mark
* @param MarkedContent $content
*/
public function bindTo($mark, MarkedContent $content)
{
$this->bindedMark = $mark;
$this->bindedTo = $content;
}

/**
* @return MarkedContent
*/
public function getBindedTo()
{
return $this->bindedTo;
}

/**
* @return string
*/
public function getBindedMark()
{
return $this->bindedMark;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
namespace DocxTemplate\Content;

interface UnescapedValueInterface
interface ContentInterface
{
/**
* @return string
*/
public function getUnescapedValue();
}
public function getContent();
}
26 changes: 0 additions & 26 deletions src/DocxTemplate/Content/DocBlock.php

This file was deleted.

75 changes: 75 additions & 0 deletions src/DocxTemplate/Content/MarkedContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace DocxTemplate\Content;

use DocxTemplate\Matcher;

class MarkedContent extends Content
{
/**
* @var Matcher
*/
private $matcher;

/**
* @param string $content
* @param Matcher $matcher
*/
public function __construct($content, Matcher $matcher)
{
parent::__construct($content);
$this->matcher = $matcher;
}

/**
* @param string|string[] $key
* @param string|null $value
* @return $this
*/
public function assign($key, $value = null)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->assignVar($k, $v);
}
} else {
$this->assignVar($key, $value);
}

return $this;
}

/**
* @param string $name
* @return MarkedContent
*/
public function extractContent($name)
{
$content = $this->getContent();
$uniqName = uniqid($name);
$extracted = $this->matcher->extractRange($name . "_start", $name . "_end", $uniqName, $content);
$this->setContent($content);

$content = new MarkedContent($extracted, $this->matcher);
$content->bindTo($uniqName, $this);
return $content;
}

/**
* @return string[]
*/
public function getMarks()
{
return $this->matcher->getMarks($this->getContent());
}

/**
* @param string $key
* @param string $value
*/
protected function assignVar($key, $value)
{
$this->setContent(
$this->matcher->replaceMark($key, $value, $this->getContent())
);
}
}
26 changes: 0 additions & 26 deletions src/DocxTemplate/Content/UnescapedValue.php

This file was deleted.

Loading

0 comments on commit 23c8f64

Please sign in to comment.