Skip to content

Commit

Permalink
Add loop functionality and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aoliverwd committed Nov 20, 2022
1 parent faf1145 commit 54cbbdd
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/brace.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,29 @@ private function processLine(string $this_line, array $dataset, bool $render): v
}

/** Process if condition or each block */
if (!$this->is_block && preg_match_all('/{{if (.*?)}}|{{each (.*?)}}/i', $this_line, $matches, PREG_SET_ORDER)) {
if (!$this->is_block && preg_match_all('/{{if (.*?)}}|{{each (.*?)}}|{{loop (.*?)}}/i', $this_line, $matches, PREG_SET_ORDER)) {

/** Set block variables */
$this->block_condition = $matches[0];

/** Set condition type */
$condition_type = $this->block_condition[0];
$this->block_condition[] = (preg_match('/{{if/', $condition_type) ? 'if' : (preg_match('/{{each/', $condition_type) ? 'each' : ''));

$this->block_spaces = strpos($this_line, '{{' . (isset($this->block_condition[3]) ? $this->block_condition[3] : $this->block_condition[2]));
preg_match('/{{(.*?) /', $condition_type, $match_types);
$block_type = '';

if (isset($match_types[1])) {
switch ($match_types[1]) {
case 'if':
case 'each':
case 'loop':
$block_type = $match_types[1];
break;
}
}

$this->block_condition[] = $block_type;
$this->block_spaces = strpos($this_line, '{{' . $block_type);
$this->is_block = true;

/** Blank line */
Expand Down Expand Up @@ -353,10 +366,11 @@ private function processBlock(string $block_string, array $conditions, array $da
$process_content = '';

/** Set is If or Each statement */
$if_or_each = (isset($conditions[3]) ? $conditions[3] : (isset($conditions[2]) ? $conditions[2] : false));
$block_type = end($conditions);
// $if_or_each = (isset($conditions[3]) ? $conditions[3] : (isset($conditions[2]) ? $conditions[2] : false));

if ($if_or_each) {
switch ($if_or_each) {
if ($block_type) {
switch ($block_type) {
case 'if':
/** new core parser class instance */
$processBlock = new Parser();
Expand Down Expand Up @@ -396,12 +410,55 @@ private function processBlock(string $block_string, array $conditions, array $da
case 'each':
$process_content = $this->processEachStatement($conditions[2], $block_string, $dataset);
break;
case 'loop':
$process_content = $this->processLoop($conditions[3], $block_string);
break;
}
}

return $process_content;
}

/**
* Process loop
* @param string $loop_statement
* @param string $block_content
* @return string
*/
private function processLoop(string $loop_statement, string $block_content): string
{
$loop_components = explode(' ', trim($loop_statement));
$return_string = '';

if (count($loop_components) === 3 && $loop_components[1] === 'to') {
$from = intval($loop_components[0]);
$to = intval($loop_components[2]);

/** new core parser class instance */
$process_each_block = new Parser();
$process_each_block->template_path = $this->template_path;

if ($from < $to) {
for ($i = $from; $i <= $to; $i += 1) {
$process_each_block->parseInputString($block_content, [
'_KEY' => $i
], false);
$return_string .= $process_each_block->return();
$process_each_block->export_string = '';
}
} else {
for ($i = $from; $i >= $to; $i -= 1) {
$process_each_block->parseInputString($block_content, [
'_KEY' => $i
], false);
$return_string .= $process_each_block->return();
$process_each_block->export_string = '';
}
}
}

return $return_string;
}

/**
* Process each statement
Expand Down
2 changes: 2 additions & 0 deletions tests/logs/testdox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Iterators (ConditionTests\Iterators)
[x] Iterator key value
[x] Iterator key value type two
[x] Inline iterator key value
[x] Loop iteration ascending
[x] Loop iteration descending

Shortcode (ConditionTests\Shortcode)
[x] Shortcode
Expand Down
3 changes: 3 additions & 0 deletions tests/tests/iterators/iteration-loop-ascending.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{loop 1 to 3}}
<li>{{_KEY}}</li>
{{end}}
3 changes: 3 additions & 0 deletions tests/tests/iterators/iteration-loop-descending.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{loop 3 to 1}}
<li>{{_KEY}}</li>
{{end}}
26 changes: 26 additions & 0 deletions tests/tests/iterators/iteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,30 @@ public function testInlineIteratorKeyValue(): void
], false)->return()
);
}

public function testLoopIterationAscending(): void
{
$brace = new Brace\Parser();
$brace->template_path = __DIR__ . '/';

$this->assertEquals(
"<li>1</li>\n" .
"<li>2</li>\n" .
"<li>3</li>\n",
$brace->parseInputString('[@include iteration-loop-ascending]', [], false)->return()
);
}

public function testLoopIterationDescending(): void
{
$brace = new Brace\Parser();
$brace->template_path = __DIR__ . '/';

$this->assertEquals(
"<li>3</li>\n" .
"<li>2</li>\n" .
"<li>1</li>\n",
$brace->parseInputString('[@include iteration-loop-descending]', [], false)->return()
);
}
}

0 comments on commit 54cbbdd

Please sign in to comment.