Skip to content

Commit fb1eba2

Browse files
committed
NEW: Add IsFirst/IsLast methods to match SS5 conventions (closes #1274)
1 parent 5f8f754 commit fb1eba2

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/Models/BaseElement.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,21 +1232,41 @@ public function getPageTitle()
12321232
}
12331233

12341234
/**
1235-
* @return boolean
1235+
* Returns true if this is the first element rendered in the ElementalArea
1236+
* @return bool
12361237
*/
1237-
public function First()
1238+
public function IsFirst(): bool
12381239
{
12391240
return ($this->Parent()->Elements()->first()->ID === $this->ID);
12401241
}
12411242

12421243
/**
1243-
* @return boolean
1244+
* @deprecated 5.4.0 Use IsFirst() instead.
12441245
*/
1245-
public function Last()
1246+
public function First()
1247+
{
1248+
Deprecation::notice('5.4.0', 'Use IsFirst() instead');
1249+
return $this->IsFirst();
1250+
}
1251+
1252+
/**
1253+
* Returns true if this is the last element rendered in the ElementalArea
1254+
* @return bool
1255+
*/
1256+
public function IsLast(): bool
12461257
{
12471258
return ($this->Parent()->Elements()->last()->ID === $this->ID);
12481259
}
12491260

1261+
/**
1262+
* @deprecated 5.4.0 Use IsLast() instead.
1263+
*/
1264+
public function Last()
1265+
{
1266+
Deprecation::notice('5.4.0', 'Use IsLast() instead');
1267+
return $this->IsLast();
1268+
}
1269+
12501270
/**
12511271
* @return int
12521272
*/

tests/BaseElementTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ public function testStyleVariants()
190190
$this->assertEquals('', $element->getStyleVariant());
191191
}
192192

193+
public function testIsFirst()
194+
{
195+
$element = $this->objFromFixture(ElementContent::class, 'content1');
196+
$element2 = $this->objFromFixture(ElementContent::class, 'content2');
197+
198+
$this->assertTrue($element->IsFirst());
199+
$this->assertFalse($element2->IsFirst());
200+
}
201+
193202
public function testFirst()
194203
{
195204
$element = $this->objFromFixture(ElementContent::class, 'content1');
@@ -199,6 +208,15 @@ public function testFirst()
199208
$this->assertFalse($element2->First());
200209
}
201210

211+
public function testIsLast()
212+
{
213+
$element = $this->objFromFixture(ElementContent::class, 'content1');
214+
$element2 = $this->objFromFixture(ElementContent::class, 'content2');
215+
216+
$this->assertFalse($element->Last());
217+
$this->assertTrue($element2->Last());
218+
}
219+
202220
public function testLast()
203221
{
204222
$element = $this->objFromFixture(ElementContent::class, 'content1');

0 commit comments

Comments
 (0)