| @@ 92-105 (lines=14) @@ | ||
| 89 | * |
|
| 90 | * @return mixed|null |
|
| 91 | */ |
|
| 92 | public function first(callable $fn = null, $default = null) |
|
| 93 | { |
|
| 94 | if (is_null($fn)) { |
|
| 95 | return count($this->items) > 0 ? reset($this->items) : $default; |
|
| 96 | } |
|
| 97 | ||
| 98 | foreach ($this->getIterator() as $value) { |
|
| 99 | if (call_user_func($fn, $value)) { |
|
| 100 | return $value; |
|
| 101 | } |
|
| 102 | } |
|
| 103 | ||
| 104 | return $default; |
|
| 105 | } |
|
| 106 | ||
| 107 | /** |
|
| 108 | * Receive the last element that matches the optional callback |
|
| @@ 115-128 (lines=14) @@ | ||
| 112 | * |
|
| 113 | * @return mixed|null |
|
| 114 | */ |
|
| 115 | public function last(callable $fn = null, $default = null) |
|
| 116 | { |
|
| 117 | if (is_null($fn)) { |
|
| 118 | return count($this->items) > 0 ? end($this->items) : $default; |
|
| 119 | } |
|
| 120 | ||
| 121 | foreach (array_reverse($this->items) as $value) { |
|
| 122 | if (call_user_func($fn, $value)) { |
|
| 123 | return $value; |
|
| 124 | } |
|
| 125 | } |
|
| 126 | ||
| 127 | return $default; |
|
| 128 | } |
|
| 129 | } |
|
| 130 | ||