forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tempestphp#323 from tempestphp/elseif-attribute
Elseif attribute
- Loading branch information
Showing
4 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
// draft | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\View\Attributes; | ||
|
||
use Exception; | ||
use Tempest\View\Attribute; | ||
use Tempest\View\Element; | ||
use Tempest\View\Elements\EmptyElement; | ||
use Tempest\View\Elements\GenericElement; | ||
|
||
final readonly class ElseIfAttribute implements Attribute | ||
{ | ||
public function apply(Element $element): Element | ||
{ | ||
if (! $element instanceof GenericElement) { | ||
throw new Exception("Invalid element with :elseif"); | ||
} | ||
|
||
$previous = $element->getPrevious(); | ||
$previousCondition = false; | ||
|
||
if (! $previous instanceof GenericElement) { | ||
throw new Exception("Invalid preceding element before :elseif"); | ||
} | ||
|
||
// Check all :elseif and :if conditions for previous elements | ||
// If one of the previous element's conditions is true, we'll stop. | ||
// We won't have to render this :elseif element | ||
while ( | ||
$previousCondition === false | ||
&& $previous instanceof GenericElement | ||
&& ($previous->hasAttribute('if') || $previous->hasAttribute('elseif')) | ||
) { | ||
$previousCondition = (bool) ($previous->getAttribute('if') ?? $previous->getAttribute('elseif')); | ||
$previous = $previous->getPrevious(); | ||
} | ||
|
||
$currentCondition = (bool) $element->getAttribute('elseif'); | ||
|
||
// For this element to render, the previous conditions need to be false, | ||
// and the current condition must be true | ||
if ($previousCondition === false && $currentCondition === true) { | ||
return $element; | ||
} | ||
|
||
return new EmptyElement(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters