Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsFirst/IsLast methods to match SS5 conventions (closes #1274) #1275

Open
wants to merge 1 commit into
base: 5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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()
kinglozzer marked this conversation as resolved.
Show resolved Hide resolved
{
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return bool

Sorry, should have caught this last time. We don't need the PHPDoc if it doesn't give any additional context over the actual type hint.

*/
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
18 changes: 18 additions & 0 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,6 +208,15 @@ 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->Last());
$this->assertTrue($element2->Last());
Comment on lines +216 to +217
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertFalse($element->Last());
$this->assertTrue($element2->Last());
$this->assertFalse($element->IsLast());
$this->assertTrue($element2->IsLast());

}

public function testLast()
{
$element = $this->objFromFixture(ElementContent::class, 'content1');
Expand Down