Skip to content

Commit

Permalink
Merge pull request #22 from aoliverwd/release/v1.0.13
Browse files Browse the repository at this point in the history
Release Version 1.0.13
  • Loading branch information
aoliverwd authored Nov 22, 2022
2 parents 099d088 + 54cbbdd commit 569f75d
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 20 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
- Shortcodes
- PHP Implementation Example
- Content Template
- Array Counting
- Display Count:
- Check Count
- Comment Blocks
- In-line Comment Block
- Multiple Line Comment Block
Expand Down Expand Up @@ -520,6 +523,32 @@ Name is "John"
[button title="Hello world" url="https://hello.world" alt="Hello world button" target="_blank"]
```

## Array Counting

Ability to check and display array item counts

### Display Count:

```html
<p>Total items is: {{COUNT(items)}}</p>
```

```txt
Total items is: 3
```

### Check Count

```html
{{if COUNT(items) == 3}}
<p>There are three items</p>
{{end}}
```

```txt
There are three items
```

## Comment Blocks

### In-line Comment Block
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
]
},
"require-dev": {
"phpstan/phpstan": "^0.12.82",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ parameters:
- src
ignoreErrors:
# The below constants are defined when running the application from a web server
- '#Function do_shortcode not found#' # WordPress shortcode
# - '#Function do_shortcode not found#' # WordPress shortcode
# - '#return_chained_variables#' # Return mixed
82 changes: 73 additions & 9 deletions src/brace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

/**
* Brace
* Copyright (C) 2021 Alex Oliver
* Copyright (C) 2022 Alex Oliver
*
* @version: 1.0.10
* @version: 1.0.13
* @author: Alex Oliver
* @Repo: https://github.com/aoliverwd/brace
*/
Expand Down 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 Expand Up @@ -646,11 +703,18 @@ private function processString(string $input_string, array $dataset): string
private function returnChainedVariables(string $string, array $dataset)
{
$return = [];
$is_count = false;

// Check for count
if (preg_match('/^COUNT\((.*?)\)/', $string, $match)) {
$string = isset($match[1]) ? $match[1] : $string;
$is_count = true;
}

foreach (explode('->', $string) as $thisVar) {
if (is_array($dataset) && isset($dataset[$thisVar])) {
$dataset = $dataset[$thisVar];
$return = $dataset;
$return = $is_count ? count($dataset) : $dataset;
} else {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/logs/testdox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ Conditions (ConditionTests\Conditions)
[x] More than or equal to include variable
[x] Less than or equal to
[x] More than
[x] More than nested data attribute
[x] Less than
[x] Is not
[x] Is not equal to
[x] Exists
[x] Not exists
[x] Count

Iterators (ConditionTests\Iterators)
[x] Nested iteration
Expand All @@ -52,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
48 changes: 39 additions & 9 deletions tests/tests/conditions/conditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ConditionsTest extends TestCase

/**
* [testEquals description]
* @return [type] [description]
* @return void
*/
public function testEquals(): void
{
Expand All @@ -35,7 +35,7 @@ public function testEquals(): void

/**
* [testMoreThanOrEqualTo description]
* @return [type] [description]
* @return void
*/
public function testMoreThanOrEqualTo(): void
{
Expand All @@ -48,7 +48,7 @@ public function testMoreThanOrEqualTo(): void

/**
* [testMoreThanOrEqualToIncludeVariable description]
* @return [type] [description]
* @return void
*/
public function testMoreThanOrEqualToIncludeVariable(): void
{
Expand All @@ -61,7 +61,7 @@ public function testMoreThanOrEqualToIncludeVariable(): void

/**
* [testLessThanOrEqualTo description]
* @return [type] [description]
* @return void
*/
public function testLessThanOrEqualTo(): void
{
Expand All @@ -74,7 +74,7 @@ public function testLessThanOrEqualTo(): void

/**
* [testMoreThan description]
* @return [type] [description]
* @return void
*/
public function testMoreThan(): void
{
Expand All @@ -85,9 +85,27 @@ public function testMoreThan(): void
);
}

/**
* [testMoreThanNestedDataAttribute description]
* @return void
*/
public function testMoreThanNestedDataAttribute(): void
{
$brace = new Brace\Parser();
$this->assertEquals(
"21\n",
$brace->parseInputString('{{age->current > age->required ? "__age->current__"}}', [
'age' => [
'current' => 21,
'required' => 18
]
], false)->return()
);
}

/**
* [testLessThan description]
* @return [type] [description]
* @return void
*/
public function testLessThan(): void
{
Expand All @@ -98,6 +116,7 @@ public function testLessThan(): void
);
}


public function testIsNot(): void
{
$brace = new Brace\Parser();
Expand All @@ -109,7 +128,7 @@ public function testIsNot(): void

/**
* [testIsNotEqualTo description]
* @return [type] [description]
* @return void
*/
public function testIsNotEqualTo(): void
{
Expand All @@ -122,7 +141,7 @@ public function testIsNotEqualTo(): void

/**
* [testExists description]
* @return [type] [description]
* @return void
*/
public function testExists(): void
{
Expand All @@ -135,7 +154,7 @@ public function testExists(): void

/**
* [testNotExists description]
* @return [type] [description]
* @return void
*/
public function testNotExists(): void
{
Expand All @@ -145,4 +164,15 @@ public function testNotExists(): void
$brace->parseInputString('{{age !EXISTS ? "No age"}}', [], false)->return()
);
}

public function testCount(): void
{
$brace = new Brace\Parser();
$this->assertEquals(
"3\n",
$brace->parseInputString('{{COUNT(items) == 3 ? "__COUNT(items)__"}}', [
'items' => [1,2,3]
], false)->return()
);
}
}
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 569f75d

Please sign in to comment.