From e75c7ab6c0eafd98d628cfe7fd30a5172c9cd0d6 Mon Sep 17 00:00:00 2001 From: yassiNebeL Date: Tue, 6 Aug 2024 00:07:46 +0100 Subject: [PATCH 1/7] Adds ElseIfAttribute directive --- src/Tempest/View/ElseIfAttribute.php | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/Tempest/View/ElseIfAttribute.php diff --git a/src/Tempest/View/ElseIfAttribute.php b/src/Tempest/View/ElseIfAttribute.php new file mode 100644 index 000000000..e5de4f3aa --- /dev/null +++ b/src/Tempest/View/ElseIfAttribute.php @@ -0,0 +1,35 @@ +getPrevious(); + + if ( + !$previous instanceof GenericElement + || !$previous->hasAttribute('if') + ) { + throw new Exception('No valid if statement found in preceding element'); + } + + $elseif = $previous->getAttribute('elseif'); + + if ($elseif) { + return $element; + } else { + return new EmptyElement; + } + } +} \ No newline at end of file From 4cc3f99583e2e7d911d69e1a4df18a85c43e8f4d Mon Sep 17 00:00:00 2001 From: yassiNebeL Date: Tue, 6 Aug 2024 22:57:23 +0100 Subject: [PATCH 2/7] Upate AttributeFactory.php --- src/Tempest/View/Attributes/AttributeFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tempest/View/Attributes/AttributeFactory.php b/src/Tempest/View/Attributes/AttributeFactory.php index 46c7224ef..61a4dd466 100644 --- a/src/Tempest/View/Attributes/AttributeFactory.php +++ b/src/Tempest/View/Attributes/AttributeFactory.php @@ -14,6 +14,7 @@ public function make(View $view, string $name, ?string $value): Attribute return match(true) { $name === ':if' => new IfAttribute(), $name === ':else' => new ElseAttribute(), + $name === ':elseif' => new ElseIfAttribute(), $name === ':foreach' => new ForeachAttribute($view, $value), $name === ':forelse' => new ForelseAttribute(), str_starts_with(':', $name) && $value => new DataAttribute($view, $name, $value), From 0f39c9a5c916075bf90ad8888627a2ee4d646eaa Mon Sep 17 00:00:00 2001 From: yassiNebeL Date: Tue, 6 Aug 2024 22:58:45 +0100 Subject: [PATCH 3/7] fix --- src/Tempest/View/Attributes/AttributeFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tempest/View/Attributes/AttributeFactory.php b/src/Tempest/View/Attributes/AttributeFactory.php index 61a4dd466..20b56b18c 100644 --- a/src/Tempest/View/Attributes/AttributeFactory.php +++ b/src/Tempest/View/Attributes/AttributeFactory.php @@ -13,8 +13,8 @@ public function make(View $view, string $name, ?string $value): Attribute { return match(true) { $name === ':if' => new IfAttribute(), - $name === ':else' => new ElseAttribute(), $name === ':elseif' => new ElseIfAttribute(), + $name === ':else' => new ElseAttribute(), $name === ':foreach' => new ForeachAttribute($view, $value), $name === ':forelse' => new ForelseAttribute(), str_starts_with(':', $name) && $value => new DataAttribute($view, $name, $value), From c031e2a4f0c313f6c3ad50256513da8dd44fd6a1 Mon Sep 17 00:00:00 2001 From: Yassine Beloued <108750248+yassiNebeL@users.noreply.github.com> Date: Tue, 6 Aug 2024 23:12:54 +0100 Subject: [PATCH 4/7] Update ElseIfAttribute.php --- src/Tempest/View/ElseIfAttribute.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Tempest/View/ElseIfAttribute.php b/src/Tempest/View/ElseIfAttribute.php index e5de4f3aa..3baa994a8 100644 --- a/src/Tempest/View/ElseIfAttribute.php +++ b/src/Tempest/View/ElseIfAttribute.php @@ -19,17 +19,17 @@ public function apply(Element $element): Element if ( !$previous instanceof GenericElement - || !$previous->hasAttribute('if') + !$previous->hasAttribute('if') || !$previous->hasAttribute('elseif') ) { throw new Exception('No valid if statement found in preceding element'); } - $elseif = $previous->getAttribute('elseif'); + $condition = $previous->getAttribute('elseif'); - if ($elseif) { + if ($condition) { return $element; } else { return new EmptyElement; } } -} \ No newline at end of file +} From a00728987cb58e7d925357ddaecfb082e84c4f21 Mon Sep 17 00:00:00 2001 From: yassiNebeL Date: Tue, 6 Aug 2024 23:36:46 +0100 Subject: [PATCH 5/7] Move ElseIfAttribute.php --- src/Tempest/View/ElseIfAttribute.php | 35 ---------------------------- 1 file changed, 35 deletions(-) delete mode 100644 src/Tempest/View/ElseIfAttribute.php diff --git a/src/Tempest/View/ElseIfAttribute.php b/src/Tempest/View/ElseIfAttribute.php deleted file mode 100644 index 3baa994a8..000000000 --- a/src/Tempest/View/ElseIfAttribute.php +++ /dev/null @@ -1,35 +0,0 @@ -getPrevious(); - - if ( - !$previous instanceof GenericElement - !$previous->hasAttribute('if') || !$previous->hasAttribute('elseif') - ) { - throw new Exception('No valid if statement found in preceding element'); - } - - $condition = $previous->getAttribute('elseif'); - - if ($condition) { - return $element; - } else { - return new EmptyElement; - } - } -} From c5806e26b780f849f9a4a893a8a1febd825ec36a Mon Sep 17 00:00:00 2001 From: yassiNebeL Date: Tue, 6 Aug 2024 23:38:08 +0100 Subject: [PATCH 6/7] fix --- .../View/Attributes/ElseIfAttribute.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Tempest/View/Attributes/ElseIfAttribute.php diff --git a/src/Tempest/View/Attributes/ElseIfAttribute.php b/src/Tempest/View/Attributes/ElseIfAttribute.php new file mode 100644 index 000000000..4d997886b --- /dev/null +++ b/src/Tempest/View/Attributes/ElseIfAttribute.php @@ -0,0 +1,33 @@ +getPrevious(); + + + $condition = $element->getAttribute('elseif'); + + if ($condition) { + return $element; + } else { + return new EmptyElement; + } + } +} From ed42d45734cbdc135ad358d1a27f587047fdc017 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Tue, 13 Aug 2024 13:20:45 +0200 Subject: [PATCH 7/7] Finish :elseif support --- src/Tempest/View/Attributes/ElseAttribute.php | 22 +++++++---- .../View/Attributes/ElseIfAttribute.php | 31 ++++++++++++--- .../View/TempestViewRendererTest.php | 38 +++++++++++++++++++ 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/Tempest/View/Attributes/ElseAttribute.php b/src/Tempest/View/Attributes/ElseAttribute.php index c0a421b91..d9a5ba9a0 100644 --- a/src/Tempest/View/Attributes/ElseAttribute.php +++ b/src/Tempest/View/Attributes/ElseAttribute.php @@ -15,17 +15,25 @@ public function apply(Element $element): Element { $previous = $element->getPrevious(); + $previousCondition = false; - if ( - ! $previous instanceof GenericElement - || ! $previous->hasAttribute('if') - ) { - throw new Exception('No valid if condition found in preceding element'); + if (! $previous instanceof GenericElement) { + throw new Exception("Invalid preceding element before :else"); } - $condition = $previous->getAttribute('if'); + // 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 :else element + while ( + $previousCondition === false + && $previous instanceof GenericElement + && ($previous->hasAttribute('if') || $previous->hasAttribute('elseif')) + ) { + $previousCondition = (bool) ($previous->getAttribute('if') ?? $previous->getAttribute('elseif')); + $previous = $previous->getPrevious(); + } - if ($condition) { + if ($previousCondition) { return new EmptyElement(); } diff --git a/src/Tempest/View/Attributes/ElseIfAttribute.php b/src/Tempest/View/Attributes/ElseIfAttribute.php index 4d997886b..e7dd5eb2f 100644 --- a/src/Tempest/View/Attributes/ElseIfAttribute.php +++ b/src/Tempest/View/Attributes/ElseIfAttribute.php @@ -1,4 +1,5 @@ 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(); + } - $condition = $element->getAttribute('elseif'); + $currentCondition = (bool) $element->getAttribute('elseif'); - if ($condition) { + // 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; - } else { - return new EmptyElement; } + + return new EmptyElement(); } } diff --git a/tests/Integration/View/TempestViewRendererTest.php b/tests/Integration/View/TempestViewRendererTest.php index 4b271e53d..1f97e5c59 100644 --- a/tests/Integration/View/TempestViewRendererTest.php +++ b/tests/Integration/View/TempestViewRendererTest.php @@ -49,6 +49,44 @@ public function test_if_attribute(): void ); } + public function test_elseif_attribute(): void + { + $this->assertSame( + '
A
', + $this->render(view('
A
B
None
')->data(a: true, b: true)), + ); + + $this->assertSame( + '
A
', + $this->render(view('
A
B
None
')->data(a: true, b: false)), + ); + + $this->assertSame( + '
B
', + $this->render(view('
A
B
None
')->data(a: false, b: true)), + ); + + $this->assertSame( + '
None
', + $this->render(view('
A
B
None
')->data(a: false, b: false)), + ); + + $this->assertSame( + '
C
', + $this->render(view('
A
B
C
None
')->data(a: false, b: false, c: true)), + ); + + $this->assertSame( + '
B
', + $this->render(view('
A
B
C
None
')->data(a: false, b: true, c: true)), + ); + + $this->assertSame( + '
None
', + $this->render(view('
A
B
C
None
')->data(a: false, b: false, c: false)), + ); + } + public function test_else_attribute(): void { $this->assertSame(