Skip to content

Commit

Permalink
block basics
Browse files Browse the repository at this point in the history
  • Loading branch information
tyz910 committed Mar 19, 2015
1 parent 39b1512 commit 2505eb8
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 3 deletions.
Binary file added examples/docs/original/02-blocks.docx
Binary file not shown.
Binary file added examples/docs/processed/02-blocks.docx
Binary file not shown.
14 changes: 14 additions & 0 deletions examples/scripts/02-blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
require "bootstrap.php";

use DocxTemplate\TemplateFactory;

$doc = new ExampleDoc("02-blocks.docx");
$template = TemplateFactory::load($doc->getOriginalPath());

$block = $template->extractBlock("block");
$template->assign([
"block_placement" => $block
]);

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

class DocBlock implements UnescapedValueInterface
{
/**
* @var string
*/
private $content;

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

/**
* @return string
*/
public function getUnescapedValue()
{
return $this->content;
}
}
26 changes: 26 additions & 0 deletions src/DocxTemplate/Content/UnescapedValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace DocxTemplate\Content;

class UnescapedValue implements UnescapedValueInterface
{
/**
* @var string
*/
private $value;

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

/**
* @return string
*/
public function getUnescapedValue()
{
return $this->value;
}
}
10 changes: 10 additions & 0 deletions src/DocxTemplate/Content/UnescapedValueInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace DocxTemplate\Content;

interface UnescapedValueInterface
{
/**
* @return string
*/
public function getUnescapedValue();
}
27 changes: 24 additions & 3 deletions src/DocxTemplate/Matcher.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace DocxTemplate;

use DocxTemplate\Content\UnescapedValueInterface;

class Matcher
{
const MARK_DEFAULT_PATTERN = '{{%s}}'; // how to display mark in template (printf format, where %s - mark name)
Expand All @@ -12,11 +14,17 @@ class Matcher
*/
private $replaceRegexp;

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

/**
* @param string $markPattern
*/
public function __construct($markPattern = self::MARK_DEFAULT_PATTERN)
{
$this->markPattern = $markPattern;
$this->replaceRegexp = $this->convertPattern($markPattern);
}

Expand Down Expand Up @@ -44,15 +52,19 @@ private function convertPattern($pattern)

/**
* @param string $key
* @param string $value
* @param string|UnescapedValueInterface $value
* @param string $text
* @return string
*/
public function replaceMark($key, $value, $text)
{
$pattern = sprintf($this->replaceRegexp, preg_quote($key));
$value = htmlspecialchars($value, ENT_QUOTES | ENT_XML1, 'UTF-8');
if ($value instanceof UnescapedValueInterface) {
$value = $value->getUnescapedValue();
} else {
$value = htmlspecialchars($value, ENT_QUOTES | ENT_XML1, 'UTF-8');
}

$pattern = sprintf($this->replaceRegexp, preg_quote($key));
$replace = function ($matches) use ($value) {
$space = $matches[1] == ' ' ? '&#160;' : ''; /** save space @see convertPattern */

Expand Down Expand Up @@ -87,4 +99,13 @@ public function getMarks($text)

return array_unique($matches['mark']);
}

/**
* @param string $name
* @return string
*/
public function toMark($name)
{
return sprintf($this->markPattern, $name);
}
}
49 changes: 49 additions & 0 deletions src/DocxTemplate/Template.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace DocxTemplate;

use DocxTemplate\Content\DocBlock;

class Template
{
/**
Expand Down Expand Up @@ -72,6 +74,53 @@ public function assign($key, $value = null)
return $this;
}

/**
* @param string|string[] $key
* @param bool|string $placeMark
* @return DocBlock
*/
public function extractBlock($key, $placeMark = true)
{
if (is_array($key)) {
$blockStart = $key[0];
$blockEnd = $key[1];
} else {
$blockStart = $key . "_start";
$blockEnd = $key . "_end";
}

if (is_bool($placeMark) && $placeMark) {
$placeMark = is_string($key) ? $key : $blockStart;
}

$uniqId = uniqid();
$this->assign([
$blockStart => "BLOCK_OPEN" . $uniqId,
$blockEnd => "BLOCK_CLOSE" . $uniqId
]);

$pattern = "/BLOCK_OPEN{$uniqId}(.*)BLOCK_CLOSE{$uniqId}/";
$blockContent = "";
$content = preg_replace_callback($pattern, function ($matches) use (&$blockContent, $placeMark) {
if (isset($matches[1])) {
$blockContent = $matches[1];
}

if ($placeMark) {
return $this->matcher->toMark($placeMark);
} else {
return "";
}

}, $this->doc->getContent());

$this->doc->setContent($content);

// todo clean $blockContent

return new DocBlock($blockContent);
}

/**
* @param string $key
* @param string $value
Expand Down

0 comments on commit 2505eb8

Please sign in to comment.