Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3935766
D3197.id8157.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D3197.id8157.diff
View Options
diff --git a/omnitools/src/Collections/BaseCollection.php b/omnitools/src/Collections/BaseCollection.php
--- a/omnitools/src/Collections/BaseCollection.php
+++ b/omnitools/src/Collections/BaseCollection.php
@@ -5,6 +5,8 @@
abstract class BaseCollection {
+ use WithCollection;
+
///
/// Constructors
///
diff --git a/omnitools/src/Collections/WithCollection.php b/omnitools/src/Collections/WithCollection.php
new file mode 100644
--- /dev/null
+++ b/omnitools/src/Collections/WithCollection.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Keruald\OmniTools\Collections;
+
+use OutOfRangeException;
+
+trait WithCollection {
+
+ abstract function count () : int;
+ abstract function toArray() : array;
+
+ public function first () : mixed {
+ foreach ($this->toArray() as $item) {
+ return $item;
+ }
+
+ throw new OutOfRangeException("The collection is empty.");
+ }
+
+ public function firstOr (mixed $default) : mixed {
+ return match ($this->count()) {
+ 0 => $default,
+ default => $this->first(),
+ };
+ }
+
+}
diff --git a/omnitools/tests/Collections/BitsVectorTest.php b/omnitools/tests/Collections/BitsVectorTest.php
--- a/omnitools/tests/Collections/BitsVectorTest.php
+++ b/omnitools/tests/Collections/BitsVectorTest.php
@@ -3,6 +3,7 @@
namespace Keruald\OmniTools\Tests\Collections;
use InvalidArgumentException;
+use OutOfRangeException;
use Keruald\OmniTools\Collections\BitsVector;
use PHPUnit\Framework\TestCase;
@@ -230,4 +231,35 @@
$this->assertEquals([0, 0, 1, 0], $bits->toArray());
}
+ ///
+ /// WithCollection trait
+ ///
+
+ public function testFirst () : void {
+ $bits = BitsVector::new(4);
+ $bits[2] = 1;
+
+ $this->assertEquals(0, $bits->first());
+ }
+
+ public function testFirstWhenEmpty () : void {
+ $bits = BitsVector::new(0);
+
+ $this->expectException(OutOfRangeException::class);
+ $bits->first();
+ }
+
+ public function testFirstOr () : void {
+ $bits = BitsVector::new(4);
+ $bits[2] = 1;
+
+ $this->assertEquals(0, $bits->firstOr(2));
+ }
+
+ public function testFirstOrWhenEmpty () : void {
+ $bits = BitsVector::new(0);
+
+ $this->assertEquals(2, $bits->firstOr(2));
+ }
+
}
diff --git a/omnitools/tests/Collections/HashMapTest.php b/omnitools/tests/Collections/HashMapTest.php
--- a/omnitools/tests/Collections/HashMapTest.php
+++ b/omnitools/tests/Collections/HashMapTest.php
@@ -2,12 +2,14 @@
namespace Keruald\OmniTools\Tests\Collections;
+use Keruald\OmniTools\Collections\BitsVector;
use Keruald\OmniTools\Collections\HashMap;
use PHPUnit\Framework\TestCase;
use InvalidArgumentException;
use IteratorAggregate;
+use OutOfRangeException;
use Traversable;
class HashMapTest extends TestCase {
@@ -462,4 +464,32 @@
$this->assertEquals(self::MAP_CONTENT, iterator_to_array($this->map));
}
+ ///
+ /// WithCollection trait
+ ///
+
+ public function testFirst () : void {
+ $this->assertEquals("Iain Banks", $this->map->first());
+ }
+
+ public function testFirstWhenEmpty () : void {
+ $map = new HashMap();
+
+ $this->expectException(OutOfRangeException::class);
+ $map->first();
+ }
+
+ public function testFirstOr () : void {
+ $bits = BitsVector::new(4);
+ $bits[2] = 1;
+
+ $this->assertEquals("Iain Banks", $this->map->firstOr("Anonymous"));
+ }
+
+ public function testFirstOrWhenEmpty () : void {
+ $map = new HashMap();
+
+ $this->assertEquals("Anonymous", $map->firstOr("Anonymous"));
+ }
+
}
diff --git a/omnitools/tests/Collections/VectorTest.php b/omnitools/tests/Collections/VectorTest.php
--- a/omnitools/tests/Collections/VectorTest.php
+++ b/omnitools/tests/Collections/VectorTest.php
@@ -9,6 +9,7 @@
use InvalidArgumentException;
use IteratorAggregate;
+use OutOfRangeException;
use Traversable;
/**
@@ -373,4 +374,29 @@
$this->assertEquals([1, 2, 3, 4, 5], iterator_to_array($this->vector));
}
+ ///
+ /// WithCollection trait
+ ///
+
+ public function testFirst () : void {
+ $this->assertEquals(1, $this->vector->first());
+ }
+
+ public function testFirstWhenEmpty () : void {
+ $vector = new Vector;
+
+ $this->expectException(OutOfRangeException::class);
+ $vector->first();
+ }
+
+ public function testFirstOr () : void {
+ $this->assertEquals(1, $this->vector->firstOr(2));
+ }
+
+ public function testFirstOrWhenEmpty () : void {
+ $vector = new Vector;
+
+ $this->assertEquals(2, $vector->firstOr(2));
+ }
+
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Dec 24, 09:26 (18 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2313859
Default Alt Text
D3197.id8157.diff (4 KB)
Attached To
Mode
D3197: Allow to get first item of a collection
Attached
Detach File
Event Timeline
Log In to Comment