From a00e6d75450546b4d67af672940af49dfd7d324f Mon Sep 17 00:00:00 2001 From: Ryuta Hamasaki Date: Wed, 12 Jun 2024 16:48:27 +0900 Subject: [PATCH 1/4] add documentation for "before" method --- collections.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/collections.md b/collections.md index fc9c1cb5fb1..2a8393c14ed 100644 --- a/collections.md +++ b/collections.md @@ -287,6 +287,35 @@ The `avg` method returns the [average value](https://en.wikipedia.org/wiki/Avera // 2 + +#### `before()` {.collection-method} + +The `before` method returns the item before the given item. It returns `null` if the given item is not found or is the first item: + + $collection = collect([1, 2, 3, 4, 5]); + + $collection->before(3); + + // 2 + + $collection->before(1); + + // null + +It searches for the given item using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use "strict" comparison, pass `true` as the second argument to the method: + + collect([2, 4, 6, 8])->before('4', $strict = true); + + // null + +Alternatively, you may provide your own closure to search for the first item that passes a given truth test: + + collect([2, 4, 6, 8])->before(function (int $item, int $key) { + return $item > 5; + }); + + // 4 + #### `chunk()` {.collection-method} From 8a9f83a013c48b77ff2b10ae402e84ba4da4f13b Mon Sep 17 00:00:00 2001 From: Ryuta Hamasaki Date: Wed, 12 Jun 2024 16:59:10 +0900 Subject: [PATCH 2/4] add documentation for "after" method --- collections.md | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/collections.md b/collections.md index 2a8393c14ed..2fc2febe315 100644 --- a/collections.md +++ b/collections.md @@ -94,9 +94,11 @@ For the majority of the remaining collection documentation, we'll discuss each m
+[after](#method-after) [all](#method-all) [average](#method-average) [avg](#method-avg) +[before](#method-before) [chunk](#method-chunk) [chunkWhile](#method-chunkwhile) [collapse](#method-collapse) @@ -255,6 +257,35 @@ For the majority of the remaining collection documentation, we'll discuss each m } + +#### `after()` {.collection-method} + +The `after` method returns the item after the given item. It returns `null` if the given item is not found or is the last item: + + $collection = collect([1, 2, 3, 4, 5]); + + $collection->after(3); + + // 4 + + $collection->after(5); + + // null + +It searches for the given item using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use "strict" comparison, pass `true` as the second argument to the method: + + collect([2, 4, 6, 8])->after('4', $strict = true); + + // null + +Alternatively, you may provide your own closure to search for the first item that passes a given truth test: + + collect([2, 4, 6, 8])->after(function (int $item, int $key) { + return $item > 5; + }); + + // 8 + #### `all()` {.collection-method .first-collection-method} @@ -290,7 +321,7 @@ The `avg` method returns the [average value](https://en.wikipedia.org/wiki/Avera #### `before()` {.collection-method} -The `before` method returns the item before the given item. It returns `null` if the given item is not found or is the first item: +The `before` method is the opposite of the [`after`](#method-after) method. It returns the item before the given item. It returns `null` if the given item is not found or is the first item: $collection = collect([1, 2, 3, 4, 5]); @@ -302,14 +333,10 @@ The `before` method returns the item before the given item. It returns `null` if // null -It searches for the given item using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use "strict" comparison, pass `true` as the second argument to the method: - collect([2, 4, 6, 8])->before('4', $strict = true); // null -Alternatively, you may provide your own closure to search for the first item that passes a given truth test: - collect([2, 4, 6, 8])->before(function (int $item, int $key) { return $item > 5; }); From 93fba4c137d5c4b221841687527ffadc175eb1fd Mon Sep 17 00:00:00 2001 From: Ryuta Hamasaki Date: Wed, 12 Jun 2024 17:01:59 +0900 Subject: [PATCH 3/4] mark "after" method as the first collection method --- collections.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collections.md b/collections.md index 2fc2febe315..351c8db38ba 100644 --- a/collections.md +++ b/collections.md @@ -258,7 +258,7 @@ For the majority of the remaining collection documentation, we'll discuss each m -#### `after()` {.collection-method} +#### `after()` {.collection-method .first-collection-method} The `after` method returns the item after the given item. It returns `null` if the given item is not found or is the last item: @@ -287,7 +287,7 @@ Alternatively, you may provide your own closure to search for the first item tha // 8 -#### `all()` {.collection-method .first-collection-method} +#### `all()` {.collection-method} The `all` method returns the underlying array represented by the collection: From fd9c9dd35d6c4367ec3121fc95e965f5c97f89df Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 18 Jun 2024 12:46:35 -0500 Subject: [PATCH 4/4] formatting --- collections.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/collections.md b/collections.md index 351c8db38ba..7a734bdf7e6 100644 --- a/collections.md +++ b/collections.md @@ -260,7 +260,7 @@ For the majority of the remaining collection documentation, we'll discuss each m #### `after()` {.collection-method .first-collection-method} -The `after` method returns the item after the given item. It returns `null` if the given item is not found or is the last item: +The `after` method returns the item after the given item. `null` is returned if the given item is not found or is the last item: $collection = collect([1, 2, 3, 4, 5]); @@ -272,9 +272,9 @@ The `after` method returns the item after the given item. It returns `null` if t // null -It searches for the given item using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use "strict" comparison, pass `true` as the second argument to the method: +This method searches for the given item using "loose" comparison, meaning a string containing an integer value will be considered equal to an integer of the same value. To use "strict" comparison, you may provide the `strict` argument to the method: - collect([2, 4, 6, 8])->after('4', $strict = true); + collect([2, 4, 6, 8])->after('4', strict: true); // null @@ -321,7 +321,7 @@ The `avg` method returns the [average value](https://en.wikipedia.org/wiki/Avera #### `before()` {.collection-method} -The `before` method is the opposite of the [`after`](#method-after) method. It returns the item before the given item. It returns `null` if the given item is not found or is the first item: +The `before` method is the opposite of the [`after`](#method-after) method. It returns the item before the given item. `null` is returned if the given item is not found or is the first item: $collection = collect([1, 2, 3, 4, 5]); @@ -333,7 +333,7 @@ The `before` method is the opposite of the [`after`](#method-after) method. It r // null - collect([2, 4, 6, 8])->before('4', $strict = true); + collect([2, 4, 6, 8])->before('4', strict: true); // null