Skip to content

Commit

Permalink
Add ability to count arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
aoliverwd committed Nov 20, 2022
1 parent 31e0704 commit faf1145
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 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
13 changes: 10 additions & 3 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 @@ -646,11 +646,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
2 changes: 2 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 Down
11 changes: 11 additions & 0 deletions tests/tests/conditions/conditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,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()
);
}
}

0 comments on commit faf1145

Please sign in to comment.