Skip to content

Commit

Permalink
NEW: Add IsFirst/IsLast methods to match SS5 conventions (closes #1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinglozzer committed Nov 15, 2024
1 parent 5f8f754 commit b670501
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
28 changes: 24 additions & 4 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
22 changes: 20 additions & 2 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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()
Expand Down

0 comments on commit b670501

Please sign in to comment.