-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
337 additions
and
181 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...plate/Content/UnescapedValueInterface.php → ...DocxTemplate/Content/ContentInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.