-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.8] Add join
method to collection
#27723
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -296,6 +296,36 @@ public function containsStrict($key, $value = null) | |
return in_array($key, $this->items, true); | ||
} | ||
|
||
/** | ||
* Join all items from the collection using a string. The final items can use a separate glue string. | ||
* | ||
* @param string $glue | ||
* @param string $finalGlue | ||
* @return string | ||
*/ | ||
public function join($glue, $finalGlue = '') | ||
{ | ||
if ($finalGlue === '') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we change the default value to |
||
return $this->implode($glue); | ||
} | ||
|
||
$count = $this->count(); | ||
|
||
if ($count === 0) { | ||
return ''; | ||
} | ||
|
||
if ($count === 1) { | ||
return $this->last(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be cast to string explicitly? |
||
} | ||
|
||
$collection = new static($this->items); | ||
|
||
$finalItem = $collection->pop(); | ||
|
||
return $collection->implode($glue).$finalGlue.$finalItem; | ||
} | ||
|
||
/** | ||
* Cross join with the given lists, returning all possible permutations. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename
finalGlue
tolastGlue
?