File tree 2 files changed +24
-0
lines changed
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -120,6 +120,17 @@ public function sort(?callable $comparator = null): self
120
120
return static ::from ($ array );
121
121
}
122
122
123
+ public function first ()
124
+ {
125
+ if ($ this instanceof \IteratorAggregate) {
126
+ // PHP 8: SplFixedArray no longer implements Iterator
127
+ $ this ->getIterator ()->rewind ();
128
+ return $ this ->getIterator ()->current () ?: null ;
129
+ }
130
+ $ this ->rewind ();
131
+ return $ this ->current () ?: null ;
132
+ }
133
+
123
134
public function jsonSerialize (): array
124
135
{
125
136
return $ this ->toArray ();
Original file line number Diff line number Diff line change @@ -93,4 +93,17 @@ public function __construct(\DateTimeInterface ...$items)
93
93
94
94
$ this ->assertSame ($ expected , $ result ->toArray ());
95
95
}
96
+
97
+ public function testFirst (): void
98
+ {
99
+ $ collection = Collection::from (range (1 , 10 ));
100
+ $ sum = 0 ;
101
+ foreach ($ collection as $ item ) {
102
+ // just iterate over it
103
+ $ sum += $ item ;
104
+ }
105
+
106
+ $ this ->assertSame ($ collection ->first (), 1 );
107
+ $ this ->assertSame (55 , $ sum );
108
+ }
96
109
}
You can’t perform that action at this time.
0 commit comments