Skip to content
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

$collection->group(): rename parameter to $caseInsensitive #5634

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Cms/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ public function findBy(string $attribute, $value)
* with an item for each group and a collection for each group.
*
* @param string|Closure $field
* @param bool $i Ignore upper/lowercase for group names
* @param bool $caseInsensitive Ignore upper/lowercase for group names
* @return \Kirby\Cms\Collection
* @throws \Kirby\Exception\Exception
*/
public function group($field, bool $i = true)
public function group($field, bool $caseInsensitive = true)
{
if (is_string($field) === true) {
$groups = new Collection([], $this->parent());
Expand All @@ -164,8 +164,12 @@ public function group($field, bool $i = true)
throw new InvalidArgumentException('Invalid grouping value for key: ' . $key);
}

$value = (string)$value;

// ignore upper/lowercase for group names
$value = $i === true ? Str::lower($value) : (string)$value;
if ($caseInsensitive === true) {
$value = Str::lower($value);
}

if (isset($groups->data[$value]) === false) {
// create a new entry for the group if it does not exist yet
Expand All @@ -179,7 +183,7 @@ public function group($field, bool $i = true)
return $groups;
}

return parent::group($field, $i);
return parent::group($field, $caseInsensitive);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/Toolkit/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,21 +516,24 @@ protected function getAttributeFromObject($object, string $attribute)
* Groups the elements by a given field or callback function
*
* @param string|Closure $field
* @param bool $i
* @return \Kirby\Toolkit\Collection A new collection with an element for
* each group and a subcollection in
* each group
* @throws \Exception if $field is not a string nor a callback function
*/
public function group($field, bool $i = true)
public function group($field, bool $caseInsensitive = true)
{
// group by field name
if (is_string($field) === true) {
return $this->group(function ($item) use ($field, $i) {
return $this->group(function ($item) use ($field, $caseInsensitive) {
$value = $this->getAttribute($item, $field);

// ignore upper/lowercase for group names
return $i === true ? Str::lower($value) : (string)$value;
if ($caseInsensitive === true) {
return Str::lower($value);
}

return (string)$value;
});
}

Expand Down