From b670501f9bd8e1aa33a7ba0c4b839b454a4ccaf8 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Tue, 12 Nov 2024 14:25:42 +0000 Subject: [PATCH] NEW: Add IsFirst/IsLast methods to match SS5 conventions (closes #1274) --- src/Models/BaseElement.php | 28 ++++++++++++++++++++++++---- tests/BaseElementTest.php | 22 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Models/BaseElement.php b/src/Models/BaseElement.php index daf287e8..3a4bcaa6 100644 --- a/src/Models/BaseElement.php +++ b/src/Models/BaseElement.php @@ -1232,21 +1232,41 @@ public function getPageTitle() } /** - * @return boolean + * Returns true if this is the first element rendered in the ElementalArea + * @return bool */ - public function First() + public function IsFirst(): bool { return ($this->Parent()->Elements()->first()->ID === $this->ID); } /** - * @return boolean + * @deprecated 5.4.0 Use IsFirst() instead. */ - public function Last() + public function First() + { + Deprecation::notice('5.4.0', 'Use IsFirst() instead'); + return $this->IsFirst(); + } + + /** + * Returns true if this is the last element rendered in the ElementalArea + * @return bool + */ + public function IsLast(): bool { return ($this->Parent()->Elements()->last()->ID === $this->ID); } + /** + * @deprecated 5.4.0 Use IsLast() instead. + */ + public function Last() + { + Deprecation::notice('5.4.0', 'Use IsLast() instead'); + return $this->IsLast(); + } + /** * @return int */ diff --git a/tests/BaseElementTest.php b/tests/BaseElementTest.php index ad6ce471..cc175497 100644 --- a/tests/BaseElementTest.php +++ b/tests/BaseElementTest.php @@ -190,6 +190,15 @@ public function testStyleVariants() $this->assertEquals('', $element->getStyleVariant()); } + public function testIsFirst() + { + $element = $this->objFromFixture(ElementContent::class, 'content1'); + $element2 = $this->objFromFixture(ElementContent::class, 'content2'); + + $this->assertTrue($element->IsFirst()); + $this->assertFalse($element2->IsFirst()); + } + public function testFirst() { $element = $this->objFromFixture(ElementContent::class, 'content1'); @@ -199,13 +208,22 @@ public function testFirst() $this->assertFalse($element2->First()); } + public function testIsLast() + { + $element = $this->objFromFixture(ElementContent::class, 'content1'); + $element2 = $this->objFromFixture(ElementContent::class, 'content2'); + + $this->assertFalse($element->IsLast()); + $this->assertTrue($element2->IsLast()); + } + public function testLast() { $element = $this->objFromFixture(ElementContent::class, 'content1'); $element2 = $this->objFromFixture(ElementContent::class, 'content2'); - $this->assertFalse($element->Last()); - $this->assertTrue($element2->Last()); + $this->assertTrue($element->Last()); + $this->assertFalse($element2->Last()); } public function testTotalItems()