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

[11.x] Introduce Collection/LazyCollection isAssoc and isList methods #53199

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,30 @@ public function groupBy($groupBy, $preserveKeys = false)
return $result;
}

/**
* Determine if the collection is associative.
*
* The collection is "associative" if it doesn't have sequential numerical keys beginning with zero.
*
* @return bool
*/
public function isAssoc()
{
return Arr::isAssoc($this->items);
}

/**
* Determine if the collection is a list.
*
* The collection is a "list" if all keys are sequential integers starting from 0 with no gaps in between.
*
* @return bool
*/
public function isList()
{
return Arr::isList($this->items);
}

/**
* Key an associative array by a field or using a callback.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,30 @@ public function groupBy($groupBy, $preserveKeys = false)
return $this->passthru('groupBy', func_get_args());
}

/**
* Determine if the collection is associative.
*
* The collection is "associative" if it doesn't have sequential numerical keys beginning with zero.
*
* @return bool
*/
public function isAssoc()
{
return Arr::isAssoc($this->all());
}

/**
* Determine if the collection is a list.
*
* The collection is a "list" if all keys are sequential integers starting from 0 with no gaps in between.
*
* @return bool
*/
public function isList()
{
return Arr::isList($this->all());
}

/**
* Key an associative array by a field or using a callback.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,42 @@ function ($item) {
$this->assertEquals($expected_result, $result->toArray());
}

#[DataProvider('collectionClassProvider')]
public function testIsAssoc($collection)
{
$this->assertTrue((new $collection(['a' => 'a', 0 => 'b']))->isAssoc());
$this->assertTrue((new $collection([1 => 'a', 0 => 'b']))->isAssoc());
$this->assertTrue((new $collection([1 => 'a', 2 => 'b']))->isAssoc());
$this->assertTrue((new $collection([1 => 'foo', 'bar']))->isAssoc());
$this->assertTrue((new $collection([0 => 'foo', 'bar' => 'baz']))->isAssoc());
$this->assertTrue((new $collection([0 => 'foo', 2 => 'bar']))->isAssoc());
$this->assertTrue((new $collection(['foo' => 'bar', 'baz' => 'qux']))->isAssoc());

$this->assertFalse((new $collection([]))->isAssoc());
$this->assertFalse((new $collection([0 => 'a', 1 => 'b']))->isAssoc());
$this->assertFalse((new $collection(['a', 'b']))->isAssoc());
$this->assertFalse((new $collection([1, 2, 3]))->isAssoc());
$this->assertFalse((new $collection(['foo', 2, 3]))->isAssoc());
$this->assertFalse((new $collection([0 => 'foo', 'bar']))->isAssoc());
}

#[DataProvider('collectionClassProvider')]
public function testIsList($collection)
{
$this->assertTrue((new $collection([]))->isList());
$this->assertTrue((new $collection([1, 2, 3]))->isList());
$this->assertTrue((new $collection(['foo', 2, 3]))->isList());
$this->assertTrue((new $collection(['foo', 'bar']))->isList());
$this->assertTrue((new $collection([0 => 'foo', 'bar']))->isList());
$this->assertTrue((new $collection([0 => 'foo', 1 => 'bar']))->isList());

$this->assertFalse((new $collection([1 => 'foo', 'bar']))->isList());
$this->assertFalse((new $collection([1 => 'foo', 0 => 'bar']))->isList());
$this->assertFalse((new $collection([0 => 'foo', 'bar' => 'baz']))->isList());
$this->assertFalse((new $collection([0 => 'foo', 2 => 'bar']))->isList());
$this->assertFalse((new $collection(['foo' => 'bar', 'baz' => 'qux']))->isList());
}

#[DataProvider('collectionClassProvider')]
public function testKeyByAttribute($collection)
{
Expand Down