@@ 8-49 (lines=42) @@ | ||
5 | ||
6 | namespace Zicht\Itertools\lib\Traits; |
|
7 | ||
8 | trait FirstTrait |
|
9 | { |
|
10 | /** |
|
11 | * Returns the first element of this iterable or |
|
12 | * returns $default when this iterable is empty |
|
13 | * |
|
14 | * @param mixed $default |
|
15 | * @return mixed |
|
16 | */ |
|
17 | public function first($default = null) |
|
18 | { |
|
19 | if ($this instanceof \Iterator) { |
|
20 | $item = $default; |
|
21 | foreach ($this as $item) { |
|
22 | break; |
|
23 | } |
|
24 | return $item; |
|
25 | } |
|
26 | ||
27 | return null; |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * Returns the key of the first element of this iterable or |
|
32 | * returns $DEFAULT when this iterable is empty |
|
33 | * |
|
34 | * @param mixed $default |
|
35 | * @return mixed |
|
36 | */ |
|
37 | public function firstKey($default = null) |
|
38 | { |
|
39 | if ($this instanceof \Iterator) { |
|
40 | $key = $default; |
|
41 | foreach ($this as $key => $value) { |
|
42 | break; |
|
43 | } |
|
44 | return $key; |
|
45 | } |
|
46 | ||
47 | return null; |
|
48 | } |
|
49 | } |
|
50 |
@@ 8-47 (lines=40) @@ | ||
5 | ||
6 | namespace Zicht\Itertools\lib\Traits; |
|
7 | ||
8 | trait LastTrait |
|
9 | { |
|
10 | /** |
|
11 | * Returns the last element of this iterable or |
|
12 | * returns $default when this iterable is empty |
|
13 | * |
|
14 | * @param mixed $default |
|
15 | * @return mixed |
|
16 | */ |
|
17 | public function last($default = null) |
|
18 | { |
|
19 | if ($this instanceof \Iterator) { |
|
20 | $item = $default; |
|
21 | foreach ($this as $item) { |
|
22 | } |
|
23 | return $item; |
|
24 | } |
|
25 | ||
26 | return null; |
|
27 | } |
|
28 | ||
29 | /** |
|
30 | * Returns the key of the last element of this iterable or |
|
31 | * returns $DEFAULT when this iterable is empty |
|
32 | * |
|
33 | * @param mixed $default |
|
34 | * @return mixed |
|
35 | */ |
|
36 | public function lastKey($default = null) |
|
37 | { |
|
38 | if ($this instanceof \Iterator) { |
|
39 | $key = $default; |
|
40 | foreach ($this as $key => $value) { |
|
41 | } |
|
42 | return $key; |
|
43 | } |
|
44 | ||
45 | return null; |
|
46 | } |
|
47 | } |
|
48 |