Skip to content

Commit 0753735

Browse files
committed
Add last() method
1 parent 96c540b commit 0753735

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/CryptoEncoding/PEMBundle.php

+15
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ public function first(): PEM
120120
return $this->_pems[0];
121121
}
122122

123+
/**
124+
* Get the last PEM in a bundle.
125+
*
126+
* @throws \LogicException If bundle contains no PEM objects
127+
*
128+
* @return PEM
129+
*/
130+
public function last(): PEM
131+
{
132+
if (!count($this->_pems)) {
133+
throw new \LogicException('No PEMs.');
134+
}
135+
return $this->_pems[count($this->_pems) - 1];
136+
}
137+
123138
/**
124139
* @see \Countable::count()
125140
*

test/unit/PEMBundleTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public function testAll(PEMBundle $bundle)
4141
public function testFirst(PEMBundle $bundle)
4242
{
4343
$this->assertInstanceOf(PEM::class, $bundle->first());
44+
$this->assertEquals($bundle->all()[0], $bundle->first());
45+
}
46+
47+
/**
48+
* @depends testBundle
49+
*
50+
* @param PEMBundle $bundle
51+
*/
52+
public function testLast(PEMBundle $bundle)
53+
{
54+
$this->assertInstanceOf(PEM::class, $bundle->last());
55+
$this->assertEquals($bundle->all()[149], $bundle->last());
4456
}
4557

4658
/**
@@ -117,6 +129,13 @@ public function testFirstEmptyFail()
117129
$bundle->first();
118130
}
119131

132+
public function testLastEmptyFail()
133+
{
134+
$bundle = new PEMBundle();
135+
$this->expectException(LogicException::class);
136+
$bundle->last();
137+
}
138+
120139
/**
121140
* @depends testBundle
122141
*

0 commit comments

Comments
 (0)