Skip to content

Commit

Permalink
Added: Forelse Directive
Browse files Browse the repository at this point in the history
  • Loading branch information
soysudhanshu committed Aug 15, 2024
1 parent db58991 commit 2deb063
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Template inheritance allows you to create layouts by defining a master template
| `@for` | For loop ||
| `@foreach` | Foreach loop ||
| `@while` | While loop ||
| `@forelse` | Forelse loop | |
| `@forelse` | Forelse loop | |
| `$loop` | Loop variable in the for loop (basic) ||

### Conditional Class & Styles
Expand Down
32 changes: 32 additions & 0 deletions src/CompileAtRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

class CompileAtRules
{
const FORELSE_CLOSED = 0;
const FORELSE_OPEN = 1;
const FORELSE_OPEN_EMPTY_BLOCK = 2;
use CompileForeachTrait;

protected bool $switchOpen = false;
protected bool $switchFirstCaseClosed = false;
protected bool $usesTemplateInheritance = false;
protected int $forelseStatus = self::FORELSE_CLOSED;

public function __construct(protected string $content) {}

Expand Down Expand Up @@ -194,6 +198,12 @@ protected function compileEndIsset(string $expression): string

protected function compileEmpty(string $expression): string
{
if ($this->forelseStatus === self::FORELSE_OPEN) {
$this->forelseStatus = self::FORELSE_OPEN_EMPTY_BLOCK;
return '<?php endforeach; ?>' .
'<?php if(!$__forelse_looped): ?>';
}

return "<?php if(empty{$expression}): ?>";
}

Expand Down Expand Up @@ -421,4 +431,26 @@ public function compileEach(string $expression): string
{
return '<?php $template_renderer->renderEach' . $expression . '; ?>';
}

public function compileForelse(string $expression): string
{
$this->forelseStatus = self::FORELSE_OPEN;

return '<?php $__forelse_looped = false; ?>' .
"<?php foreach{$expression}: ?>" .
'<?php $__forelse_looped = true; ?>';
}

public function compileEndforelse(string $expression): string
{
$output = '<?php endforeach; ?>';

if ($this->forelseStatus === self::FORELSE_OPEN_EMPTY_BLOCK) {
$output = "<?php endif; ?>";
}

$this->forelseStatus = self::FORELSE_CLOSED;

return $output;
}
}
30 changes: 30 additions & 0 deletions tests/ForelseDirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase;

class ForelseDirectiveTest extends TestCase
{
use VerifiesOutputTrait;

public function testWorksLikeForeach()
{
$this->assertSame(
'0:1 1:2 2:3 ',
$this->renderBlade(
'@forelse([1, 2, 3] as $key => $item){{ $key }}:{{ $item }} @endforelse',
)
);
}

public function testRendersEmptyOutput()
{
$this->assertSame(
'some empty content ',
$this->renderBlade(
'@forelse([] as $key => $item) not content @empty some empty content @endforelse',
)
);
}
}

0 comments on commit 2deb063

Please sign in to comment.