Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Traversable objects in for loops #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/TextTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,15 @@ private function _removeWhitespace ($input) {
}


private function _getValueByName ($context, $name, $softFail) {
private function _getRawValueByName ($context, $name, $softFail) {
$dd = explode (".", $name);
$value = $context;
$cur = "";
foreach ($dd as $cur) {
if (is_numeric($cur))
$cur = (int)$cur;
if (is_array($value)) {
if ( ! array_key_exists($cur, $value)) {
if (is_array($value) || $value instanceof \ArrayAccess) {
if ( ! array_key_exists($cur, $value) && ! isset($value[$cur])) {
if ( ! $softFail) {
throw new UndefinedVariableException("ParsingError: Can't parse element: '{$name}' Error on subelement: '$cur'", $name);
}
Expand Down Expand Up @@ -338,6 +338,14 @@ private function _getValueByName ($context, $name, $softFail) {
}
}
}

return $value;
}


private function _getValueByName ($context, $name, $softFail) {
$value = $this->_getRawValueByName($context, $name, $softFail);

if (is_object($value) && ! method_exists($value, "__toString"))
$value = "##ERR:OBJECT_IN_TEXT:[{$name}]ON[{$cur}]:" . gettype($value) . "###";

Expand Down Expand Up @@ -412,10 +420,10 @@ private function _runFor (&$context, $content, $cmdParam, $softFail) {
$iterateOverName = $matches[2];
$localName = $matches[1];

$repeatVal = $this->_getValueByName($context, $iterateOverName, $softFail);
$repeatVal = $this->_getRawValueByName($context, $iterateOverName, $softFail);


if ( ! is_array($repeatVal))
if ( ! is_array($repeatVal) && ! $repeatVal instanceof \Traversable)
return "";
$index = 0;
$result = "";
Expand Down Expand Up @@ -806,4 +814,4 @@ public function apply ($params, $softFail=TRUE, &$context=[]) {
return $result;
}

}
}
5 changes: 4 additions & 1 deletion test/unit/tpls/04_simple_for/_in.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"arrVal1" => [
"a", "b", "c"
],
"traversableVal1" => new ArrayIterator([
"x", "y", "z"
]),
"value1" => "Some Value",
];
];
5 changes: 4 additions & 1 deletion test/unit/tpls/04_simple_for/_in.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
{for curVal in arrVal1}
2: curVal = {=curVal}
{/for}
5: Some other text
{for curVal in traversableVal1}
3: curVal = {=curVal}
{/for}
5: Some other text
5 changes: 4 additions & 1 deletion test/unit/tpls/04_simple_for/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
2: curVal = a
2: curVal = b
2: curVal = c
5: Some other text
3: curVal = x
3: curVal = y
3: curVal = z
5: Some other text